Repository navigation
Expand file tree
/
Copy pathWithSortableTest.php
More file actions
153 lines (109 loc) · 4.38 KB
/
Copy pathWithSortableTest.php
File metadata and controls
153 lines (109 loc) · 4.38 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
<?php
declare(strict_types=1);
namespace NyonCode\WireSortable\Tests\Unit;
use NyonCode\WireSortable\Concerns\WithSortable;
use NyonCode\WireSortable\SortableTable;
use NyonCode\WireTable\Columns\TextColumn;
use NyonCode\WireTable\Table;
use ReflectionMethod;
// Minimal stub to test the trait in isolation
class WithSortableTestStub
{
use WithSortable;
private SortableTable $table;
public mixed $cachedRecords = null;
protected string $wireTableClass = Table::class;
protected ?Table $tableInstance = null;
public function __construct(SortableTable $table)
{
$this->table = $table;
}
public function getTable(): Table
{
return $this->table;
}
}
// ── Column Reorder ──────────────────────────────────────
it('returns columns in default order when no custom order set', function () {
$table = SortableTable::make()
->columnReorderable()
->columns([
TextColumn::make('name'),
TextColumn::make('email'),
TextColumn::make('age'),
]);
$stub = new WithSortableTestStub($table);
$names = array_map(fn ($c) => $c->getName(), $stub->getReorderableColumns());
expect($names)->toBe(['name', 'email', 'age']);
});
it('returns columns in custom order', function () {
$table = SortableTable::make()
->columnReorderable()
->columns([
TextColumn::make('name'),
TextColumn::make('email'),
TextColumn::make('age'),
]);
$stub = new WithSortableTestStub($table);
$stub->reorderableColumnOrder = ['age', 'name', 'email'];
$names = array_map(fn ($c) => $c->getName(), $stub->getReorderableColumns());
expect($names)->toBe(['age', 'name', 'email']);
});
it('appends new columns not in saved order', function () {
$table = SortableTable::make()
->columnReorderable()
->columns([
TextColumn::make('name'),
TextColumn::make('email'),
TextColumn::make('age'),
TextColumn::make('phone'),
]);
$stub = new WithSortableTestStub($table);
$stub->reorderableColumnOrder = ['email', 'name'];
$names = array_map(fn ($c) => $c->getName(), $stub->getReorderableColumns());
expect($names)->toBe(['email', 'name', 'age', 'phone']);
});
it('ignores removed columns in saved order', function () {
$table = SortableTable::make()
->columnReorderable()
->columns([
TextColumn::make('name'),
TextColumn::make('email'),
]);
$stub = new WithSortableTestStub($table);
$stub->reorderableColumnOrder = ['deleted_column', 'email', 'name'];
$names = array_map(fn ($c) => $c->getName(), $stub->getReorderableColumns());
expect($names)->toBe(['email', 'name']);
});
// ── Row Reorder Toggle ──────────────────────────────────
it('is not reordering by default', function () {
$table = SortableTable::make()->reorderable();
$stub = new WithSortableTestStub($table);
expect($stub->isTableReordering())->toBeFalse();
});
it('toggles reorder mode', function () {
$table = SortableTable::make()->reorderable();
$stub = new WithSortableTestStub($table);
$stub->toggleReordering();
expect($stub->isTableReordering())->toBeTrue();
$stub->toggleReordering();
expect($stub->isTableReordering())->toBeFalse();
});
it('does not toggle when table is not reorderable', function () {
$table = SortableTable::make(); // not reorderable
$stub = new WithSortableTestStub($table);
$stub->toggleReordering();
expect($stub->isTableReordering())->toBeFalse();
});
// ── Hooks ────────────────────────────────────────
it('interceptTableRecords returns null when not in reorder mode', function () {
$table = SortableTable::make()->reorderable();
$stub = new WithSortableTestStub($table);
$reflection = new ReflectionMethod($stub, 'interceptTableRecords');
$result = $reflection->invoke($stub);
expect($result)->toBeNull();
});
it('has interceptTableRecords method for WithTable hook', function () {
$stub = new WithSortableTestStub(SortableTable::make());
expect(method_exists($stub, 'interceptTableRecords'))->toBeTrue();
});