mirror of
https://github.com/filamentphp/filament.git
synced 2026-09-24 15:42:09 +08:00
Update 03-testing-tables.md - "Testing that a column can be sorted" (#17871)
* Issue #17817: Update 03-testing-tables.md - "Testing that a column can be sorted" Currently, the documentation includes a flaky test as an example for sorting a column. This is because Filament sorts its table records according to the underlying database strategy. Previously, the documentation suggested using the `sortBy()` method on the collection returned by the factory. However, this approach does not account for differences in how various databases handle sorting, which can lead to inconsistent test results. With this commit, the documentation has been updated. The example test now uses a stable approach that explicitly orders records through the database query, ensuring reliable behavior across different database systems. Signed-off-by: Merdin <me@merdin.dev> * Update 03-testing-tables.md - "Testing that a column can be sorted" Improve query Signed-off-by: Merdin <me@merdin.dev> * Update 03-testing-tables.md Change `orderBy('title', 'desc')` to `orderByDesc('title')` to improve code readability. Signed-off-by: Merdin <me@merdin.dev> * Update 03-testing-tables.md Signed-off-by: Merdin <me@merdin.dev> * Refactor post sorting in test to use query builder Signed-off-by: Dan Harrin <git@danharrin.com> * Fix sorting order for posts by title Signed-off-by: Dan Harrin <git@danharrin.com> --------- Signed-off-by: Merdin <me@merdin.dev> Signed-off-by: Dan Harrin <git@danharrin.com> Co-authored-by: Dan Harrin <git@danharrin.com>
This commit is contained in:
@@ -115,16 +115,23 @@ Once the table is sorted, you can ensure that the table records are rendered in
|
||||
use function Pest\Livewire\livewire;
|
||||
|
||||
it('can sort posts by title', function () {
|
||||
$posts = Post::factory()->count(10)->create();
|
||||
Post::factory()->count(10)->create();
|
||||
|
||||
$sortedPostsAsc = Post::query()->orderBy('title')->get();
|
||||
$sortedPostsDesc = Post::query()->orderBy('title', 'desc')->get();
|
||||
|
||||
livewire(PostResource\Pages\ListPosts::class)
|
||||
->sortTable('title')
|
||||
->assertCanSeeTableRecords($posts->sortBy('title'), inOrder: true)
|
||||
->assertCanSeeTableRecords($sortedPostsAsc, inOrder: true)
|
||||
->sortTable('title', 'desc')
|
||||
->assertCanSeeTableRecords($posts->sortByDesc('title'), inOrder: true);
|
||||
->assertCanSeeTableRecords($sortedPostsDesc, inOrder: true);
|
||||
});
|
||||
```
|
||||
|
||||
<Aside variant="info">
|
||||
Filament tables use a SQL `order` statement to sort records before they are output. Different database drivers can use different sorting strategies, and they can differ from PHP's own sorting strategy, so you should ensure that test records are sorted using `orderBy()` on a database query rather than `sortBy()` on a collection of models.
|
||||
</Aside>
|
||||
|
||||
### Testing the state of a column
|
||||
|
||||
To assert that a certain column has a state or does not have a state for a record you can use `assertTableColumnStateSet()` and `assertTableColumnStateNotSet()`:
|
||||
|
||||
Reference in New Issue
Block a user