跳到主要内容
版本:2.x

测试

所有本指南内的示例都会使用 Pest 编写。当然,这些同样也适用于 PHPUnit。

由于表格构造器是基于 Livewire 组件的,因此你也可以使用 Livewire 测试辅助函数。不过,我们自定义了一个测试辅助函数,让你可以用于表格测试:

Render

使用 assertSuccessful() Livewire helper,测试表格组件渲染:

use function Pest\Livewire\livewire;

it('can render page', function () {
livewire(ListPosts::class)->assertSuccessful();
});

要断言是否可以看见表单中的特定记录,将其传入 assertCanSeeTableRecord():

use function Pest\Livewire\livewire;

it('can list posts', function () {
$posts = Post::factory()->count(10)->create();

livewire(PostResource\Pages\ListPosts::class)
->assertCanSeeTableRecords($posts);
});

注意,如果表格使用分页,assertCanSeeTableRecords() 将只会返回首页的记录。要切换页面,可以调用 set('page', 2)

Columns

要断言某个特定的列是否渲染,将列名传入到 assertCanRenderTableColumn():

use function Pest\Livewire\livewire;

it('can render post titles', function () {
Post::factory()->count(10)->create();

livewire(PostResource\Pages\ListPosts::class)
->assertCanRenderTableColumn('title');
});

该辅助函数将获得该列的 HTML,并检测其是否在表格中出现。

要断言一个列没有被渲染,你可以使用 assertCanNotRenderTableColumn():

use function Pest\Livewire\livewire;

it('can not render post comments', function () {
Post::factory()->count(10)->create()

livewire(PostResource\Pages\ListPosts::class)
->assertCanNotRenderTableColumn('comments');
});

该辅助函数将断言该类的 HTML,默认不显示在当前表格。

Sorting

To sort table records, you can call sortTable(), passing the name of the column to sort by. You can use 'desc' in the second parameter of sortTable() to reverse the sorting direction.

Once the table is sorted, you can ensure that the table records are rendered in order using assertCanSeeTableRecords() with the inOrder parameter:

use function Pest\Livewire\livewire;

it('can sort posts by title', function () {
$posts = Post::factory()->count(10)->create();

livewire(PostResource\Pages\ListPosts::class)
->sortTable('title')
->assertCanSeeTableRecords($posts->sortBy('title'), inOrder: true)
->sortTable('title', 'desc')
->assertCanSeeTableRecords($posts->sortByDesc('title'), inOrder: true);
});

Searching

To search the table, call the searchTable() method with your search query.

You can then use assertCanSeeTableRecords() to check your filtered table records, and use assertCanNotSeeTableRecords() to assert that some records are no longer in the table:

use function Pest\Livewire\livewire;

it('can search posts by title', function () {
$posts = Post::factory()->count(10)->create();

$title = $posts->first()->title;

livewire(PostResource\Pages\ListPosts::class)
->searchTable($title)
->assertCanSeeTableRecords($posts->where('title', $title))
->assertCanNotSeeTableRecords($posts->where('title', '!=', $title));
});

State

To assert that a certain column has a state for a record:

use function Pest\Livewire\livewire;

it('can get post author names', function () {
$posts = Post::factory()->count(10)->create();

$post = $posts->first();

livewire(PostResource\Pages\ListPosts::class)
->assertTableColumnStateSet('author.name', $post->author->name, record: $post);
});

Authorization

To ensure that a particular user cannot see a column, you can use the assertTableColumnHidden() method:

use function Pest\Livewire\livewire;

it('can hide the author column', function () {
livewire(PostResource\Pages\ListPosts::class)
->assertTableColumnHidden(`author`);
});

Filters

To filter the table records, you can use the filterTable() method, along with assertCanSeeTableRecords() and assertCanNotSeeTableRecords():

use function Pest\Livewire\livewire;

