Languages

Version

Theme

测试

测试表格

测试表格是否可以渲染

要短语表格组件能否渲染,请使用 assertSuccessful() Livewire 助手:

use function Pest\Livewire\livewire;

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

要测试显示了哪些记录,你可以使用 assertCanSeeTableRecords()assertCanNotSeeTableRecords()assertCountTableRecords()

use function Pest\Livewire\livewire;

it('cannot display trashed posts by default', function () {
    $posts = Post::factory()->count(4)->create();
    $trashedPosts = Post::factory()->trashed()->count(6)->create();

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

如果的你表格使用分页功能,assertCanSeeTableRecords() 将仅检查第一页的记录。要切换页面,请调用 call('gotoPage', 2)

如果你的表格使用 deferLoading(),则应在 assertCanSeeTableRecords() 之前调用 loadTable()

测试表格列

要断言渲染了某一特定列,请将列名传递给 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 默认不显示在当前表中。

测试列是否可以搜索

要搜索表格,请调用 searchTable() 方法并输入你的搜索查询。

然后,你可以使用 assertCanSeeTableRecords() 检查已过滤的表格记录,并使用 assertCanNotSeeTableRecords() 断言某些记录不再存在于表格中:

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));
});

要搜索单个列,你可以将搜索数组传递给 searchTableColumns()

use function Pest\Livewire\livewire;

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

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

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

测试列是否可以排序

要对表记录进行排序,你可以调用 sortTable(),并传递要排序的列的名称。你可以在 sortTable() 的第二个参数中使用 'desc' 来反转排序方向。

对表进行排序后,你可以使用 assertCanSeeTableRecords()inOrder 参数来断言表记录是否按顺序渲染:

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);
});

测试列的状态

要断言某条记录的某个列有状态或无状态,可以使用 assertTableColumnStateSet()assertTableColumnStateNotSet()

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)
        ->assertTableColumnStateNotSet('author.name', 'Anonymous', record: $post);
});

要断言某一列具有格式化状态或不具有记录的格式化状态,你可以使用 assertTableColumnFormattedStateSet()assertTableColumnFormattedStateNotSet()

use function Pest\Livewire\livewire;

it('can get post author names', function () {
    $post = Post::factory(['name' => 'John Smith'])->create();

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

测试列是否存在

要断言列存在,可以使用 assertTableColumnExists() 方法:

use function Pest\Livewire\livewire;

it('has an author column', function () {
    livewire(PostResource\Pages\ListPosts::class)
        ->assertTableColumnExists('author');
});

你可以传递一个函数作为附加参数,以断言某列通过了给定的“真值测试”。这对于断言某列具有特定配置非常有用。你还可以传递一条记录作为第三个参数,如果你的检查依赖于正在渲染的表行,这将非常有用:

use function Pest\Livewire\livewire;
use Filament\Tables\Columns\TextColumn;

it('has an author column', function () {
    $post = Post::factory()->create();
    
    livewire(PostResource\Pages\ListPosts::class)
        ->assertTableColumnExists('author', function (TextColumn $column): bool {
            return $column->getDescriptionBelow() === $post->subtitle;
        }, $post);
});

测试列的可见性

要断言特定用户无法看到某个列,你可以使用 assertTableColumnVisible()assertTableColumnHidden() 方法:

use function Pest\Livewire\livewire;

it('shows the correct columns', function () {
    livewire(PostResource\Pages\ListPosts::class)
        ->assertTableColumnVisible('created_at')
        ->assertTableColumnHidden('author');
});

测试列的描述

要断言列的上方或下方描述正确,你可以使用 assertTableColumnHasDescription()assertTableColumnDoesNotHaveDescription() 方法:

use function Pest\Livewire\livewire;

it('has the correct descriptions above and below author', function () {
    $post = Post::factory()->create();

    livewire(PostsTable::class)
        ->assertTableColumnHasDescription('author', 'Author! ↓↓↓', $post, 'above')
        ->assertTableColumnHasDescription('author', 'Author! ↑↑↑', $post)
        ->assertTableColumnDoesNotHaveDescription('author', 'Author! ↑↑↑', $post, 'above')
        ->assertTableColumnDoesNotHaveDescription('author', 'Author! ↓↓↓', $post);
});

测试列的额外属性

要断言列具有正确的额外属性,你可以使用 assertTableColumnHasExtraAttributes()assertTableColumnDoesNotHaveExtraAttributes() 方法:

use function Pest\Livewire\livewire;

it('displays author in red', function () {
    $post = Post::factory()->create();

    livewire(PostsTable::class)
        ->assertTableColumnHasExtraAttributes('author', ['class' => 'text-danger-500'], $post)
        ->assertTableColumnDoesNotHaveExtraAttributes('author', ['class' => 'text-primary-500'], $post);
});

测试 SelectColumn 中的选项

如果你有一个 Select 列,你可以使用 assertTableSelectColumnHasOptions()assertTableSelectColumnDoesNotHaveOptions() 断言它具有正确的选项:

use function Pest\Livewire\livewire;

it('has the correct statuses', function () {
    $post = Post::factory()->create();

    livewire(PostsTable::class)
        ->assertTableSelectColumnHasOptions('status', ['unpublished' => 'Unpublished', 'published' => 'Published'], $post)
        ->assertTableSelectColumnDoesNotHaveOptions('status', ['archived' => 'Archived'], $post);
});

测试过滤器

要过滤表格的记录,可以使用 filterTable() 方法以及 assertCanSeeTableRecords()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));
});

