diff --git a/packages/table-core/tests/RowPinning.test.ts b/packages/table-core/tests/RowPinning.test.ts index 223c37562d..5cd5bcb65a 100644 --- a/packages/table-core/tests/RowPinning.test.ts +++ b/packages/table-core/tests/RowPinning.test.ts @@ -1,25 +1,27 @@ -import { describe, expect, it } from 'vitest' +import { beforeEach, describe, expect, it } from 'vitest' import { - ColumnDef, - createColumnHelper, constructTable, - createCoreRowModel, + coreFeatures, + createColumnHelper, createPaginatedRowModel, - rowPinningFeature, rowPaginationFeature, + rowPinningFeature, } from '../src' -import { makeData, Person } from './makeTestData' +import { makeData } from './makeTestData' +import type { ColumnDef } from '../src' +import type { Person } from './makeTestData' type personKeys = keyof Person -type PersonColumn = ColumnDef< - any, - Person, - string | number | Person[] | undefined -> +type PersonColumn = ColumnDef -function generateColumns(people: Person[]): PersonColumn[] { +function generateColumns(people: Array): Array { const columnHelper = createColumnHelper() const person = people[0] + + if (!person) { + return [] + } + return Object.keys(person).map((key) => { const typedKey = key as personKeys return columnHelper.accessor(typedKey, { id: typedKey }) @@ -27,14 +29,22 @@ function generateColumns(people: Person[]): PersonColumn[] { } describe('rowPinningFeature', () => { + let data: Array + let columns: Array + beforeEach(() => { + data = makeData(10) + columns = generateColumns(data) + }) + describe('constructTable', () => { describe('getTopRows', () => { it('should return pinned rows when keepPinnedRows is true rows are visible', () => { - const data = makeData(10) - const columns = generateColumns(data) - const table = constructTable({ - _features: { rowPinningFeature, rowPaginationFeature }, + _features: { + rowPinningFeature, + rowPaginationFeature, + ...coreFeatures, + }, _rowModels: { paginatedRowModel: createPaginatedRowModel(), }, @@ -46,7 +56,7 @@ describe('rowPinningFeature', () => { state: { pagination: { pageSize: 5, - pageIndex: 0, //pinned rows will be on page 0 + pageIndex: 0, // pinned rows will be on page 0 }, rowPinning: { bottom: [], @@ -59,15 +69,16 @@ describe('rowPinningFeature', () => { const result = table.getTopRows() expect(result.length).toBe(2) - expect(result[0].id).toBe('0') - expect(result[1].id).toBe('1') + expect(result[0]?.id).toBe('0') + expect(result[1]?.id).toBe('1') }) it('should return pinned rows when keepPinnedRows is true rows are not visible', () => { - const data = makeData(10) - const columns = generateColumns(data) - const table = constructTable({ - _features: { rowPinningFeature, rowPaginationFeature }, + _features: { + rowPinningFeature, + rowPaginationFeature, + ...coreFeatures, + }, _rowModels: { paginatedRowModel: createPaginatedRowModel(), }, @@ -79,7 +90,7 @@ describe('rowPinningFeature', () => { state: { pagination: { pageSize: 5, - pageIndex: 1, //pinned rows will be on page 0 + pageIndex: 1, // pinned rows will be on page 0 }, rowPinning: { bottom: [], @@ -92,15 +103,16 @@ describe('rowPinningFeature', () => { const result = table.getTopRows() expect(result.length).toBe(2) - expect(result[0].id).toBe('0') - expect(result[1].id).toBe('1') + expect(result[0]?.id).toBe('0') + expect(result[1]?.id).toBe('1') }) it('should return pinned rows when keepPinnedRows is false rows are visible', () => { - const data = makeData(10) - const columns = generateColumns(data) - const table = constructTable({ - _features: { rowPinningFeature, rowPaginationFeature }, + _features: { + rowPinningFeature, + rowPaginationFeature, + ...coreFeatures, + }, _rowModels: { paginatedRowModel: createPaginatedRowModel(), }, @@ -112,7 +124,7 @@ describe('rowPinningFeature', () => { state: { pagination: { pageSize: 5, - pageIndex: 0, //pinned rows will be on page 0 + pageIndex: 0, // pinned rows will be on page 0 }, rowPinning: { bottom: [], @@ -125,15 +137,16 @@ describe('rowPinningFeature', () => { const result = table.getTopRows() expect(result.length).toBe(2) - expect(result[0].id).toBe('0') - expect(result[1].id).toBe('1') + expect(result[0]?.id).toBe('0') + expect(result[1]?.id).toBe('1') }) it('should not return pinned rows when keepPinnedRows is false and rows are not visible', () => { - const data = makeData(10) - const columns = generateColumns(data) - const table = constructTable({ - _features: { rowPinningFeature, rowPaginationFeature }, + _features: { + rowPinningFeature, + rowPaginationFeature, + ...coreFeatures, + }, _rowModels: { paginatedRowModel: createPaginatedRowModel(), }, @@ -145,7 +158,7 @@ describe('rowPinningFeature', () => { state: { pagination: { pageSize: 5, - pageIndex: 1, //pinned rows will be on page 0, but this is page 1 + pageIndex: 1, // pinned rows will be on page 0, but this is page 1 }, rowPinning: { bottom: [], @@ -161,11 +174,12 @@ describe('rowPinningFeature', () => { }) it('should return correct top rows', () => { - const data = makeData(10) - const columns = generateColumns(data) - const table = constructTable({ - _features: { rowPinningFeature, rowPaginationFeature }, + _features: { + rowPinningFeature, + rowPaginationFeature, + ...coreFeatures, + }, _rowModels: { paginatedRowModel: createPaginatedRowModel(), }, @@ -177,7 +191,7 @@ describe('rowPinningFeature', () => { state: { pagination: { pageSize: 5, - pageIndex: 0, //pinned rows will be on page 0 + pageIndex: 0, // pinned rows will be on page 0 }, rowPinning: { bottom: [], @@ -190,15 +204,16 @@ describe('rowPinningFeature', () => { const result = table.getTopRows() expect(result.length).toBe(2) - expect(result[0].id).toBe('1') - expect(result[1].id).toBe('3') + expect(result[0]?.id).toBe('1') + expect(result[1]?.id).toBe('3') }) it('should return correct bottom rows', () => { - const data = makeData(10) - const columns = generateColumns(data) - const table = constructTable({ - _features: { rowPinningFeature, rowPaginationFeature }, + _features: { + rowPinningFeature, + rowPaginationFeature, + ...coreFeatures, + }, _rowModels: { paginatedRowModel: createPaginatedRowModel(), }, @@ -210,7 +225,7 @@ describe('rowPinningFeature', () => { state: { pagination: { pageSize: 5, - pageIndex: 0, //pinned rows will be on page 0 + pageIndex: 0, // pinned rows will be on page 0 }, rowPinning: { bottom: ['1', '3'], @@ -223,8 +238,8 @@ describe('rowPinningFeature', () => { const result = table.getBottomRows() expect(result.length).toBe(2) - expect(result[0].id).toBe('1') - expect(result[1].id).toBe('3') + expect(result[0]?.id).toBe('1') + expect(result[1]?.id).toBe('3') }) }) describe('getCenterRows', () => { @@ -233,7 +248,11 @@ describe('rowPinningFeature', () => { const columns = generateColumns(data) const table = constructTable({ - _features: { rowPinningFeature, rowPaginationFeature }, + _features: { + rowPinningFeature, + rowPaginationFeature, + ...coreFeatures, + }, _rowModels: { paginatedRowModel: createPaginatedRowModel(), }, @@ -258,8 +277,8 @@ describe('rowPinningFeature', () => { const result = table.getCenterRows() expect(result.length).toBe(2) - expect(result[0].id).toBe('0') // 0 and 5 are the only rows not pinned - expect(result[1].id).toBe('5') + expect(result[0]?.id).toBe('0') // 0 and 5 are the only rows not pinned + expect(result[1]?.id).toBe('5') }) }) }) diff --git a/packages/table-core/tests/RowSelection.test.ts b/packages/table-core/tests/RowSelection.test.ts index 439e693361..e6213d5f7b 100644 --- a/packages/table-core/tests/RowSelection.test.ts +++ b/packages/table-core/tests/RowSelection.test.ts @@ -1,24 +1,26 @@ import { describe, expect, it } from 'vitest' import { - ColumnDef, - createColumnHelper, constructTable, - createCoreRowModel, + coreFeatures, + createColumnHelper, rowSelectionFeature, } from '../src' import * as RowSelectionUtils from '../src/features/row-selection/rowSelectionFeature.utils' -import { makeData, Person } from './makeTestData' +import { makeData } from './makeTestData' +import type { Person } from './makeTestData' +import type { ColumnDef } from '../src' type personKeys = keyof Person -type PersonColumn = ColumnDef< - any, - Person, - string | number | Person[] | undefined -> +type PersonColumn = ColumnDef -function generateColumns(people: Person[]): PersonColumn[] { +function generateColumns(people: Array): Array { const columnHelper = createColumnHelper() const person = people[0] + + if (!person) { + return [] + } + return Object.keys(person).map((key) => { const typedKey = key as personKeys return columnHelper.accessor(typedKey, { id: typedKey }) @@ -32,7 +34,7 @@ describe('rowSelectionFeature', () => { const columns = generateColumns(data) const table = constructTable({ - _features: { rowSelectionFeature }, + _features: { rowSelectionFeature, ...coreFeatures }, _rowModels: {}, enableRowSelection: true, onStateChange() {}, @@ -49,7 +51,7 @@ describe('rowSelectionFeature', () => { }) const rowModel = table.getCoreRowModel() - const result = RowSelectionUtils.selectRowsFn(table, rowModel) + const result = RowSelectionUtils.selectRowsFn(rowModel) expect(result.rows.length).toBe(2) expect(result.flatRows.length).toBe(2) @@ -62,7 +64,7 @@ describe('rowSelectionFeature', () => { const columns = generateColumns(data) const table = constructTable({ - _features: { rowSelectionFeature }, + _features: { rowSelectionFeature, ...coreFeatures }, _rowModels: {}, enableRowSelection: true, onStateChange() {}, @@ -79,9 +81,9 @@ describe('rowSelectionFeature', () => { }) const rowModel = table.getCoreRowModel() - const result = RowSelectionUtils.selectRowsFn(table, rowModel) + const result = RowSelectionUtils.selectRowsFn(rowModel) - expect(result.rows[0].subRows.length).toBe(1) + 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') @@ -92,7 +94,7 @@ describe('rowSelectionFeature', () => { const columns = generateColumns(data) const table = constructTable({ - _features: { rowSelectionFeature }, + _features: { rowSelectionFeature, ...coreFeatures }, _rowModels: {}, enableRowSelection: true, onStateChange() {}, @@ -106,7 +108,7 @@ describe('rowSelectionFeature', () => { }) const rowModel = table.getCoreRowModel() - const result = RowSelectionUtils.selectRowsFn(table, rowModel) + const result = RowSelectionUtils.selectRowsFn(rowModel) expect(result.rows.length).toBe(0) expect(result.flatRows.length).toBe(0) @@ -115,43 +117,101 @@ describe('rowSelectionFeature', () => { }) 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 = { - '123': true, - '456': false, - } + const data = makeData(3) + const columns = generateColumns(data) - const result = RowSelectionUtils.isRowSelected(row, selection) + const table = constructTable({ + _features: { rowSelectionFeature, ...coreFeatures }, + _rowModels: {}, + enableRowSelection: true, + onStateChange() {}, + renderFallbackValue: '', + data, + state: { + rowSelection: { + '123': true, + '456': false, + }, + }, + columns, + }) + + const row = { id: '123', data: {}, table } as any + + const result = RowSelectionUtils.isRowSelected(row) 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 = { - '123': true, - '456': false, - } + const data = makeData(3) + const columns = generateColumns(data) - const result = RowSelectionUtils.isRowSelected(row, selection) + const table = constructTable({ + _features: { rowSelectionFeature, ...coreFeatures }, + _rowModels: {}, + enableRowSelection: true, + onStateChange() {}, + renderFallbackValue: '', + data, + state: { + rowSelection: { + '123': true, + '456': false, + }, + }, + columns, + }) + + const row = { id: '456', data: {}, table } as any + + const result = RowSelectionUtils.isRowSelected(row) 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 = { - '123': true, - '456': false, - } + const data = makeData(3) + const columns = generateColumns(data) + + const table = constructTable({ + _features: { rowSelectionFeature, ...coreFeatures }, + _rowModels: {}, + enableRowSelection: true, + onStateChange() {}, + renderFallbackValue: '', + data, + state: { + rowSelection: { + '123': true, + '456': false, + }, + }, + columns, + }) - const result = RowSelectionUtils.isRowSelected(row, selection) + const row = { id: '789', data: {}, table } as any + + const result = RowSelectionUtils.isRowSelected(row) expect(result).toEqual(false) }) it('should return false if selection is an empty object', () => { - const row = { id: '789', data: {} } as any - const selection: Record = {} + const data = makeData(3) + const columns = generateColumns(data) + + const table = constructTable({ + _features: { rowSelectionFeature, ...coreFeatures }, + _rowModels: {}, + enableRowSelection: true, + onStateChange() {}, + renderFallbackValue: '', + data, + state: {}, + columns, + }) + + const row = { id: '789', data: {}, table } as any - const result = RowSelectionUtils.isRowSelected(row, selection) + const result = RowSelectionUtils.isRowSelected(row) expect(result).toEqual(false) }) }) @@ -161,7 +221,7 @@ describe('rowSelectionFeature', () => { const columns = generateColumns(data) const table = constructTable({ - _features: { rowSelectionFeature }, + _features: { rowSelectionFeature, ...coreFeatures }, _rowModels: {}, enableRowSelection: true, onStateChange() {}, @@ -171,13 +231,9 @@ describe('rowSelectionFeature', () => { columns, }) - const firstRow = table.getCoreRowModel().rows[0] + const firstRow = table.getCoreRowModel().rows[0]! - const result = RowSelectionUtils.isSubRowSelected( - firstRow, - table.getState().rowSelection, - table, - ) + const result = RowSelectionUtils.isSubRowSelected(firstRow) expect(result).toEqual(false) }) @@ -187,7 +243,7 @@ describe('rowSelectionFeature', () => { const columns = generateColumns(data) const table = constructTable({ - _features: { rowSelectionFeature }, + _features: { rowSelectionFeature, ...coreFeatures }, _rowModels: {}, enableRowSelection: true, onStateChange() {}, @@ -200,13 +256,9 @@ describe('rowSelectionFeature', () => { columns, }) - const firstRow = table.getCoreRowModel().rows[0] + const firstRow = table.getCoreRowModel().rows[0]! - const result = RowSelectionUtils.isSubRowSelected( - firstRow, - table.getState().rowSelection, - table, - ) + const result = RowSelectionUtils.isSubRowSelected(firstRow) expect(result).toEqual(false) }) @@ -216,7 +268,7 @@ describe('rowSelectionFeature', () => { const columns = generateColumns(data) const table = constructTable({ - _features: { rowSelectionFeature }, + _features: { rowSelectionFeature, ...coreFeatures }, _rowModels: {}, enableRowSelection: true, onStateChange() {}, @@ -231,13 +283,9 @@ describe('rowSelectionFeature', () => { columns, }) - const firstRow = table.getCoreRowModel().rows[0] + const firstRow = table.getCoreRowModel().rows[0]! - const result = RowSelectionUtils.isSubRowSelected( - firstRow, - table.getState().rowSelection, - table, - ) + const result = RowSelectionUtils.isSubRowSelected(firstRow) expect(result).toEqual('some') }) @@ -247,7 +295,7 @@ describe('rowSelectionFeature', () => { const columns = generateColumns(data) const table = constructTable({ - _features: { rowSelectionFeature }, + _features: { rowSelectionFeature, ...coreFeatures }, _rowModels: {}, enableRowSelection: true, onStateChange() {}, @@ -263,13 +311,9 @@ describe('rowSelectionFeature', () => { columns, }) - const firstRow = table.getCoreRowModel().rows[0] + const firstRow = table.getCoreRowModel().rows[0]! - const result = RowSelectionUtils.isSubRowSelected( - firstRow, - table.getState().rowSelection, - table, - ) + const result = RowSelectionUtils.isSubRowSelected(firstRow) expect(result).toEqual('all') }) @@ -278,7 +322,7 @@ describe('rowSelectionFeature', () => { const columns = generateColumns(data) const table = constructTable({ - _features: { rowSelectionFeature }, + _features: { rowSelectionFeature, ...coreFeatures }, _rowModels: {}, enableRowSelection: (row) => row.index === 0, // only first row is selectable (of 2 sub-rows) onStateChange() {}, @@ -293,13 +337,9 @@ describe('rowSelectionFeature', () => { columns, }) - const firstRow = table.getCoreRowModel().rows[0] + const firstRow = table.getCoreRowModel().rows[0]! - const result = RowSelectionUtils.isSubRowSelected( - firstRow, - table.getState().rowSelection, - table, - ) + const result = RowSelectionUtils.isSubRowSelected(firstRow) expect(result).toEqual('all') }) @@ -308,7 +348,7 @@ describe('rowSelectionFeature', () => { const columns = generateColumns(data) const table = constructTable({ - _features: { rowSelectionFeature }, + _features: { rowSelectionFeature, ...coreFeatures }, _rowModels: {}, enableRowSelection: true, onStateChange() {}, @@ -323,13 +363,9 @@ describe('rowSelectionFeature', () => { columns, }) - const firstRow = table.getCoreRowModel().rows[0] + const firstRow = table.getCoreRowModel().rows[0]! - const result = RowSelectionUtils.isSubRowSelected( - firstRow, - table.getState().rowSelection, - table, - ) + const result = RowSelectionUtils.isSubRowSelected(firstRow) expect(result).toEqual('some') }) diff --git a/packages/table-core/tests/getGroupedRowModel.test.ts b/packages/table-core/tests/getGroupedRowModel.test.ts index 92bce5186f..0447113fdb 100644 --- a/packages/table-core/tests/getGroupedRowModel.test.ts +++ b/packages/table-core/tests/getGroupedRowModel.test.ts @@ -1,24 +1,27 @@ import { describe, expect, it } from 'vitest' import { + aggregationFns, + columnGroupingFeature, constructTable, - ColumnDef, - createCoreRowModel, + coreFeatures, createGroupedRowModel, - columnGroupingFeature, } from '../src' import { createColumnHelper } from '../src/helpers/columnHelper' -import { makeData, Person } from './makeTestData' +import { makeData } from './makeTestData' +import type { Person } from './makeTestData' +import type { ColumnDef } from '../src' type personKeys = keyof Person -type PersonColumn = ColumnDef< - any, - Person, - string | number | Person[] | undefined -> +type PersonColumn = ColumnDef -function generateColumns(people: Person[]): PersonColumn[] { +function generateColumns(people: Array): Array { const columnHelper = createColumnHelper() const person = people[0] + + if (!person) { + return [] + } + return Object.keys(person).map((key) => { const typedKey = key as personKeys return columnHelper.accessor(typedKey, { id: typedKey }) @@ -37,9 +40,9 @@ describe('#getGroupedRowModel', () => { data.forEach((p) => (p.age = 123)) const table = constructTable({ - _features: { columnGroupingFeature }, + _features: { columnGroupingFeature, ...coreFeatures }, _rowModels: { - groupedRowModel: createGroupedRowModel(), + groupedRowModel: createGroupedRowModel(aggregationFns), }, onStateChange() {}, renderFallbackValue: '', @@ -50,12 +53,13 @@ describe('#getGroupedRowModel', () => { const groupedById = table.getGroupedRowModel().rowsById const end = new Date() - expect(groupedById['firstName:Fixed'].leafRows.length).toEqual(50000) + expect(groupedById['firstName:Fixed']?.getLeafRows().length).toEqual(50002) expect( - groupedById['firstName:Fixed>lastName:Name'].leafRows.length, - ).toEqual(50000) + groupedById['firstName:Fixed>lastName:Name']?.getLeafRows().length, + ).toEqual(50001) expect( - groupedById['firstName:Fixed>lastName:Name>age:123'].leafRows.length, + groupedById['firstName:Fixed>lastName:Name>age:123']?.getLeafRows() + .length, ).toEqual(50000) expect(end.valueOf() - start.valueOf()).toBeLessThan(5000) }) diff --git a/packages/table-core/tsconfig.json b/packages/table-core/tsconfig.json index eb63835950..44533f71cd 100644 --- a/packages/table-core/tsconfig.json +++ b/packages/table-core/tsconfig.json @@ -1,4 +1,4 @@ { "extends": "../../tsconfig.json", - "include": ["src", "eslint.config.js", "vite.config.ts"] + "include": ["src", "eslint.config.js", "vite.config.ts", "tests"] }