Skip to content

Commit 3fead84

Browse files
committed
tests: add tests for core apis and properties are present
1 parent ceff666 commit 3fead84

File tree

10 files changed

+215
-4
lines changed

10 files changed

+215
-4
lines changed

packages/table-core/src/core/cells/Cells.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assignAPIs, getMemoOptions, memo } from '../../utils'
1+
import { assignAPIs } from '../../utils'
22
import { cell_getContext, cell_getValue, cell_renderValue } from './Cells.utils'
33
import type { CellData, RowData } from '../../types/type-utils'
44
import type { TableFeature, TableFeatures } from '../../types/TableFeatures'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { _createCell } from './createCell'
3+
import { Cells } from './Cells'
4+
import type { Row } from '../../types/Row'
5+
import type { Column } from '../../types/Column'
6+
import type { Table } from '../../types/Table'
7+
8+
describe('createCell', () => {
9+
it('should populate the cell with all core cell APIs and properties', () => {
10+
const column = { id: 'test-column' } as Column<any, any, any>
11+
const row = { id: 'test-row' } as Row<any, any>
12+
const table = { _features: { Cells }, options: {} } as Table<any, any>
13+
const coreCell = _createCell(column, row, table)
14+
15+
expect(coreCell).toBeDefined()
16+
expect(coreCell).toHaveProperty('column')
17+
expect(coreCell).toHaveProperty('id')
18+
expect(coreCell).toHaveProperty('row')
19+
expect(coreCell).toHaveProperty('getContext')
20+
expect(coreCell).toHaveProperty('getValue')
21+
expect(coreCell).toHaveProperty('renderValue')
22+
23+
expect(coreCell.id).toBe(`${row.id}_${column.id}`)
24+
expect(coreCell.column).toBe(column)
25+
expect(coreCell.row).toBe(row)
26+
})
27+
})

packages/table-core/src/core/columns/Columns.types.ts

