-
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathLivewireBoardTest.php
More file actions
369 lines (287 loc) · 15 KB
/
LivewireBoardTest.php
File metadata and controls
369 lines (287 loc) · 15 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
declare(strict_types=1);
use Livewire\Livewire;
use Relaticle\Flowforge\Services\DecimalPosition;
use Relaticle\Flowforge\Tests\Fixtures\Task;
use Relaticle\Flowforge\Tests\Fixtures\TestBoard;
describe('board rendering', function () {
test('renders board with all columns', function () {
Livewire::test(TestBoard::class)
->assertStatus(200)
->assertSee('To Do')
->assertSee('In Progress')
->assertSee('Completed')
->assertDontSee('Hidden Column');
});
test('renders board with unhidden column', function () {
Livewire::test(TestBoard::class)
->assertStatus(200)
->assertDontSee('Hidden Column')
->set('showAllColumns', true)
->assertSee('Hidden Column');
});
test('re-hides column when visibility condition flips back', function () {
Livewire::test(TestBoard::class)
->assertStatus(200)
->assertDontSee('Hidden Column')
->set('showAllColumns', true)
->assertSee('Hidden Column')
->set('showAllColumns', false)
->assertDontSee('Hidden Column');
});
test('column configured with hidden(true) literal never renders', function () {
Livewire::test(TestBoard::class)
->assertStatus(200)
->assertDontSee('Archived Column')
->set('showAllColumns', true)
->assertDontSee('Archived Column');
});
test('getColumnIdentifiers excludes hidden columns', function () {
$board = Livewire::test(TestBoard::class)->instance()->getBoard();
expect($board->getColumnIdentifiers())
->not->toContain('hidden_column')
->not->toContain('archived')
->toContain('todo', 'in_progress', 'completed');
});
test('getColumnLabels excludes hidden columns', function () {
$board = Livewire::test(TestBoard::class)->instance()->getBoard();
expect(array_keys($board->getColumnLabels()))
->not->toContain('hidden_column')
->not->toContain('archived')
->toContain('todo', 'in_progress', 'completed');
});
test('getColumnColors excludes hidden columns', function () {
$board = Livewire::test(TestBoard::class)->instance()->getBoard();
expect(array_keys($board->getColumnColors()))
->not->toContain('hidden_column')
->not->toContain('archived');
});
test('getColumns returns a sequentially-indexed array after filtering', function () {
$board = Livewire::test(TestBoard::class)->instance()->getBoard();
$columns = $board->getColumns();
expect(array_keys($columns))->toBe(range(0, count($columns) - 1));
});
test('getColumnIdentifiers returns empty when every column is hidden', function () {
$board = Livewire::test(TestBoard::class)
->set('hideEverything', true)
->instance()
->getBoard();
expect($board->getColumns())->toBe([])
->and($board->getColumnIdentifiers())->toBe([])
->and($board->getColumnLabels())->toBe([])
->and($board->getColumnColors())->toBe([]);
});
test('displays cards in correct columns', function () {
Task::factory()->todo()->create(['title' => 'Task in Todo']);
Task::factory()->inProgress()->create(['title' => 'Task in Progress']);
Task::factory()->completed()->create(['title' => 'Task Completed']);
Livewire::test(TestBoard::class)
->assertSee('Task in Todo')
->assertSee('Task in Progress')
->assertSee('Task Completed');
});
test('shows empty state for empty columns', function () {
// Create task only in one column
Task::factory()->todo()->create(['title' => 'Only Task']);
$component = Livewire::test(TestBoard::class);
// Should see the task in todo
$component->assertSee('Only Task');
// Board should still render all columns
$component->assertSee('To Do')
->assertSee('In Progress')
->assertSee('Completed');
});
});
describe('card movement', function () {
test('moves card to different column', function () {
$task = Task::factory()->todo()->withPosition('65535.0000000000')->create();
Livewire::test(TestBoard::class)
->call('moveCard', (string) $task->id, 'in_progress', null, null)
->assertDispatched('kanban-card-moved');
expect($task->fresh()->status)->toBe('in_progress');
});
test('moves card to top of column', function () {
$existingTask = Task::factory()->inProgress()->withPosition('65535.0000000000')->create();
$taskToMove = Task::factory()->todo()->withPosition('65535.0000000000')->create();
Livewire::test(TestBoard::class)
->call('moveCard', (string) $taskToMove->id, 'in_progress', null, (string) $existingTask->id);
$movedTask = $taskToMove->fresh();
expect($movedTask->status)->toBe('in_progress')
->and((float) $movedTask->order_position)->toBeLessThan(65535);
});
test('dragging last card to top places it above first card (regression for #110)', function () {
$first = Task::factory()->inProgress()->withPosition('65535.0000000000')->create(['title' => 'First']);
$second = Task::factory()->inProgress()->withPosition('131070.0000000000')->create(['title' => 'Second']);
$third = Task::factory()->inProgress()->withPosition('196605.0000000000')->create(['title' => 'Third']);
$last = Task::factory()->inProgress()->withPosition('262140.0000000000')->create(['title' => 'Last']);
Livewire::test(TestBoard::class)
->call('moveCard', (string) $last->id, 'in_progress', null, (string) $first->id);
$movedTask = $last->fresh();
expect((float) $movedTask->order_position)->toBeLessThan((float) $first->order_position);
$orderedIds = Task::where('status', 'in_progress')
->orderBy('order_position')
->pluck('id')
->map(fn ($id) => (string) $id)
->toArray();
expect($orderedIds[0])->toBe((string) $last->id)
->and($orderedIds[1])->toBe((string) $first->id)
->and($orderedIds[2])->toBe((string) $second->id)
->and($orderedIds[3])->toBe((string) $third->id);
});
test('moves card to bottom of column', function () {
$existingTask = Task::factory()->inProgress()->withPosition('65535.0000000000')->create();
$taskToMove = Task::factory()->todo()->withPosition('65535.0000000000')->create();
Livewire::test(TestBoard::class)
->call('moveCard', (string) $taskToMove->id, 'in_progress', (string) $existingTask->id, null);
$movedTask = $taskToMove->fresh();
expect($movedTask->status)->toBe('in_progress')
->and((float) $movedTask->order_position)->toBeGreaterThan(65535);
});
test('moves card between two existing cards', function () {
$task1 = Task::factory()->inProgress()->withPosition('65535.0000000000')->create();
$task2 = Task::factory()->inProgress()->withPosition('131070.0000000000')->create();
$taskToMove = Task::factory()->todo()->withPosition('65535.0000000000')->create();
Livewire::test(TestBoard::class)
->call('moveCard', (string) $taskToMove->id, 'in_progress', (string) $task1->id, (string) $task2->id);
$movedTask = $taskToMove->fresh();
expect($movedTask->status)->toBe('in_progress')
->and((float) $movedTask->order_position)->toBeGreaterThan(65535)
->and((float) $movedTask->order_position)->toBeLessThan(131070);
});
test('dispatches kanban-card-moved event after move', function () {
$task = Task::factory()->todo()->withPosition('65535.0000000000')->create();
Livewire::test(TestBoard::class)
->call('moveCard', (string) $task->id, 'completed', null, null)
->assertDispatched('kanban-card-moved');
// Verify the move actually happened
expect($task->fresh()->status)->toBe('completed');
});
test('updates position in database after move', function () {
$task = Task::factory()->todo()->withPosition('65535.0000000000')->create();
$originalPosition = $task->order_position;
Livewire::test(TestBoard::class)
->call('moveCard', (string) $task->id, 'in_progress', null, null);
$movedTask = $task->fresh();
expect($movedTask->status)->toBe('in_progress')
->and($movedTask->order_position)->not->toBe($originalPosition);
});
});
describe('move to empty column', function () {
test('handles card moved to empty column', function () {
$task = Task::factory()->todo()->withPosition('65535.0000000000')->create();
// Move to completed column which is empty
Livewire::test(TestBoard::class)
->call('moveCard', (string) $task->id, 'completed', null, null)
->assertDispatched('kanban-card-moved');
$movedTask = $task->fresh();
expect($movedTask->status)->toBe('completed')
->and((float) $movedTask->order_position)->toBe((float) DecimalPosition::DEFAULT_GAP);
});
test('first card in empty column gets default position', function () {
$task = Task::factory()->inProgress()->withPosition('100.0000000000')->create();
Livewire::test(TestBoard::class)
->call('moveCard', (string) $task->id, 'todo', null, null);
expect((float) $task->fresh()->order_position)->toBe((float) DecimalPosition::DEFAULT_GAP);
});
});
describe('concurrent card movement', function () {
test('handles very close positions by inserting between', function () {
// Create two tasks with very close positions (but different due to unique constraint)
$task1 = Task::factory()->inProgress()->withPosition('65535.0000000000')->create();
$task2 = Task::factory()->inProgress()->withPosition('65536.0000000000')->create();
$taskToMove = Task::factory()->todo()->withPosition('10000.0000000000')->create();
// Move between two cards with close positions - should work
Livewire::test(TestBoard::class)
->call('moveCard', (string) $taskToMove->id, 'in_progress', (string) $task1->id, (string) $task2->id)
->assertDispatched('kanban-card-moved');
// Task should be moved and positioned between the two
$movedTask = $taskToMove->fresh();
expect($movedTask->status)->toBe('in_progress')
->and((float) $movedTask->order_position)->toBeGreaterThan(65535)
->and((float) $movedTask->order_position)->toBeLessThan(65536);
});
test('triggers auto-rebalancing when gap too small', function () {
// Create cards with very small gap (below MIN_GAP threshold)
$task1 = Task::factory()->inProgress()->withPosition('1000.0000000001')->create();
$task2 = Task::factory()->inProgress()->withPosition('1000.0000000002')->create();
$taskToMove = Task::factory()->todo()->withPosition('65535.0000000000')->create();
Livewire::test(TestBoard::class)
->call('moveCard', (string) $taskToMove->id, 'in_progress', (string) $task1->id, (string) $task2->id);
// After rebalancing, positions should be evenly distributed with large gaps
$positions = Task::where('status', 'in_progress')
->orderBy('order_position')
->pluck('order_position')
->map(fn ($p) => (float) $p)
->values();
// Check that gaps are now reasonable (after rebalancing)
if ($positions->count() >= 2) {
$gap = $positions[1] - $positions[0];
expect($gap)->toBeGreaterThan(1000); // Rebalanced with large gaps
}
});
});
describe('pagination', function () {
test('loads more items on demand', function () {
// Create many tasks in one column
Task::factory(30)->todo()->create();
$component = Livewire::test(TestBoard::class);
// Call loadMoreItems
$component->call('loadMoreItems', 'todo', 20)
->assertDispatched('kanban-items-loaded');
});
test('dispatches kanban-items-loaded event after loading more', function () {
Task::factory(30)->todo()->create();
Livewire::test(TestBoard::class)
->call('loadMoreItems', 'todo', 10)
->assertDispatched('kanban-items-loaded');
});
test('loads all items when requested', function () {
Task::factory(50)->todo()->create();
Livewire::test(TestBoard::class)
->call('loadAllItems', 'todo')
->assertDispatched('kanban-all-items-loaded');
});
test('tracks fully loaded state for small columns', function () {
// Create only 5 tasks - should be fully loaded by default
Task::factory(5)->todo()->create();
$component = Livewire::test(TestBoard::class);
// With only 5 tasks and default limit of 20, column should be fully loaded
expect($component->instance()->isColumnFullyLoaded('todo'))->toBeTrue();
});
});
describe('edge cases', function () {
test('handles multiple consecutive moves', function () {
$task = Task::factory()->todo()->withPosition('65535.0000000000')->create();
$component = Livewire::test(TestBoard::class);
// Move card multiple times
$component->call('moveCard', (string) $task->id, 'in_progress', null, null);
expect($task->fresh()->status)->toBe('in_progress');
$component->call('moveCard', (string) $task->id, 'completed', null, null);
expect($task->fresh()->status)->toBe('completed');
$component->call('moveCard', (string) $task->id, 'todo', null, null);
expect($task->fresh()->status)->toBe('todo');
});
test('maintains correct order after multiple moves', function () {
// Create 3 tasks in order
$task1 = Task::factory()->todo()->withPosition('65535.0000000000')->create(['title' => 'First']);
$task2 = Task::factory()->todo()->withPosition('131070.0000000000')->create(['title' => 'Second']);
$task3 = Task::factory()->todo()->withPosition('196605.0000000000')->create(['title' => 'Third']);
$component = Livewire::test(TestBoard::class);
// Move task3 to top (before task1)
$component->call('moveCard', (string) $task3->id, 'todo', null, (string) $task1->id);
// Verify order
$orderedTasks = Task::where('status', 'todo')
->orderBy('order_position')
->pluck('title')
->toArray();
expect($orderedTasks[0])->toBe('Third'); // Now first
});
test('handles moving last card from column', function () {
$task = Task::factory()->todo()->withPosition('65535.0000000000')->create();
Livewire::test(TestBoard::class)
->call('moveCard', (string) $task->id, 'completed', null, null);
// Todo column should now be empty
expect(Task::where('status', 'todo')->count())->toBe(0);
expect(Task::where('status', 'completed')->count())->toBe(1);
});
});