diff options
| author | Sam Light <samlight1994@gmail.com> | 2026-07-27 22:28:18 +0100 |
|---|---|---|
| committer | Sam Light <samlight1994@gmail.com> | 2026-07-27 22:28:18 +0100 |
| commit | 1dc1088933c906d4676056d53395d34ce0a68d01 (patch) | |
| tree | b3c39b1e58305e2b0985678b720991883372667f /tests/Feature/TableTest.php | |
| parent | 827a53363e7a03329d6e5972d9d9be99d17c889e (diff) | |
Diffstat (limited to 'tests/Feature/TableTest.php')
| -rw-r--r-- | tests/Feature/TableTest.php | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/tests/Feature/TableTest.php b/tests/Feature/TableTest.php index 8dc327c..8c34095 100644 --- a/tests/Feature/TableTest.php +++ b/tests/Feature/TableTest.php @@ -2,6 +2,7 @@ use Livewire\Livewire; use Workbench\App\Livewire\ProductsTable; +use Workbench\App\Models\Product; describe('products table', function () { it('does render', function () { @@ -13,3 +14,90 @@ describe('products table', function () { ->assertSeeHtml('<td'); }); }); + +describe('products table ordering', function () { + it('cycles a column through ascending, descending and unordered', function () { + Livewire::test(ProductsTable::class) + ->call('orderBy', 'name') + ->assertSet('order', 'name') + ->assertSet('orderDirection', 'asc') + ->call('orderBy', 'name') + ->assertSet('order', 'name') + ->assertSet('orderDirection', 'desc') + ->call('orderBy', 'name') + ->assertSet('order', null) + ->assertSet('orderDirection', null); + }); + + it('resets to ascending when a different column is clicked', function () { + Livewire::test(ProductsTable::class) + ->call('orderBy', 'name') + ->call('orderBy', 'name') + ->assertSet('orderDirection', 'desc') + ->call('orderBy', 'price') + ->assertSet('order', 'price') + ->assertSet('orderDirection', 'asc'); + }); + + it('exposes the direction as a SortDirection', function () { + $component = Livewire::test(ProductsTable::class); + + expect($component->instance()->getOrderDirection())->toBeNull(); + + $component->call('orderBy', 'name'); + expect($component->instance()->getOrderDirection())->toBe(SortDirection::Ascending); + + $component->call('orderBy', 'name'); + expect($component->instance()->getOrderDirection())->toBe(SortDirection::Descending); + }); + + it('marks the ordered column in the header', function () { + Livewire::test(ProductsTable::class) + ->assertDontSeeHtml('ordered-asc') + ->assertDontSeeHtml('ordered-desc') + ->call('orderBy', 'name') + ->assertSeeHtml('ordered-asc') + ->assertDontSeeHtml('ordered-desc') + ->call('orderBy', 'name') + ->assertSeeHtml('ordered-desc') + ->assertDontSeeHtml('ordered-asc'); + }); + + it('applies the direction to the query', function () { + $lowest = Product::orderBy('price')->orderBy('id')->value('id'); + $highest = Product::orderByDesc('price')->orderByDesc('id')->value('id'); + + expect($lowest)->not->toBe($highest); + + Livewire::test(ProductsTable::class) + // A single row of a single column, so the only <td> is the ordered id. + ->set('activeColumns', ['id']) + ->set('pageSize', 1) + ->call('orderBy', 'price') + ->assertSeeHtml(">{$lowest}</td>") + ->assertDontSeeHtml(">{$highest}</td>") + ->call('orderBy', 'price') + ->assertSeeHtml(">{$highest}</td>") + ->assertDontSeeHtml(">{$lowest}</td>"); + }); + + it('restores the order from the query string', function () { + $highest = Product::orderByDesc('price')->orderByDesc('id')->value('id'); + + Livewire::withQueryParams(['order' => 'price', 'orderDirection' => 'desc']) + ->test(ProductsTable::class) + ->assertSet('orderDirection', 'desc') + ->assertSeeHtml('ordered-desc') + ->set('activeColumns', ['id']) + ->set('pageSize', 1) + ->assertSeeHtml(">{$highest}</td>"); + }); + + it('ignores an unrecognised direction in the query string', function () { + Livewire::withQueryParams(['order' => 'price', 'orderDirection' => 'nonsense']) + ->test(ProductsTable::class) + ->assertOk() + ->assertDontSeeHtml('ordered-asc') + ->assertDontSeeHtml('ordered-desc'); + }); +}); |
