@@ -11,9 +11,16 @@ describe('DataTable', () => {
11
11
] ;
12
12
13
13
const columns = [
14
- { key : 'id' , name : 'ID' , sortable : true } ,
15
- { key : 'name' , name : 'Name' , sortable : true } ,
16
- { key : 'age' , name : 'Age' , sortable : true }
14
+ { id : 'id' , key : 'id' , name : 'ID' , sortable : true } ,
15
+ { id : 'name' , key : 'name' , name : 'Name' , sortable : true } ,
16
+ { id : 'age' , key : 'age' , name : 'Age' , sortable : true } ,
17
+ {
18
+ id : 'ageGroup' ,
19
+ key : 'age' ,
20
+ name : 'Age Group' ,
21
+ sortable : true ,
22
+ getValue : ( row ) => ( row . age < 30 ? 'Young' : 'Adult' )
23
+ }
17
24
] satisfies ColumnDef < ( typeof sampleData ) [ 0 ] > [ ] ;
18
25
19
26
describe ( 'Initialization' , ( ) => {
@@ -60,12 +67,18 @@ describe('DataTable', () => {
60
67
const incompleteData = [
61
68
{ id : 1 , name : 'Alice' } ,
62
69
{ id : 2 , age : 25 }
63
- ] ;
70
+ ] as any [ ] ;
64
71
const table = new DataTable ( { data : incompleteData , columns } ) ;
65
72
expect ( table . rows ) . toHaveLength ( 2 ) ;
66
73
expect ( table . rows [ 0 ] . age ) . toBeUndefined ( ) ;
67
74
expect ( table . rows [ 1 ] . name ) . toBeUndefined ( ) ;
68
75
} ) ;
76
+
77
+ it ( 'should handle multiple columns with the same key' , ( ) => {
78
+ const table = new DataTable ( { data : sampleData , columns } ) ;
79
+ expect ( table . columns ) . toHaveLength ( 4 ) ;
80
+ expect ( table . columns . filter ( ( col ) => col . key === 'age' ) ) . toHaveLength ( 2 ) ;
81
+ } ) ;
69
82
} ) ;
70
83
71
84
describe ( 'Sorting' , ( ) => {
@@ -101,7 +114,7 @@ describe('DataTable', () => {
101
114
102
115
it ( 'should handle sorting with custom sorter function' , ( ) => {
103
116
const customColumns = columns . map ( ( col ) =>
104
- col . key === 'name'
117
+ col . id === 'name'
105
118
? {
106
119
...col ,
107
120
sorter : ( a , b ) => b . name . localeCompare ( a . name ) // Reverse alphabetical order
@@ -136,14 +149,22 @@ describe('DataTable', () => {
136
149
{ id : 1 , name : 'Alice' , age : null } ,
137
150
{ id : 2 , name : 'Bob' , age : null } ,
138
151
{ id : 3 , name : 'Charlie' , age : null }
139
- ] ;
152
+ ] as any [ ] ;
140
153
const table = new DataTable ( { data : nullData , columns } ) ;
141
154
table . toggleSort ( 'age' ) ;
142
155
expect ( table . rows ) . toHaveLength ( 3 ) ;
143
156
// The order should remain unchanged
144
157
expect ( table . rows [ 0 ] . name ) . toBe ( 'Alice' ) ;
145
158
expect ( table . rows [ 2 ] . name ) . toBe ( 'Charlie' ) ;
146
159
} ) ;
160
+
161
+ it ( 'should sort independently for columns with the same key' , ( ) => {
162
+ const table = new DataTable ( { data : sampleData , columns } ) ;
163
+ table . toggleSort ( 'age' ) ;
164
+ const ageSortState = table . getSortState ( 'age' ) ;
165
+ const ageGroupSortState = table . getSortState ( 'ageGroup' ) ;
166
+ expect ( ageSortState ) . not . toBe ( ageGroupSortState ) ;
167
+ } ) ;
147
168
} ) ;
148
169
149
170
describe ( 'Filtering' , ( ) => {
@@ -167,6 +188,13 @@ describe('DataTable', () => {
167
188
) . toBe ( true ) ;
168
189
} ) ;
169
190
191
+ it ( 'should filter derived columns' , ( ) => {
192
+ const table = new DataTable ( { data : sampleData , columns } ) ;
193
+ table . setFilter ( 'ageGroup' , [ 'Young' ] ) ;
194
+ expect ( table . rows ) . toHaveLength ( 2 ) ;
195
+ expect ( table . rows . every ( ( row ) => row . age < 30 ) ) . toBe ( true ) ;
196
+ } ) ;
197
+
170
198
it ( 'should clear filter' , ( ) => {
171
199
const table = new DataTable ( { data : sampleData , columns } ) ;
172
200
table . setFilter ( 'age' , [ 30 ] ) ;
@@ -231,6 +259,13 @@ describe('DataTable', () => {
231
259
expect ( table . rows ) . toHaveLength ( 2 ) ;
232
260
expect ( table . rows . every ( ( row ) => row . name === 'Alice' || row . name === 'Bob' ) ) . toBe ( true ) ;
233
261
} ) ;
262
+
263
+ it ( 'should filter independently for columns with the same key' , ( ) => {
264
+ const table = new DataTable ( { data : sampleData , columns } ) ;
265
+ table . setFilter ( 'age' , [ 30 , 35 ] ) ;
266
+ table . setFilter ( 'ageGroup' , [ 'Young' ] ) ;
267
+ expect ( table . rows ) . toHaveLength ( 0 ) ; // No rows match both filters
268
+ } ) ;
234
269
} ) ;
235
270
236
271
describe ( 'Pagination' , ( ) => {
@@ -276,19 +311,7 @@ describe('DataTable', () => {
276
311
} ) ;
277
312
} ) ;
278
313
279
- describe ( 'baseRows functionality' , ( ) => {
280
- const sampleData = [
281
- { id : 1 , name : 'Alice' , age : 30 } ,
282
- { id : 2 , name : 'Bob' , age : 25 } ,
283
- { id : 3 , name : 'Charlie' , age : 35 }
284
- ] ;
285
-
286
- const columns = [
287
- { key : 'id' , name : 'ID' , sortable : true } ,
288
- { key : 'name' , name : 'Name' , sortable : true } ,
289
- { key : 'age' , name : 'Age' , sortable : true }
290
- ] satisfies ColumnDef < ( typeof sampleData ) [ 0 ] > [ ] ;
291
-
314
+ describe ( 'baseRows' , ( ) => {
292
315
it ( 'should return the original data when getting baseRows' , ( ) => {
293
316
const table = new DataTable ( { data : sampleData , columns } ) ;
294
317
expect ( table . baseRows ) . toEqual ( sampleData ) ;
0 commit comments