Skip to content

Commit 133b26b

Browse files
authored
Feat/feature unit tests p1 (#5847)
* feat: added unit tests for column ordering and pinning * feat: added more unit tests
1 parent de273af commit 133b26b

File tree

6 files changed

+1649
-6
lines changed

6 files changed

+1649
-6
lines changed

packages/table-core/tests/helpers/generateTestTable.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import type { Person } from '../fixtures/data/types'
66

77
export function generateTestTableWithData<TFeatures extends TableFeatures>(
88
lengths: Array<number> | number = 10,
9-
options?: Omit<TableOptions<TFeatures, Person>, 'data' | 'columns'>,
9+
options?: Omit<
10+
TableOptions<TFeatures, Person>,
11+
'data' | 'columns' | '_features'
12+
> & {
13+
_features?: TableFeatures
14+
},
1015
) {
1116
const lengthsArray = Array.isArray(lengths) ? lengths : [lengths]
1217
const data = generateTestData(...lengthsArray)

packages/table-core/tests/helpers/testUtils.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { vi } from 'vitest'
2-
import type { RowPinningState } from '../../src'
32
import type { Person } from '../fixtures/data/types'
43

54
export const createArrayOfNumbers = (length: number) => {
@@ -13,10 +12,7 @@ export const getPeopleIds = (
1312
return people.map((person, index) => (usePersonId ? person.id : `${index}`))
1413
}
1514

16-
export function getUpdaterResult(
17-
mock: ReturnType<typeof vi.fn>,
18-
input: RowPinningState,
19-
) {
15+
export function getUpdaterResult(mock: ReturnType<typeof vi.fn>, input: any) {
2016
const updaterFn = mock.mock.calls[0]?.[0]
2117
return updaterFn?.(input)
2218
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
import { describe, expect, it, vi } from 'vitest'
2+
import { generateTestTableWithData } from '../../../helpers/generateTestTable'
3+
import {
4+
column_getIndex,
5+
column_getIsFirstColumn,
6+
column_getIsLastColumn,
7+
getDefaultColumnOrderState,
8+
orderColumns,
9+
table_getOrderColumnsFn,
10+
table_resetColumnOrder,
11+
table_setColumnOrder,
12+
} from '../../../../src/features/column-ordering/columnOrderingFeature.utils'
13+
import type { TableFeatures } from '../../../../src'
14+
15+
describe('getDefaultColumnOrderState', () => {
16+
it('should return an empty array', () => {
17+
expect(getDefaultColumnOrderState()).toEqual([])
18+
})
19+
})
20+
21+
describe('column_getIndex', () => {
22+
it('should return correct index for a column', () => {
23+
const table = generateTestTableWithData<TableFeatures>(3)
24+
const column = table.getAllLeafColumns()[1]!
25+
26+
expect(column_getIndex(column)).toBe(1)
27+
})
28+
29+
it('should return -1 for non-existent column', () => {
30+
const table = generateTestTableWithData<TableFeatures>(3)
31+
const column = {
32+
...table.getAllLeafColumns()[0],
33+
id: 'non-existent',
34+
table,
35+
} as any
36+
37+
expect(column_getIndex(column)).toBe(-1)
38+
})
39+
})
40+
41+
describe('column_getIsFirstColumn', () => {
42+
it('should return true for first column', () => {
43+
const table = generateTestTableWithData<TableFeatures>(3)
44+
const firstColumn = table.getAllLeafColumns()[0]!
45+
46+
expect(column_getIsFirstColumn(firstColumn)).toBe(true)
47+
})
48+
49+
it('should return false for non-first column', () => {
50+
const table = generateTestTableWithData<TableFeatures>(3)
51+
const secondColumn = table.getAllLeafColumns()[1]!
52+
53+
expect(column_getIsFirstColumn(secondColumn)).toBe(false)
54+
})
55+
})
56+
57+
describe('column_getIsLastColumn', () => {
58+
it('should return true for last column', () => {
59+
const table = generateTestTableWithData<TableFeatures>(3)
60+
const columns = table.getAllLeafColumns()
61+
const lastColumn = columns[columns.length - 1]!
62+
63+
expect(column_getIsLastColumn(lastColumn)).toBe(true)
64+
})
65+
66+
it('should return false for non-last column', () => {
67+
const table = generateTestTableWithData<TableFeatures>(3)
68+
const firstColumn = table.getAllLeafColumns()[0]!
69+
70+
expect(column_getIsLastColumn(firstColumn)).toBe(false)
71+
})
72+
})
73+
74+
describe('table_setColumnOrder', () => {
75+
it('should call onColumnOrderChange with updater', () => {
76+
const onColumnOrderChange = vi.fn()
77+
const table = generateTestTableWithData<TableFeatures>(3, {
78+
onColumnOrderChange,
79+
})
80+
const newOrder = ['col1', 'col2']
81+
82+
table_setColumnOrder(table, newOrder)
83+
84+
expect(onColumnOrderChange).toHaveBeenCalledWith(newOrder)
85+
})
86+
})
87+
88+
describe('table_resetColumnOrder', () => {
89+
it('should reset to empty array when defaultState is true', () => {
90+
const onColumnOrderChange = vi.fn()
91+
const table = generateTestTableWithData<TableFeatures>(3, {
92+
onColumnOrderChange,
93+
})
94+
95+
table_resetColumnOrder(table, true)
96+
97+
expect(onColumnOrderChange).toHaveBeenCalledWith([])
98+
})
99+
100+
it('should reset to initialState when defaultState is false', () => {
101+
const initialColumnOrder = ['col1', 'col2']
102+
const onColumnOrderChange = vi.fn()
103+
const table = generateTestTableWithData<TableFeatures>(3, {
104+
onColumnOrderChange,
105+
initialState: { columnOrder: initialColumnOrder },
106+
})
107+
108+
table_resetColumnOrder(table, false)
109+
110+
expect(onColumnOrderChange).toHaveBeenCalledWith(initialColumnOrder)
111+
})
112+
})
113+
114+
describe('table_getOrderColumnsFn', () => {
115+
it('should return original columns when no column order is specified', () => {
116+
const table = generateTestTableWithData<TableFeatures>(3)
117+
const columns = table.getAllLeafColumns()
118+
const orderFn = table_getOrderColumnsFn(table)
119+
120+
expect(orderFn(columns)).toEqual(columns)
121+
})
122+
123+
it('should reorder columns according to columnOrder', () => {
124+
const table = generateTestTableWithData<TableFeatures>(3, {
125+
state: {
126+
columnOrder: ['lastName', 'firstName'],
127+
},
128+
})
129+
const columns = table.getAllLeafColumns()
130+
const orderFn = table_getOrderColumnsFn(table)
131+
const orderedColumns = orderFn(columns)
132+
133+
expect(orderedColumns[0]?.id).toBe('lastName')
134+
expect(orderedColumns[1]?.id).toBe('firstName')
135+
})
136+
})
137+
138+
describe('orderColumns', () => {
139+
it('should return original columns when no grouping is present', () => {
140+
const table = generateTestTableWithData<TableFeatures>(3)
141+
const columns = table.getAllLeafColumns()
142+
143+
expect(orderColumns(table, columns)).toEqual(columns)
144+
})
145+
146+
it('should remove grouped columns when groupedColumnMode is "remove"', () => {
147+
const table = generateTestTableWithData<TableFeatures>(3, {
148+
state: {
149+
grouping: ['firstName'],
150+
},
151+
groupedColumnMode: 'remove',
152+
})
153+
const columns = table.getAllLeafColumns()
154+
const orderedColumns = orderColumns(table, columns)
155+
156+
expect(orderedColumns.find((col) => col.id === 'firstName')).toBeUndefined()
157+
})
158+
159+
it('should move grouped columns to start when groupedColumnMode is "reorder"', () => {
160+
const table = generateTestTableWithData<TableFeatures>(3, {
161+
state: {
162+
grouping: ['lastName'],
163+
},
164+
groupedColumnMode: 'reorder',
165+
})
166+
const columns = table.getAllLeafColumns()
167+
const orderedColumns = orderColumns(table, columns)
168+
169+
expect(orderedColumns[0]?.id).toBe('lastName')
170+
})
171+
})

0 commit comments

Comments
 (0)