Skip to content

Commit

Permalink
filterFns unit tests and rearrange some fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Dec 24, 2024
1 parent cf0972d commit bce4188
Show file tree
Hide file tree
Showing 15 changed files with 537 additions and 124 deletions.
45 changes: 40 additions & 5 deletions packages/table-core/src/fns/filterFns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export const filterFn_includesStringSensitive: FilterFn<any, any> = <
columnId: string,
filterValue: string,
) => {
return Boolean(row.getValue(columnId)?.toString().includes(filterValue))
return Boolean(
row.getValue(columnId)?.toString().includes(filterValue.toString()),
)
}

filterFn_includesStringSensitive.autoRemove = (val: any) => testFalsy(val)
Expand Down Expand Up @@ -96,6 +98,22 @@ export const filterFn_equalsString: FilterFn<any, any> = <

filterFn_equalsString.autoRemove = (val: any) => testFalsy(val)

/**
* Filter function for checking if a string is exactly equal to a given string. (Case-sensitive)
*/
export const filterFn_equalsStringSensitive: FilterFn<any, any> = <
TFeatures extends TableFeatures,
TData extends RowData,
>(
row: Row<TFeatures, TData>,
columnId: string,
filterValue: string,
) => {
return row.getValue(columnId)?.toString() === filterValue
}

filterFn_equalsStringSensitive.autoRemove = (val: any) => testFalsy(val)

// Number filters

/**
Expand Down Expand Up @@ -277,6 +295,20 @@ filterFn_inNumberRange.autoRemove = (val: any) =>

// Array filters

/**
* Filter function for checking if an array has a given value.
*/
export const filterFn_arrHas: FilterFn<any, any> = <
TFeatures extends TableFeatures,
TData extends RowData,
>(
row: Row<TFeatures, TData>,
columnId: string,
filterValue: Array<unknown>,
) => {
return filterValue.some((val) => row.getValue<unknown>(columnId) === val)
}

/**
* Filter function for checking if an array includes a given value.
*/
Expand All @@ -286,11 +318,13 @@ export const filterFn_arrIncludes: FilterFn<any, any> = <
>(
row: Row<TFeatures, TData>,
columnId: string,
filterValue: unknown,
filterValue: Array<unknown>,
) => {
const value = row.getValue<Array<unknown>>(columnId)
if (!Array.isArray(value)) return false
return value.includes(filterValue)
return filterValue.some((val) =>
(row.getValue<unknown>(columnId) as Array<unknown> | string).includes(
val as any,
),
)
}

filterFn_arrIncludes.autoRemove = (val: any) => testFalsy(val) || !val?.length
Expand Down Expand Up @@ -338,6 +372,7 @@ filterFn_arrIncludesSome.autoRemove = (val: any) =>
export const filterFns = {
arrIncludes: filterFn_arrIncludes,
arrIncludesAll: filterFn_arrIncludesAll,
arrHas: filterFn_arrHas,
arrIncludesSome: filterFn_arrIncludesSome,
between: filterFn_between,
betweenInclusive: filterFn_betweenInclusive,
Expand Down
17 changes: 0 additions & 17 deletions packages/table-core/tests/fixtures/data/generateColumns.ts

This file was deleted.

20 changes: 20 additions & 0 deletions packages/table-core/tests/fixtures/data/generateTestColumnDefs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createColumnHelper } from '../../../src'
import type { Person, PersonColumn, PersonKeys } from './types'
import type { TableFeatures } from '../../../src'

export function generateTestColumnDefs<TFeatures extends TableFeatures>(
people: Array<Person>,
): Array<PersonColumn<TFeatures>> {
const columnHelper = createColumnHelper<TFeatures, Person>()
const person = people[0]

if (!person) {
return []
}

return Object.keys(person).map((key) => {
const typedKey = key as PersonKeys

return columnHelper.accessor(typedKey, { id: typedKey } as any)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function createPerson(): Person {
* e.g. makeData(3, 2) creates 3 parent rows with 2 sub-rows each
* @returns An array of Person objects with optional nested subRows based on the provided lengths
*/
export function makeData(...lengths: Array<number>) {
export function generateTestData(...lengths: Array<number>) {
const makeDataLevel = (depth = 0): Array<Person> => {
const len = lengths[depth]

Expand All @@ -40,3 +40,35 @@ export function makeData(...lengths: Array<number>) {

return makeDataLevel()
}

export function getStaticTestData() {
return [
{
id: '1',
firstName: 'John',
lastName: 'Doe',
age: 30,
visits: 100,
progress: 50,
status: 'relationship',
},
{
id: '2',
firstName: 'Jane',
lastName: 'Smith',
age: 25,
visits: 200,
progress: 75,
status: 'complicated',
},
{
id: '3',
firstName: 'Alice',
lastName: 'Johnson',
age: 35,
visits: 150,
progress: 60,
status: 'single',
},
] as const satisfies Array<Person>
}
8 changes: 6 additions & 2 deletions packages/table-core/tests/fixtures/data/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type { ColumnDef } from '../../../src'
import type { ColumnDef, TableFeatures } from '../../../src'

export type PersonKeys = keyof Person
export type PersonColumn = ColumnDef<any, Person, any>
export type PersonColumn<TFeatures extends TableFeatures> = ColumnDef<
TFeatures,
Person,
any
>

export type Person = {
id: string
Expand Down
54 changes: 0 additions & 54 deletions packages/table-core/tests/helpers/createTestTable.ts

This file was deleted.

59 changes: 59 additions & 0 deletions packages/table-core/tests/helpers/generateTestRows.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {
generateTestTableFromData,
generateTestTableWithData,
generateTestTableWithDataAndState,
generateTestTableWithStateFromData,
} from './generateTestTable'
import type { TableFeatures, TableOptions } from '../../src'
import type { Person } from '../fixtures/data/types'

export function generateTestRowsWithData<TFeatures extends TableFeatures>(
lengths: Array<number> | number = 10,
options?: Omit<TableOptions<TFeatures, Person>, 'data' | 'columns'> & {
_features?: TFeatures
},
) {
const testTable = generateTestTableWithData<TFeatures>(lengths, options)
return testTable.getRowModel().rows
}

export function generateTestRowsFromData<TFeatures extends TableFeatures>(
data: Array<Person>,
options?: Omit<TableOptions<TFeatures, Person>, 'data' | 'columns'> & {
_features?: TFeatures
},
) {
const testTable = generateTestTableFromData<TFeatures>(data, options)
return testTable.getRowModel().rows
}

export function generateTestRowsWithState<TFeatures extends TableFeatures>(
lengths: Array<number> | number = 10,
options?: Omit<
TableOptions<TFeatures, Person>,
'data' | 'columns' | 'onStateChange'
> & {
_features?: TFeatures
},
) {
const testTable = generateTestTableWithDataAndState<TFeatures>(
lengths,
options,
)
return testTable.getRowModel().rows
}

export function generateTestRowsWithStateFromData<
TFeatures extends TableFeatures,
>(
data: Array<Person>,
options?: Omit<
TableOptions<TFeatures, Person>,
'data' | 'columns' | 'onStateChange'
> & {
_features?: TFeatures
},
) {
const testTable = generateTestTableWithStateFromData<TFeatures>(data, options)
return testTable.getRowModel().rows
}
Loading

0 comments on commit bce4188

Please sign in to comment.