it('can filter posts by `is_published`', function () {
$posts = Post::factory()->count(10)->create();

livewire(PostResource\Pages\ListPosts::class)
->assertCanSeeTableRecords($posts)
->filterTable('is_published')
->assertCanSeeTableRecords($posts->where('is_published', true))
->assertCanNotSeeTableRecords($posts->where('is_published', false));
});

For a simple filter, this will just enable the filter.

If you'd like to set the value of a SelectFilter or TernaryFilter, pass the value as a second argument:

use function Pest\Livewire\livewire;

it('can filter posts by `author_id`', function () {
$posts = Post::factory()->count(10)->create();

$authorId = $posts->first()->author_id;

livewire(PostResource\Pages\ListPosts::class)
->assertCanSeeTableRecords($posts)
->filterTable('author_id', $authorId)
->assertCanSeeTableRecords($posts->where('author_id', $authorId))
->assertCanNotSeeTableRecords($posts->where('author_id', '!=', $authorId));
});

Resetting filters

To reset all filters to their original state, call resetTableFilters()

use function Pest\Livewire\livewire;

it('can reset table filters`', function () {
$posts = Post::factory()->count(10)->create();

livewire(PostResource\Pages\ListPosts::class)
->resetTableFilters();
});

Actions

You can call an action by passing its name or class to callTableAction():

use function Pest\Livewire\livewire;

it('can delete posts', function () {
$post = Post::factory()->create();

livewire(PostResource\Pages\ListPosts::class)
->callTableAction(DeleteAction::class, $post);

$this->assertModelMissing($post);
});

This example assumes that you have a DeleteAction on your table. If you have a custom Action::make('reorder'), you may use callTableAction('reorder').

For bulk actions, you may do the same, passing in records to execute the bulk action against to callTableBulkAction():

use function Pest\Livewire\livewire;

it('can bulk delete posts', function () {
$posts = Post::factory()->count(10)->create();

livewire(PostResource\Pages\ListPosts::class)
->callTableBulkAction(DeleteBulkAction::class, $posts);

foreach ($posts as $post) {
$this->assertModelMissing($post);
}
});

To pass an array of data into an action, use the data parameter:

use function Pest\Livewire\livewire;

it('can edit posts', function () {
$post = Post::factory()->create();

livewire(PostResource\Pages\ListPosts::class)
->callTableAction(EditAction::class, $post, data: [
'title' => $title = fake()->words(asText: true),
])
->assertHasNoTableActionErrors();

expect($post->refresh())
->title->toBe($title);
});

assertHasNoTableActionErrors() is used to assert that no validation errors occurred when submitting the action form.

To check if a validation error has occurred with the data, use assertHasTableActionErrors(), similar to assertHasErrors() in Livewire:

use function Pest\Livewire\livewire;

it('can validate edited post data', function () {
$post = Post::factory()->create();

livewire(PostResource\Pages\ListPosts::class)
->callTableAction(EditAction::class, $post, data: [
'title' => null,
])
->assertHasTableActionErrors(['title' => ['required']]);
});

For bulk actions, this method is called assertHasTableBulkActionErrors().

To check if an action or bulk action is pre-filled with data, you can use the assertTableActionDataSet() or assertTableBulkActionDataSet() method:

use function Pest\Livewire\livewire;

it('can load existing post data for editing', function () {
$post = Post::factory()->create();

livewire(PostResource\Pages\ListPosts::class)
->mountTableAction(EditAction::class, $post)
->assertTableActionDataSet([[
'title' => $post->title,
])
->setTableActionData([[
'title' => $title = fake()->words(asText: true),
])
->callMountedTableAction()
->assertHasNoTableActionErrors();

expect($post->refresh())
->title->toBe($title);
});

To ensure that an action or bulk action is hidden for a user, you can use the assertTableActionHidden() or assertTableBulkActionHidden() method:

use function Pest\Livewire\livewire;

it('can not publish posts', function () {
$post = Post::factory()->create();

livewire(PostResource\Pages\ListPosts::class)
->assertTableActionHidden('publish', $post)
->assertTableBulkActionHidden('publish');
});