WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit ba25178

Browse files
committed
feat(tests): add toggleAllRowsSelected tests to respect row selection rules
1 parent 02c203a commit ba25178

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

packages/table-core/src/features/RowSelection.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ export const RowSelection: TableFeature = {
245245
})
246246
} else {
247247
preGroupedFlatRows.forEach(row => {
248+
if (!row.getCanSelect()) {
249+
return
250+
}
248251
delete rowSelection[row.id]
249252
})
250253
}

packages/table-core/tests/RowSelection.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,4 +320,74 @@ describe('RowSelection', () => {
320320
expect(result).toEqual('some')
321321
})
322322
})
323+
324+
describe('toggleAllRowsSelected', () => {
325+
it('should respect enableRowSelection when selecting all rows', () => {
326+
const data = makeData(5)
327+
const columns = generateColumns(data)
328+
329+
let rowSelection: Record<string, boolean> = {}
330+
331+
const table = createTable<Person>({
332+
enableRowSelection: row => row.index !== 2, // Row at index 2 cannot be selected
333+
onStateChange: updater => {
334+
const newState =
335+
typeof updater === 'function' ? updater(table.getState()) : updater
336+
rowSelection = newState.rowSelection
337+
},
338+
renderFallbackValue: '',
339+
data,
340+
get state() {
341+
return { rowSelection }
342+
},
343+
columns,
344+
getCoreRowModel: getCoreRowModel(),
345+
})
346+
347+
table.toggleAllRowsSelected(true)
348+
349+
expect(rowSelection['0']).toBe(true)
350+
expect(rowSelection['1']).toBe(true)
351+
expect(rowSelection['2']).toBeUndefined() // Row 2 should not be selected
352+
expect(rowSelection['3']).toBe(true)
353+
expect(rowSelection['4']).toBe(true)
354+
})
355+
356+
it('should respect enableRowSelection when deselecting all rows', () => {
357+
const data = makeData(5)
358+
const columns = generateColumns(data)
359+
360+
let rowSelection: Record<string, boolean> = {
361+
'0': true,
362+
'1': true,
363+
'2': true, // This row was somehow selected (maybe rules changed)
364+
'3': true,
365+
'4': true,
366+
}
367+
368+
const table = createTable<Person>({
369+
enableRowSelection: row => row.index !== 2, // Row at index 2 cannot be selected
370+
onStateChange: updater => {
371+
const newState =
372+
typeof updater === 'function' ? updater(table.getState()) : updater
373+
rowSelection = newState.rowSelection
374+
},
375+
renderFallbackValue: '',
376+
data,
377+
get state() {
378+
return { rowSelection }
379+
},
380+
columns,
381+
getCoreRowModel: getCoreRowModel(),
382+
})
383+
384+
table.toggleAllRowsSelected(false)
385+
386+
expect(rowSelection['0']).toBeUndefined()
387+
expect(rowSelection['1']).toBeUndefined()
388+
expect(rowSelection['2']).toBe(true) // Row 2 should remain selected since it can't be deselected
389+
expect(rowSelection['3']).toBeUndefined()
390+
expect(rowSelection['4']).toBeUndefined()
391+
})
392+
})
323393
})

0 commit comments

Comments
 (0)