Skip to content

Commit

Permalink
add more tests for row selection functions
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Sep 15, 2023
1 parent c29cd13 commit 42028fa
Showing 1 changed file with 126 additions and 0 deletions.
126 changes: 126 additions & 0 deletions packages/table-core/__tests__/RowSelection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,132 @@ function generateColumns(people: Person[]): PersonColumn[] {
}

describe('RowSelection', () => {
describe('selectRowsFn', () => {
it('should only return rows that are selected', () => {
const data = makeData(5)
const columns = generateColumns(data)

const table = createTable<Person>({
enableRowSelection: true,
onStateChange() {},
renderFallbackValue: '',
data,
getSubRows: row => row.subRows,
state: {
rowSelection: {
'0': true,
'2': true,
},
},
columns,
getCoreRowModel: getCoreRowModel(),
})
const rowModel = table.getCoreRowModel()

const result = RowSelection.selectRowsFn(table, rowModel)

expect(result.rows.length).toBe(2)
expect(result.flatRows.length).toBe(2)
expect(result.rowsById).toHaveProperty('0')
expect(result.rowsById).toHaveProperty('2')
})

it('should recurse into subRows and only return selected subRows', () => {
const data = makeData(3, 2) // assuming 3 parent rows with 2 sub-rows each
const columns = generateColumns(data)

const table = createTable<Person>({
enableRowSelection: true,
onStateChange() {},
renderFallbackValue: '',
data,
getSubRows: row => row.subRows,
state: {
rowSelection: {
'0': true,
'0.0': true,
},
},
columns,
getCoreRowModel: getCoreRowModel(),
})
const rowModel = table.getCoreRowModel()

const result = RowSelection.selectRowsFn(table, rowModel)

expect(result.rows[0].subRows.length).toBe(1)
expect(result.flatRows.length).toBe(2)
expect(result.rowsById).toHaveProperty('0')
expect(result.rowsById).toHaveProperty('0.0')
})

it('should return an empty list if no rows are selected', () => {
const data = makeData(5)
const columns = generateColumns(data)

const table = createTable<Person>({
enableRowSelection: true,
onStateChange() {},
renderFallbackValue: '',
data,
getSubRows: row => row.subRows,
state: {
rowSelection: {},
},
columns,
getCoreRowModel: getCoreRowModel(),
})
const rowModel = table.getCoreRowModel()

const result = RowSelection.selectRowsFn(table, rowModel)

expect(result.rows.length).toBe(0)
expect(result.flatRows.length).toBe(0)
expect(result.rowsById).toEqual({})
})
})
describe('isRowSelected', () => {
it('should return true if the row id exists in selection and is set to true', () => {
const row = { id: '123', data: {} } as any
const selection: Record<string, boolean> = {
'123': true,
'456': false,
}

const result = RowSelection.isRowSelected(row, selection)
expect(result).toEqual(true)
})

it('should return false if the row id exists in selection and is set to false', () => {
const row = { id: '456', data: {} } as any
const selection: Record<string, boolean> = {
'123': true,
'456': false,
}

const result = RowSelection.isRowSelected(row, selection)
expect(result).toEqual(false)
})

it('should return false if the row id does not exist in selection', () => {
const row = { id: '789', data: {} } as any
const selection: Record<string, boolean> = {
'123': true,
'456': false,
}

const result = RowSelection.isRowSelected(row, selection)
expect(result).toEqual(false)
})

it('should return false if selection is an empty object', () => {
const row = { id: '789', data: {} } as any
const selection: Record<string, boolean> = {}

const result = RowSelection.isRowSelected(row, selection)
expect(result).toEqual(false)
})
})
describe('isSubRowSelected', () => {
it('should return false if there are no sub-rows', () => {
const data = makeData(3)
Expand Down

0 comments on commit 42028fa

Please sign in to comment.