+10
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,17 @@ export interface Table_Columns<
9797
TFeatures extends TableFeatures,
9898
TData extends RowData,
9999
> {
100+
/**
101+
* Returns a map of all flat columns by their ID.
102+
* @link [API Docs](https://tanstack.com/table/v8/docs/api/core/table#getallflatcolumnsbyid)
103+
* @link [Guide](https://tanstack.com/table/v8/docs/guide/tables)
104+
*/
100105
getAllFlatColumnsById: () => Record<string, Column<TFeatures, TData, unknown>>
106+
/**
107+
* Returns the default column options to use for all column defs supplied to the table.
108+
* @link [API Docs](https://tanstack.com/table/v8/docs/api/core/table#getdefaultcolumndef)
109+
* @link [Guide](https://tanstack.com/table/v8/docs/guide/tables)
110+
*/
101111
getDefaultColumnDef: () => Partial<ColumnDef<TFeatures, TData, unknown>>
102112
/**
103113
* Returns all columns in the table in their normalized and nested hierarchy.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { Columns } from './Columns'
3+
import { _createColumn } from './createColumn'
4+
import type { ColumnDef } from '../../types/ColumnDef'
5+
import type { Table } from '../../types/Table'
6+
7+
describe('createColumn', () => {
8+
it('should create a column with all core column APIs and properties', () => {
9+
const table = { _features: { Columns }, options: {} } as Table<any, any>
10+
const columnDef = {
11+
id: 'test-column',
12+
accessorKey: 'test-accessor-key',
13+
} as ColumnDef<any, any, any>
14+
const depth = 0
15+
const parent = undefined
16+
17+
const column = _createColumn(table, columnDef, depth, parent)
18+
19+
expect(column).toBeDefined()
20+
expect(column).toHaveProperty('accessorFn')
21+
expect(column).toHaveProperty('columnDef')
22+
expect(column).toHaveProperty('columns')
23+
expect(column).toHaveProperty('depth')
24+
expect(column).toHaveProperty('id')
25+
expect(column).toHaveProperty('parent')
26+
expect(column).toHaveProperty('getFlatColumns')
27+
expect(column).toHaveProperty('getLeafColumns')
28+
29+
expect(column.id).toBe(columnDef.id)
30+
expect(column.depth).toBe(depth)
31+
expect(column.parent).toBe(parent)
32+
})
33+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { Headers } from './Headers'
3+
import { _createHeader } from './createHeader'
4+
import type { Column } from '../../types/Column'
5+
import type { Table } from '../../types/Table'
6+
7+
describe('createHeader', () => {
8+
it('should create a column with all core column APIs and properties', () => {
9+
const table = { _features: { Headers }, options: {} } as Table<any, any>
10+
const column = {
11+
id: 'test-column',
12+
columnDef: { id: 'test-column-def' },
13+
} as Column<any, any, any>
14+
const index = 0
15+
const depth = 0
16+
17+
const header = _createHeader(table, column, {
18+
index,
19+
depth,
20+
})
21+
22+
expect(header).toBeDefined()
23+
expect(header).toHaveProperty('colSpan')
24+
expect(header).toHaveProperty('column')
25+
expect(header).toHaveProperty('depth')
26+
expect(header).toHaveProperty('headerGroup')
27+
expect(header).toHaveProperty('id')
28+
expect(header).toHaveProperty('index')
29+
expect(header).toHaveProperty('isPlaceholder')
30+
expect(header).toHaveProperty('placeholderId')
31+
expect(header).toHaveProperty('rowSpan')
32+
expect(header).toHaveProperty('subHeaders')
33+
expect(header).toHaveProperty('getContext')
34+
expect(header).toHaveProperty('getLeafHeaders')
35+
36+
expect(header.id).toBe(column.id)
37+
})
38+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { Rows } from './Rows'
3+
import { _createRow } from './createRow'
4+
import type { Row } from '../../types/Row'
5+
import type { Table } from '../../types/Table'
6+
7+
interface Person {
8+
firstName: string
9+
}
10+
11+
describe('createRow', () => {
12+
it('should create a row with all core row APIs and properties', () => {
13+
const table = { _features: { Rows }, options: {} } as Table<any, Person>
14+
const id = 'test-row'
15+
const original = { firstName: 'Tanner' } as Person
16+
const rowIndex = 0
17+
const depth = 0
18+
const subRows = [] as Array<Row<any, Person>>
19+
const parentId = 'parent-id'
20+
21+
const row = _createRow(
22+
table,
23+
id,
24+
original,
25+
rowIndex,
26+
depth,
27+
subRows,
28+
parentId,
29+
)
30+
31+
expect(row).toBeDefined()
32+
expect(row).toHaveProperty('_uniqueValuesCache')
33+
expect(row).toHaveProperty('_valuesCache')
34+
expect(row).toHaveProperty('depth')
35+
expect(row).toHaveProperty('id')
36+
expect(row).toHaveProperty('index')
37+
expect(row).toHaveProperty('original')
38+
expect(row).toHaveProperty('parentId')
39+
expect(row).toHaveProperty('subRows')
40+
expect(row).toHaveProperty('getAllCellsByColumnId')
41+
expect(row).toHaveProperty('getAllCells')
42+
expect(row).toHaveProperty('getLeafRows')
43+
expect(row).toHaveProperty('getParentRow')
44+
expect(row).toHaveProperty('getParentRows')
45+
expect(row).toHaveProperty('getUniqueValues')
46+
expect(row).toHaveProperty('getValue')
47+
expect(row).toHaveProperty('renderValue')
48+
49+
expect(row.id).toBe(id)
50+
expect(row.original).toBe(original)
51+
expect(row.index).toBe(rowIndex)
52+
expect(row.depth).toBe(depth)
53+
expect(row.subRows).toBe(subRows)
54+
expect(row.parentId).toBe(parentId)
55+
})
56+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { tableFeatures } from '../../helpers/tableFeatures'
3+
import { _createTable, coreFeatures } from './createTable'
4+
5+
describe('createTable', () => {
6+
it('should create a table with all core table APIs and properties', () => {
7+
const table = _createTable({
8+
_features: coreFeatures,
9+
columns: [],
10+
data: [],
11+
})
12+
13+
expect(table).toBeDefined()
14+
// core table properties
15+
expect(table).toHaveProperty('_features')
16+
expect(table).toHaveProperty('_rowModels')
17+
expect(table).toHaveProperty('initialState')
18+
expect(table).toHaveProperty('options')
19+
20+
// column related table APIs
21+
expect(table).toHaveProperty('getAllFlatColumnsById')
22+
expect(table).toHaveProperty('getDefaultColumnDef')
23+
expect(table).toHaveProperty('getAllColumns')
24+
expect(table).toHaveProperty('getAllFlatColumns')
25+
expect(table).toHaveProperty('getAllLeafColumns')
26+
expect(table).toHaveProperty('getColumn')
27+
28+
// header related table APIs
29+
expect(table).toHaveProperty('getHeaderGroups')
30+
expect(table).toHaveProperty('getFooterGroups')
31+
expect(table).toHaveProperty('getFlatHeaders')
32+
expect(table).toHaveProperty('getLeafHeaders')
33+
34+
// row related table APIs
35+
expect(table).toHaveProperty('getRowId')
36+
expect(table).toHaveProperty('getRow')
37+
38+
// table APIs
39+
expect(table).toHaveProperty('_queue')
40+
expect(table).toHaveProperty('getCoreRowModel')
41+
expect(table).toHaveProperty('getRowModel')
42+
expect(table).toHaveProperty('getState')
43+
expect(table).toHaveProperty('reset')
44+
expect(table).toHaveProperty('setState')
45+
expect(table).toHaveProperty('setOptions')
46+
})
47+
})

packages/table-core/src/helpers/tableFeatures.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { TableFeatures } from '../types/TableFeatures'
1+
import type { CoreTableFeatures, TableFeatures } from '../types/TableFeatures'
22

33
/**
44
* A helper function to help define the features that are to be imported and applied to a table instance.

packages/table-core/src/types/TableFeatures.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export interface CoreTableFeatures {
6161
Cells: TableFeature
6262
}
6363

64-
export interface TableFeatures {
64+
export interface TableFeatures extends CoreTableFeatures {
6565
ColumnFaceting?: TableFeature
6666
ColumnFiltering?: TableFeature
6767
ColumnGrouping?: TableFeature

packages/table-core/vite.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import packageJson from './package.json'
55
const config = defineConfig({
66
test: {
77
name: packageJson.name,
8-
dir: './tests',
8+
dir: './',
99
watch: false,
1010
environment: 'jsdom',
1111
setupFiles: ['./tests/test-setup.ts'],

0 commit comments

Comments
 (0)