对于简单的过滤器,这只会启用过滤器。

如果你想设置 SelectFilterTernaryFilter 的值,请将该值作为第二个参数传递:

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));
});

在测试中重置过滤器

要将所有过滤器重置为原始状态,请调用 resetTableFilters()

use function Pest\Livewire\livewire;

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

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

在测试中移除过滤器

要移除单个过滤器,你可以使用 removeTableFilter()

use function Pest\Livewire\livewire;

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

    $unpublishedPosts = $posts->where('is_published', false)->get();

    livewire(PostsTable::class)
        ->filterTable('is_published')
        ->assertCanNotSeeTableRecords($unpublishedPosts)
        ->removeTableFilter('is_published')
        ->assertCanSeeTableRecords($posts);
});

要移除所有过滤器,你可以使用 removeTableFilters()

use function Pest\Livewire\livewire;

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

    $unpublishedPosts = $posts
        ->where('is_published', false)
        ->where('author_id', $posts->first()->author->getKey());

    livewire(PostsTable::class)
        ->filterTable('is_published')
        ->filterTable('author', $author)
        ->assertCanNotSeeTableRecords($unpublishedPosts)
        ->removeTableFilters()
        ->assertCanSeeTableRecords($posts);
});

测试过滤器的可见性

要断言特定用户无法看到过滤器,你可以使用 assertTableFilterVisible()assertTableFilterHidden() 方法:

use function Pest\Livewire\livewire;

it('shows the correct filters', function () {
    livewire(PostsTable::class)
        ->assertTableFilterVisible('created_at')
        ->assertTableFilterHidden('author');
});

测试过滤器的存在

要断言过滤器存在,你可以使用 assertTableFilterExists()方法:

use function Pest\Livewire\livewire;

it('has an author filter', function () {
    livewire(PostResource\Pages\ListPosts::class)
        ->assertTableFilterExists('author');
});

你可以传递一个函数作为附加参数,来断言过滤器是否通过了给定的“真值测试”。这对于断言过滤器是否具有特定配置非常有用:

use function Pest\Livewire\livewire;
use Filament\Tables\Filters\SelectFilter;

it('has an author filter', function () {    
    livewire(PostResource\Pages\ListPosts::class)
        ->assertTableFilterExists('author', function (SelectFilter $column): bool {
            return $column->getLabel() === 'Select author';
        });
});

测试汇总

要测试汇总计算是否有效,你可以使用 assertTableColumnSummarySet() 方法:

use function Pest\Livewire\livewire;

it('can average values in a column', function () {
    $posts = Post::factory()->count(10)->create();

    livewire(PostResource\Pages\ListPosts::class)
        ->assertCanSeeTableRecords($posts)
        ->assertTableColumnSummarySet('rating', 'average', $posts->avg('rating'));
});

第一个参数是列名,第二个参数是汇总器 ID,第三个参数是预期值。

请注意,预期值和实际值已进行规范化,因此 123.12 被视为与 "123.12" 相同,而 ['Fred', 'Jim'] 被视为与 ['Jim', 'Fred'] 相同。

你可以通过将汇总器 ID 传递给 make() 方法来设置它:

use Filament\Tables\Columns\Summarizers\Average;
use Filament\Tables\Columns\TextColumn;

TextColumn::make('rating')
    ->summarize(Average::make('average'))

该 ID 在该列的各个汇总器之间应保持唯一。

仅在一个分页页面上测试汇总

要仅计算一个分页页面的平均值,请使用 isCurrentPaginationPageOnly 参数:

use function Pest\Livewire\livewire;

it('can average values in a column', function () {
    $posts = Post::factory()->count(20)->create();

    livewire(PostResource\Pages\ListPosts::class)
        ->assertCanSeeTableRecords($posts->take(10))
        ->assertTableColumnSummarySet('rating', 'average', $posts->take(10)->avg('rating'), isCurrentPaginationPageOnly: true);
});

测试范围汇总器

要测试范围,请将最小值和最大值传入元组式的 [$minimum, $maximum] 数组:

use function Pest\Livewire\livewire;

it('can average values in a column', function () {
    $posts = Post::factory()->count(10)->create();

    livewire(PostResource\Pages\ListPosts::class)
        ->assertCanSeeTableRecords($posts)
        ->assertTableColumnSummarySet('rating', 'range', [$posts->min('rating'), $posts->max('rating')]);
});
Edit on GitHub

Still need help? Join our Discord community or open a GitHub discussion

Previous
测试资源