From 1dc1088933c906d4676056d53395d34ce0a68d01 Mon Sep 17 00:00:00 2001 From: Sam Light Date: Mon, 27 Jul 2026 22:28:18 +0100 Subject: changed to use sort direction --- tests/Feature/TableTest.php | 88 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) (limited to 'tests/Feature') 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('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 is the ordered id. + ->set('activeColumns', ['id']) + ->set('pageSize', 1) + ->call('orderBy', 'price') + ->assertSeeHtml(">{$lowest}") + ->assertDontSeeHtml(">{$highest}") + ->call('orderBy', 'price') + ->assertSeeHtml(">{$highest}") + ->assertDontSeeHtml(">{$lowest}"); + }); + + 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}"); + }); + + 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'); + }); +}); -- cgit v1.2.3