Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/feature unit tests p1 #5847

Merged
merged 2 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/table-core/tests/helpers/generateTestTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import type { Person } from '../fixtures/data/types'

export function generateTestTableWithData<TFeatures extends TableFeatures>(
lengths: Array<number> | number = 10,
options?: Omit<TableOptions<TFeatures, Person>, 'data' | 'columns'>,
options?: Omit<
TableOptions<TFeatures, Person>,
'data' | 'columns' | '_features'
> & {
_features?: TableFeatures
},
) {
const lengthsArray = Array.isArray(lengths) ? lengths : [lengths]
const data = generateTestData(...lengthsArray)
Expand Down
6 changes: 1 addition & 5 deletions packages/table-core/tests/helpers/testUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { vi } from 'vitest'
import type { RowPinningState } from '../../src'
import type { Person } from '../fixtures/data/types'

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

export function getUpdaterResult(
mock: ReturnType<typeof vi.fn>,
input: RowPinningState,
) {
export function getUpdaterResult(mock: ReturnType<typeof vi.fn>, input: any) {
const updaterFn = mock.mock.calls[0]?.[0]
return updaterFn?.(input)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import { describe, expect, it, vi } from 'vitest'
import { generateTestTableWithData } from '../../../helpers/generateTestTable'
import {
column_getIndex,
column_getIsFirstColumn,
column_getIsLastColumn,
getDefaultColumnOrderState,
orderColumns,
table_getOrderColumnsFn,
table_resetColumnOrder,
table_setColumnOrder,
} from '../../../../src/features/column-ordering/columnOrderingFeature.utils'
import type { TableFeatures } from '../../../../src'

describe('getDefaultColumnOrderState', () => {
it('should return an empty array', () => {
expect(getDefaultColumnOrderState()).toEqual([])
})
})

describe('column_getIndex', () => {
it('should return correct index for a column', () => {
const table = generateTestTableWithData<TableFeatures>(3)
const column = table.getAllLeafColumns()[1]!

expect(column_getIndex(column)).toBe(1)
})

it('should return -1 for non-existent column', () => {
const table = generateTestTableWithData<TableFeatures>(3)
const column = {
...table.getAllLeafColumns()[0],
id: 'non-existent',
table,
} as any

expect(column_getIndex(column)).toBe(-1)
})
})

describe('column_getIsFirstColumn', () => {
it('should return true for first column', () => {
const table = generateTestTableWithData<TableFeatures>(3)
const firstColumn = table.getAllLeafColumns()[0]!

expect(column_getIsFirstColumn(firstColumn)).toBe(true)
})

it('should return false for non-first column', () => {
const table = generateTestTableWithData<TableFeatures>(3)
const secondColumn = table.getAllLeafColumns()[1]!

expect(column_getIsFirstColumn(secondColumn)).toBe(false)
})
})

describe('column_getIsLastColumn', () => {
it('should return true for last column', () => {
const table = generateTestTableWithData<TableFeatures>(3)
const columns = table.getAllLeafColumns()
const lastColumn = columns[columns.length - 1]!

expect(column_getIsLastColumn(lastColumn)).toBe(true)
})

it('should return false for non-last column', () => {
const table = generateTestTableWithData<TableFeatures>(3)
const firstColumn = table.getAllLeafColumns()[0]!

expect(column_getIsLastColumn(firstColumn)).toBe(false)
})
})

describe('table_setColumnOrder', () => {
it('should call onColumnOrderChange with updater', () => {
const onColumnOrderChange = vi.fn()
const table = generateTestTableWithData<TableFeatures>(3, {
onColumnOrderChange,
})
const newOrder = ['col1', 'col2']

table_setColumnOrder(table, newOrder)

expect(onColumnOrderChange).toHaveBeenCalledWith(newOrder)
})
})

describe('table_resetColumnOrder', () => {
it('should reset to empty array when defaultState is true', () => {
const onColumnOrderChange = vi.fn()
const table = generateTestTableWithData<TableFeatures>(3, {
onColumnOrderChange,
})

table_resetColumnOrder(table, true)

expect(onColumnOrderChange).toHaveBeenCalledWith([])
})

it('should reset to initialState when defaultState is false', () => {
const initialColumnOrder = ['col1', 'col2']
const onColumnOrderChange = vi.fn()
const table = generateTestTableWithData<TableFeatures>(3, {
onColumnOrderChange,
initialState: { columnOrder: initialColumnOrder },
})

table_resetColumnOrder(table, false)

expect(onColumnOrderChange).toHaveBeenCalledWith(initialColumnOrder)
})
})

describe('table_getOrderColumnsFn', () => {
it('should return original columns when no column order is specified', () => {
const table = generateTestTableWithData<TableFeatures>(3)
const columns = table.getAllLeafColumns()
const orderFn = table_getOrderColumnsFn(table)

expect(orderFn(columns)).toEqual(columns)
})

it('should reorder columns according to columnOrder', () => {
const table = generateTestTableWithData<TableFeatures>(3, {
state: {
columnOrder: ['lastName', 'firstName'],
},
})
const columns = table.getAllLeafColumns()
const orderFn = table_getOrderColumnsFn(table)
const orderedColumns = orderFn(columns)

expect(orderedColumns[0]?.id).toBe('lastName')
expect(orderedColumns[1]?.id).toBe('firstName')
})
})

describe('orderColumns', () => {
it('should return original columns when no grouping is present', () => {
const table = generateTestTableWithData<TableFeatures>(3)
const columns = table.getAllLeafColumns()

expect(orderColumns(table, columns)).toEqual(columns)
})

it('should remove grouped columns when groupedColumnMode is "remove"', () => {
const table = generateTestTableWithData<TableFeatures>(3, {
state: {
grouping: ['firstName'],
},
groupedColumnMode: 'remove',
})
const columns = table.getAllLeafColumns()
const orderedColumns = orderColumns(table, columns)

expect(orderedColumns.find((col) => col.id === 'firstName')).toBeUndefined()
})

it('should move grouped columns to start when groupedColumnMode is "reorder"', () => {
const table = generateTestTableWithData<TableFeatures>(3, {
state: {
grouping: ['lastName'],
},
groupedColumnMode: 'reorder',
})
const columns = table.getAllLeafColumns()
const orderedColumns = orderColumns(table, columns)

expect(orderedColumns[0]?.id).toBe('lastName')
})
})
Loading
Loading