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 | |
| parent | 827a53363e7a03329d6e5972d9d9be99d17c889e (diff) | |
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/Feature/TableTest.php | 88 | ||||
| -rw-r--r-- | tests/Unit/SortDirectionsTest.php | 30 |
2 files changed, 118 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'); + }); +}); diff --git a/tests/Unit/SortDirectionsTest.php b/tests/Unit/SortDirectionsTest.php new file mode 100644 index 0000000..c63d5bb --- /dev/null +++ b/tests/Unit/SortDirectionsTest.php @@ -0,0 +1,30 @@ +<?php + +use Lightscale\LaralightTables\Support\SortDirections; + +describe('SortDirections', function () { + it('converts the enum to a query builder direction', function () { + expect(SortDirections::value(SortDirection::Ascending))->toBe('asc'); + expect(SortDirections::value(SortDirection::Descending))->toBe('desc'); + }); + + it('resolves strings to the enum', function () { + expect(SortDirections::tryFrom('asc'))->toBe(SortDirection::Ascending); + expect(SortDirections::tryFrom('desc'))->toBe(SortDirection::Descending); + }); + + it('resolves strings case insensitively', function () { + expect(SortDirections::tryFrom('ASC'))->toBe(SortDirection::Ascending); + expect(SortDirections::tryFrom('Desc'))->toBe(SortDirection::Descending); + }); + + it('passes an enum straight through', function () { + expect(SortDirections::tryFrom(SortDirection::Descending))->toBe(SortDirection::Descending); + }); + + it('resolves null and unrecognised values to null', function () { + expect(SortDirections::tryFrom(null))->toBeNull(); + expect(SortDirections::tryFrom(''))->toBeNull(); + expect(SortDirections::tryFrom('nonsense'))->toBeNull(); + }); +}); |
