Repository navigation
Expand file tree
/
Copy pathTableTestingMacros.php
More file actions
177 lines (143 loc) · 6.46 KB
/
Copy pathTableTestingMacros.php
File metadata and controls
177 lines (143 loc) · 6.46 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
declare(strict_types=1);
namespace NyonCode\WireTable\Testing;
use Illuminate\Contracts\Pagination\CursorPaginator;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Livewire\Component;
use Livewire\Features\SupportTesting\Testable;
use PHPUnit\Framework\Assert;
/**
* Pest / PHPUnit helpers on `Livewire::test()` for a component using
* `WithTable` — so a test says what it means ("search, then see these
* records") instead of the state path it happens to live under.
*
* Every helper drives the component through the same endpoints the browser
* does (a `tableState` write, `sortTable()`, `executeTableAction()` …), so a
* passing test is evidence about the real request, not about a shortcut.
*
* Records are compared by key and may be given as models, keys, or a mix.
* "Can see" means on the page the table would render now — the page size and
* the current page apply, as they do for a user.
*/
final class TableTestingMacros
{
public static function register(): void
{
Testable::macro('searchTable', function (?string $term): Testable {
/** @var Testable<Component> $this */
return $this->set('tableState.search', $term);
});
Testable::macro('sortTable', function (string $column, ?string $direction = null): Testable {
/** @var Testable<Component> $this */
$this->call('sortTable', $column);
// sortTable() toggles; a requested direction is reached in at most one more call.
if ($direction !== null && $this->get('tableState.sort.direction') !== $direction) {
$this->call('sortTable', $column);
}
return $this;
});
Testable::macro('filterTable', function (string $filter, mixed $value): Testable {
/** @var Testable<Component> $this */
return $this->set('tableState.filters.'.$filter, $value);
});
Testable::macro('resetTableFilters', function (): Testable {
/** @var Testable<Component> $this */
return $this->call('resetTableFilters');
});
Testable::macro('selectTableRecords', function (iterable $records): Testable {
/** @var Testable<Component> $testable */
$testable = $this;
foreach (TableTestingMacros::keys($records) as $key) {
$testable->call('toggleRecordSelection', $key);
}
return $testable;
});
Testable::macro('callTableAction', function (string $action, mixed $record, array $data = []): Testable {
/** @var Testable<Component> $this */
$key = TableTestingMacros::keys([$record])[0];
return $data === []
? $this->call('executeTableAction', $key, $action, true)
: $this->call('executeTableActionWithData', $key, $action, $data, true);
});
Testable::macro('callTableBulkAction', function (string $action, iterable $records, array $data = []): Testable {
/** @var Testable<Component> $this */
$this->selectTableRecords($records);
return $this->call('executeBulkActionWithData', $action, $data, true);
});
Testable::macro('assertCanSeeTableRecords', function (iterable $records, bool $inOrder = false): Testable {
/** @var Testable<Component> $this */
$expected = TableTestingMacros::keys($records);
$shown = TableTestingMacros::shownKeys($this);
foreach ($expected as $key) {
Assert::assertContains($key, $shown, "Record [{$key}] is not on the table's current page.");
}
if ($inOrder) {
Assert::assertSame($expected, array_values(array_intersect($shown, $expected)), 'The records are on the page, but not in the expected order.');
}
return $this;
});
Testable::macro('assertCanNotSeeTableRecords', function (iterable $records): Testable {
/** @var Testable<Component> $this */
$shown = TableTestingMacros::shownKeys($this);
foreach (TableTestingMacros::keys($records) as $key) {
Assert::assertNotContains($key, $shown, "Record [{$key}] is on the table's current page.");
}
return $this;
});
Testable::macro('assertCountTableRecords', function (int $count): Testable {
/** @var Testable<Component> $this */
$records = TableTestingMacros::records($this);
$total = $records instanceof LengthAwarePaginator ? $records->total() : count(TableTestingMacros::items($records));
Assert::assertSame($count, $total, 'The table matches a different number of records.');
return $this;
});
}
/**
* Record keys as the table spells them — strings, the way they travel.
*
* @param iterable<mixed> $records
* @return list<string>
*/
public static function keys(iterable $records): array
{
$keys = [];
foreach ($records as $record) {
$keys[] = (string) ($record instanceof Model ? $record->getKey() : $record);
}
return $keys;
}
/**
* The keys on the page the table would render now.
*
* @param Testable<Component> $testable
* @return list<string>
*/
public static function shownKeys(Testable $testable): array
{
return array_values(array_map(
static fn (mixed $record): string => (string) ($record instanceof Model ? $record->getKey() : Arr::get((array) $record, 'id')),
self::items(self::records($testable)),
));
}
/** @param Testable<Component> $testable */
public static function records(Testable $testable): mixed
{
$component = $testable->instance();
if (! method_exists($component, 'getTableRecords')) {
Assert::fail($component::class.' is not a table: it does not use WithTable.');
}
return $component->getTableRecords();
}
/** @return array<int, mixed> */
public static function items(mixed $records): array
{
return match (true) {
$records instanceof Paginator, $records instanceof CursorPaginator => array_values($records->items()),
$records instanceof \Traversable => array_values(iterator_to_array($records)),
default => [],
};
}
}