-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.test.ts
673 lines (599 loc) · 22.8 KB
/
index.test.ts
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
import { describe, it, expect } from 'vitest';
import { DataTable, type ColumnDef } from '$lib/DataTable.svelte.js';
describe('DataTable', () => {
const sampleData = [
{ id: 1, name: 'Alice', age: 30 },
{ id: 2, name: 'Bob', age: 25 },
{ id: 3, name: 'Charlie', age: 35 },
{ id: 4, name: 'David', age: 28 },
{ id: 5, name: 'Eve', age: 32 }
];
const columns = [
{ id: 'id', key: 'id', name: 'ID', sortable: true },
{ id: 'name', key: 'name', name: 'Name', sortable: true },
{ id: 'age', key: 'age', name: 'Age', sortable: true },
{
id: 'ageGroup',
key: 'age',
name: 'Age Group',
sortable: true,
getValue: (row) => (row.age < 30 ? 'Young' : 'Adult')
}
] satisfies ColumnDef<(typeof sampleData)[0]>[];
describe('Initialization', () => {
it('should initialize with default settings', () => {
const table = new DataTable({ data: sampleData, columns });
expect(table.rows).toHaveLength(5);
expect(table.currentPage).toBe(1);
expect(table.totalPages).toBe(1);
});
it('should initialize with custom page size', () => {
const table = new DataTable({ data: sampleData, columns, pageSize: 2 });
expect(table.rows).toHaveLength(2);
expect(table.totalPages).toBe(3);
});
it('should initialize with initial sort', () => {
const table = new DataTable({
data: sampleData,
columns,
initialSort: 'age',
initialSortDirection: 'desc'
});
expect(table.rows[0].age).toBe(35);
});
it('should initialize with initial filters', () => {
const table = new DataTable({
data: sampleData,
columns,
initialFilters: { age: [30, 35] }
});
expect(table.rows).toHaveLength(2);
expect(table.rows.every((row) => row.age === 30 || row.age === 35)).toBe(true);
});
it('should handle empty data set', () => {
const table = new DataTable({ data: [], columns });
expect(table.rows).toHaveLength(0);
expect(table.totalPages).toBe(1);
});
it('should handle data with missing properties', () => {
const incompleteData = [
{ id: 1, name: 'Alice' },
{ id: 2, age: 25 }
] as any[];
const table = new DataTable({ data: incompleteData, columns });
expect(table.rows).toHaveLength(2);
expect(table.rows[0].age).toBeUndefined();
expect(table.rows[1].name).toBeUndefined();
});
it('should handle multiple columns with the same key', () => {
const table = new DataTable({ data: sampleData, columns });
expect(table.columns).toHaveLength(4);
expect(table.columns.filter((col) => col.key === 'age')).toHaveLength(2);
});
});
describe('Sorting', () => {
it('should sort ascending', () => {
const table = new DataTable({ data: sampleData, columns });
table.toggleSort('age');
expect(table.rows[0].age).toBe(25);
expect(table.rows[4].age).toBe(35);
});
it('should sort descending', () => {
const table = new DataTable({ data: sampleData, columns });
table.toggleSort('age');
table.toggleSort('age');
expect(table.rows[0].age).toBe(35);
expect(table.rows[4].age).toBe(25);
});
it('should clear sort', () => {
const table = new DataTable({ data: sampleData, columns });
table.toggleSort('age');
table.toggleSort('age');
table.toggleSort('age');
expect(table.getSortState('age')).toBeNull();
});
it('should not sort unsortable columns', () => {
const unsortableColumns = columns.map((col) => ({ ...col, sortable: false }));
const table = new DataTable({ data: sampleData, columns: unsortableColumns });
table.toggleSort('age');
expect(table.getSortState('age')).toBeNull();
});
it('should maintain sort state when applying filters', () => {
const table = new DataTable({ data: sampleData, columns });
table.toggleSort('age');
table.setFilter('name', ['Alice', 'Bob', 'Charlie']);
expect(table.rows[0].name).toBe('Bob');
expect(table.rows[2].name).toBe('Charlie');
});
it('should handle repeated toggling of sort', () => {
const table = new DataTable({ data: sampleData, columns });
for (let i = 0; i < 10; i++) {
table.toggleSort('age');
}
// After 10 toggles, we should be back to 'asc'
expect(table.getSortState('age')).toBe('asc');
});
it('should handle sorting on column with all null values', () => {
const nullData = [
{ id: 1, name: 'Alice', age: null },
{ id: 2, name: 'Bob', age: null },
{ id: 3, name: 'Charlie', age: null }
] as any[];
const table = new DataTable({ data: nullData, columns });
table.toggleSort('age');
expect(table.rows).toHaveLength(3);
// The order should remain unchanged
expect(table.rows[0].name).toBe('Alice');
expect(table.rows[2].name).toBe('Charlie');
});
it('should sort independently for columns with the same key', () => {
const table = new DataTable({ data: sampleData, columns });
table.toggleSort('age');
const ageSortState = table.getSortState('age');
const ageGroupSortState = table.getSortState('ageGroup');
expect(ageSortState).not.toBe(ageGroupSortState);
});
it('should handle sorting with custom getValue returning undefined', () => {
const customColumns: ColumnDef<any>[] = [
{
id: 'customSort',
key: 'value',
name: 'Custom Sort',
sortable: true,
getValue: (row) => (row.value === 3 ? undefined : row.value)
}
];
const customData = [
{ id: 1, value: 3 },
{ id: 2, value: 1 },
{ id: 3, value: 2 }
];
const table = new DataTable({ data: customData, columns: customColumns });
table.toggleSort('customSort');
expect(table.rows[0].value).toBe(1);
expect(table.rows[1].value).toBe(2);
expect(table.rows[2].value).toBe(3);
});
it('should maintain sort stability for equal elements', () => {
const data = [
{ id: 1, value: 'A', order: 1 },
{ id: 2, value: 'B', order: 2 },
{ id: 3, value: 'A', order: 3 },
{ id: 4, value: 'C', order: 4 },
{ id: 5, value: 'B', order: 5 }
];
const columns: ColumnDef<(typeof data)[0]>[] = [
{ id: 'value', key: 'value', name: 'Value', sortable: true },
{ id: 'order', key: 'order', name: 'Order', sortable: true }
];
const table = new DataTable({ data, columns });
table.toggleSort('value');
expect(table.rows.map((r) => r.id)).toEqual([1, 3, 2, 5, 4]);
});
});
describe('Enhanced Sorting', () => {
const sampleData = [
{ id: 1, name: 'Alice', age: 30, score: 85 },
{ id: 2, name: 'Bob', age: 25, score: 92 },
{ id: 3, name: 'Charlie', age: 35, score: 78 },
{ id: 4, name: 'David', age: 28, score: 88 },
{ id: 5, name: 'Eve', age: 32, score: 95 }
];
const columns: ColumnDef<(typeof sampleData)[0]>[] = [
{ id: 'id', key: 'id', name: 'ID', sortable: true },
{ id: 'name', key: 'name', name: 'Name', sortable: true },
{ id: 'age', key: 'age', name: 'Age', sortable: true },
{ id: 'score', key: 'score', name: 'Score', sortable: true },
{
id: 'complexSort',
key: 'score',
name: 'Complex Sort',
sortable: true,
getValue: (row) => row.score,
sorter: (a, b, rowA, rowB) => {
// Sort by score, but if scores are equal, sort by age
if (a === b) {
return rowA.age - rowB.age;
}
return b - a; // Descending order of scores
}
}
];
it('should sort using custom sorter with access to full row data', () => {
const table = new DataTable({ data: sampleData, columns });
table.toggleSort('complexSort');
expect(table.rows[0].name).toBe('Eve'); // Highest score
expect(table.rows[1].name).toBe('Bob'); // Second highest score
expect(table.rows[2].name).toBe('David'); // Third highest score
expect(table.rows[3].name).toBe('Alice'); // Equal score with Charlie, but younger
expect(table.rows[4].name).toBe('Charlie'); // Equal score with Alice, but older
});
it('should handle custom sorter with reverse direction', () => {
const table = new DataTable({ data: sampleData, columns });
table.toggleSort('complexSort');
table.toggleSort('complexSort'); // Toggle twice for descending order
expect(table.rows[0].name).toBe('Charlie');
expect(table.rows[1].name).toBe('Alice');
expect(table.rows[2].name).toBe('David');
expect(table.rows[3].name).toBe('Bob');
expect(table.rows[4].name).toBe('Eve');
});
it('should use custom sorter with initial sort', () => {
const table = new DataTable({
data: sampleData,
columns,
initialSort: 'complexSort',
initialSortDirection: 'desc'
});
expect(table.rows[0].name).toBe('Charlie');
expect(table.rows[4].name).toBe('Eve');
});
it('should maintain custom sort when applying filters', () => {
const table = new DataTable({ data: sampleData, columns });
table.toggleSort('complexSort');
table.setFilter('age', [30, 32, 35]); // Filter out Bob and David
expect(table.rows).toHaveLength(3);
expect(table.rows[0].name).toBe('Eve');
expect(table.rows[1].name).toBe('Alice');
expect(table.rows[2].name).toBe('Charlie');
});
it('should handle custom sorter with all equal primary values', () => {
const equalScoreData = sampleData.map((row) => ({ ...row, score: 90 }));
const table = new DataTable({ data: equalScoreData, columns });
table.toggleSort('complexSort');
expect(table.rows[0].name).toBe('Bob'); // Youngest
expect(table.rows[4].name).toBe('Charlie'); // Oldest
});
});
describe('Filtering', () => {
it('should apply single column filter', () => {
const table = new DataTable({ data: sampleData, columns });
table.setFilter('age', [30, 35]);
expect(table.rows).toHaveLength(2);
expect(table.rows.every((row) => row.age === 30 || row.age === 35)).toBe(true);
});
it('should apply multiple column filters', () => {
const table = new DataTable({ data: sampleData, columns });
table.setFilter('age', [30, 35]);
table.setFilter('name', ['Alice', 'Charlie']);
expect(table.rows).toHaveLength(2);
expect(
table.rows.every(
(row) =>
(row.age === 30 || row.age === 35) && (row.name === 'Alice' || row.name === 'Charlie')
)
).toBe(true);
});
it('should filter derived columns', () => {
const table = new DataTable({ data: sampleData, columns });
table.setFilter('ageGroup', ['Young']);
expect(table.rows).toHaveLength(2);
expect(table.rows.every((row) => row.age < 30)).toBe(true);
});
it('should clear filter', () => {
const table = new DataTable({ data: sampleData, columns });
table.setFilter('age', [30]);
table.clearFilter('age');
expect(table.rows).toHaveLength(5);
});
it('should toggle filter value', () => {
const table = new DataTable({ data: sampleData, columns });
table.toggleFilter('age', 30);
expect(table.rows).toHaveLength(1);
table.toggleFilter('age', 30);
expect(table.rows).toHaveLength(5);
});
it('should apply global filter', () => {
const table = new DataTable({ data: sampleData, columns });
table.globalFilter = 'ali';
expect(table.rows).toHaveLength(1);
expect(table.rows[0].name).toBe('Alice');
});
it('should handle case-insensitive global filter', () => {
const table = new DataTable({ data: sampleData, columns });
table.globalFilter = 'ALICE';
expect(table.rows).toHaveLength(1);
expect(table.rows[0].name).toBe('Alice');
});
it('should handle special characters in global filter', () => {
const specialData = [
{ id: 1, name: 'John (Manager)', age: 40 },
{ id: 2, name: 'Jane [HR]', age: 35 }
];
const table = new DataTable({ data: specialData, columns });
table.globalFilter = '(Manager)';
expect(table.rows).toHaveLength(1);
expect(table.rows[0].name).toBe('John (Manager)');
});
it('should handle custom filter function', () => {
const customColumns = columns.map((col) =>
col.key === 'age'
? {
...col,
filter: (value, filterValue) => value >= filterValue
}
: col
) satisfies ColumnDef<(typeof sampleData)[0]>[];
const table = new DataTable({ data: sampleData, columns: customColumns });
table.setFilter('age', [30]);
expect(table.rows).toHaveLength(3);
expect(table.rows.every((row) => row.age >= 30)).toBe(true);
});
it('should handle multiple rapid filter changes', () => {
const table = new DataTable({ data: sampleData, columns });
table.setFilter('age', [30]);
table.setFilter('name', ['Alice']);
table.clearFilter('age');
table.toggleFilter('name', 'Bob');
expect(table.rows).toHaveLength(2);
expect(table.rows.every((row) => row.name === 'Alice' || row.name === 'Bob')).toBe(true);
});
it('should filter independently for columns with the same key', () => {
const table = new DataTable({ data: sampleData, columns });
table.setFilter('age', [30, 35]);
table.setFilter('ageGroup', ['Young']);
expect(table.rows).toHaveLength(0); // No rows match both filters
});
it('should handle filtering with complex custom filter function', () => {
const customColumns: ColumnDef<any>[] = [
{
id: 'complexFilter',
key: 'value',
name: 'Complex Filter',
filter: (value, filterValue, row) => {
return value > filterValue && row.id % 2 === 0;
}
}
];
const customData = [
{ id: 1, value: 10 },
{ id: 2, value: 20 },
{ id: 3, value: 30 },
{ id: 4, value: 40 }
];
const table = new DataTable({ data: customData, columns: customColumns });
table.setFilter('complexFilter', [15]);
expect(table.rows).toHaveLength(2);
expect(table.rows[0].id).toBe(2);
expect(table.rows[1].id).toBe(4);
});
it('should handle filtering with extremely long filter lists', () => {
const longFilterList = Array.from({ length: 10000 }, (_, i) => i);
const table = new DataTable({ data: sampleData, columns });
table.setFilter('age', longFilterList);
expect(table.rows).toHaveLength(5); // All rows should match
});
it('should handle global filter with special regex characters', () => {
const data = [
{ id: 1, name: 'Alice (Manager)' },
{ id: 2, name: 'Bob [Developer]' }
] as any;
const table = new DataTable({ data, columns });
table.globalFilter = '(Manager)';
expect(table.rows).toHaveLength(1);
expect(table.rows[0].name).toBe('Alice (Manager)');
});
});
describe('Pagination', () => {
it('should paginate correctly', () => {
const table = new DataTable({ data: sampleData, columns, pageSize: 2 });
expect(table.rows).toHaveLength(2);
expect(table.totalPages).toBe(3);
});
it('should change current page', () => {
const table = new DataTable({ data: sampleData, columns, pageSize: 2 });
table.currentPage = 2;
expect(table.currentPage).toBe(2);
expect(table.rows[0].id).toBe(3);
});
it('should not exceed total pages', () => {
const table = new DataTable({ data: sampleData, columns, pageSize: 2 });
table.currentPage = 10;
expect(table.currentPage).toBe(3);
});
it('should handle empty result set', () => {
const table = new DataTable({ data: sampleData, columns });
table.setFilter('age', [100]);
expect(table.rows).toHaveLength(0);
expect(table.totalPages).toBe(1);
expect(table.currentPage).toBe(1);
});
it('should adjust current page when filters reduce total pages', () => {
const table = new DataTable({ data: sampleData, columns, pageSize: 2 });
table.currentPage = 3;
table.setFilter('age', [30, 32]);
expect(table.currentPage).toBe(1);
expect(table.totalPages).toBe(1);
});
it('should handle page size larger than data set', () => {
const table = new DataTable({ data: sampleData, columns, pageSize: 10 });
expect(table.rows).toHaveLength(5);
expect(table.totalPages).toBe(1);
});
it('should handle setting page size to 0', () => {
const table = new DataTable({ data: sampleData, columns, pageSize: 0 });
expect(table.rows).toHaveLength(5); // Should default to showing all rows
});
it('should handle navigation near total page count', () => {
const table = new DataTable({ data: sampleData, columns, pageSize: 2 });
table.currentPage = 3;
expect(table.canGoForward).toBe(false);
expect(table.canGoBack).toBe(true);
table.currentPage = 2;
expect(table.canGoForward).toBe(true);
expect(table.canGoBack).toBe(true);
});
});
describe('baseRows', () => {
it('should return the original data when getting baseRows', () => {
const table = new DataTable({ data: sampleData, columns });
expect(table.baseRows).toEqual(sampleData);
});
it('should update the data when setting baseRows', () => {
const table = new DataTable({ data: sampleData, columns });
const newData = [
{ id: 4, name: 'David', age: 40 },
{ id: 5, name: 'Eve', age: 45 }
];
table.baseRows = newData;
expect(table.baseRows).toEqual(newData);
expect(table.rows).toEqual(newData);
});
it('should reset currentPage to 1 when setting baseRows', () => {
const table = new DataTable({ data: sampleData, columns, pageSize: 1 });
table.currentPage = 2;
table.baseRows = [{ id: 4, name: 'David', age: 40 }];
expect(table.currentPage).toBe(1);
});
it('should maintain existing filters when setting baseRows', () => {
const table = new DataTable({ data: sampleData, columns });
table.setFilter('age', [30]);
const newData = [
{ id: 4, name: 'David', age: 30 },
{ id: 5, name: 'Eve', age: 45 },
{ id: 6, name: 'Frank', age: 30 }
];
table.baseRows = newData;
expect(table.rows).toHaveLength(2);
expect(table.rows.every((row) => row.age === 30)).toBe(true);
});
it('should apply existing filters to new baseRows', () => {
const table = new DataTable({ data: sampleData, columns });
table.setFilter('age', [40, 45]);
const newData = [
{ id: 4, name: 'David', age: 40 },
{ id: 5, name: 'Eve', age: 45 },
{ id: 6, name: 'Frank', age: 50 }
];
table.baseRows = newData;
expect(table.rows).toHaveLength(2);
expect(table.rows.every((row) => row.age === 40 || row.age === 45)).toBe(true);
});
it('should maintain sort state when setting baseRows', () => {
const table = new DataTable({ data: sampleData, columns });
table.toggleSort('age');
const newData = [
{ id: 4, name: 'David', age: 50 },
{ id: 5, name: 'Eve', age: 45 }
];
table.baseRows = newData;
expect(table.rows[0].name).toBe('Eve');
expect(table.rows[1].name).toBe('David');
});
it('should handle setting baseRows to an empty array', () => {
const table = new DataTable({ data: sampleData, columns });
table.baseRows = [];
expect(table.baseRows).toEqual([]);
expect(table.rows).toEqual([]);
expect(table.totalPages).toBe(1);
});
it('should handle setting baseRows with different data structure', () => {
const table = new DataTable({ data: sampleData, columns });
const newData = [
{ id: 4, name: 'David', age: 40, newField: 'value' },
{ id: 5, name: 'Eve', age: 45, newField: 'another value' }
];
table.baseRows = newData;
expect(table.baseRows).toEqual(newData);
expect(table.rows).toEqual(newData);
});
it('should update global filter results when setting baseRows', () => {
const table = new DataTable({ data: sampleData, columns });
table.globalFilter = 'Alice';
expect(table.rows).toHaveLength(1);
table.baseRows = [
{ id: 4, name: 'David', age: 40 },
{ id: 5, name: 'Alice Smith', age: 45 }
];
expect(table.rows).toHaveLength(1);
expect(table.rows[0].name).toBe('Alice Smith');
});
it('should handle setting baseRows multiple times', () => {
const table = new DataTable({ data: sampleData, columns });
table.baseRows = [{ id: 4, name: 'David', age: 40 }];
table.baseRows = [{ id: 5, name: 'Eve', age: 45 }];
table.baseRows = [{ id: 6, name: 'Frank', age: 50 }];
expect(table.baseRows).toHaveLength(1);
expect(table.baseRows[0].name).toBe('Frank');
});
});
describe('allRows', () => {
const sampleData = [
{ id: 1, name: 'Alice', age: 30 },
{ id: 2, name: 'Bob', age: 25 },
{ id: 3, name: 'Charlie', age: 35 },
{ id: 4, name: 'David', age: 28 },
{ id: 5, name: 'Eve', age: 32 }
];
const columns = [
{ id: 'id', key: 'id', name: 'ID', sortable: true },
{ id: 'name', key: 'name', name: 'Name', sortable: true },
{ id: 'age', key: 'age', name: 'Age', sortable: true }
] satisfies ColumnDef<(typeof sampleData)[0]>[];
it('should return all rows when no filtering or sorting is applied', () => {
const table = new DataTable({ data: sampleData, columns, pageSize: 2 });
expect(table.allRows).toHaveLength(5);
expect(table.allRows).toEqual(sampleData);
});
it('should return all filtered rows regardless of pagination', () => {
const table = new DataTable({ data: sampleData, columns, pageSize: 2 });
table.setFilter('age', [30, 35]);
expect(table.rows).toHaveLength(2); // Due to pagination
expect(table.allRows).toHaveLength(2); // All matching rows
expect(table.allRows.every((row) => row.age === 30 || row.age === 35)).toBe(true);
});
it('should return all sorted rows', () => {
const table = new DataTable({ data: sampleData, columns, pageSize: 2 });
table.toggleSort('age');
expect(table.allRows).toHaveLength(5);
expect(table.allRows[0].age).toBe(25);
expect(table.allRows[4].age).toBe(35);
});
it('should return all filtered and sorted rows', () => {
const table = new DataTable({ data: sampleData, columns, pageSize: 2 });
table.setFilter('age', [30, 32, 35]);
table.toggleSort('age');
expect(table.allRows).toHaveLength(3);
expect(table.allRows[0].age).toBe(30);
expect(table.allRows[1].age).toBe(32);
expect(table.allRows[2].age).toBe(35);
});
it('should update when global filter is applied', () => {
const table = new DataTable({ data: sampleData, columns, pageSize: 2 });
table.globalFilter = 'a'; // Matches 'Alice' and 'Charlie' and 'David'
expect(table.allRows).toHaveLength(3);
expect(table.allRows.every((row) => ['Alice', 'Charlie', 'David'].includes(row.name))).toBe(
true
);
});
it('should return an empty array when no rows match the filters', () => {
const table = new DataTable({ data: sampleData, columns, pageSize: 2 });
table.setFilter('age', [100]); // No rows have age 100
expect(table.allRows).toHaveLength(0);
});
it('should reflect changes when baseRows is updated', () => {
const table = new DataTable({ data: sampleData, columns, pageSize: 2 });
const newData = [
{ id: 6, name: 'Frank', age: 40 },
{ id: 7, name: 'Grace', age: 45 }
];
table.baseRows = newData;
expect(table.allRows).toHaveLength(2);
expect(table.allRows).toEqual(newData);
});
it('should contain all filtered and sorted rows while rows respects pagination', () => {
const table = new DataTable({ data: sampleData, columns, pageSize: 2 });
table.setFilter('age', [30, 32, 35]);
table.toggleSort('age');
expect(table.allRows).toHaveLength(3);
expect(table.allRows.map((row) => row.age)).toEqual([30, 32, 35]);
expect(table.rows).toHaveLength(2);
expect(table.rows.map((row) => row.age)).toEqual([30, 32]);
table.currentPage = 2;
expect(table.allRows).toHaveLength(3);
expect(table.allRows.map((row) => row.age)).toEqual([30, 32, 35]);
expect(table.rows).toHaveLength(1);
expect(table.rows.map((row) => row.age)).toEqual([35]);
});
});
});