summaryrefslogtreecommitdiff
path: root/tests/Feature/TableTest.php
blob: 8c3409568e2194c9fac322cbc0882e8f08605316 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php

use Livewire\Livewire;
use Workbench\App\Livewire\ProductsTable;
use Workbench\App\Models\Product;

describe('products table', function () {
    it('does render', function () {
        Livewire::test(ProductsTable::class)
            ->assertSeeHtml('<table')
            ->assertSeeHtml('<thead>')
            ->assertSeeHtml('<tbody>')
            ->assertSeeHtml('<th')
            ->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');
    });
});