Skip to content

Commit

Permalink
test: add test helpers afterRow
Browse files Browse the repository at this point in the history
  • Loading branch information
santiago.trigo committed Sep 4, 2024
1 parent d028335 commit c4ef013
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 102 deletions.
15 changes: 15 additions & 0 deletions packages/table/src/helpers/afterRow/addAfterRowsToData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,18 @@ export const addAfterRowsToData = (data: TData) => {
}, [] as TData);
return [newData, afterRowIds];
};

export const addAfterRowToData = (data: TData, id: string | number) =>
data.reduce(
(prev, row) =>
row.id === id
? prev.concat([row].concat([{ ...row, id: `afterRow-${id}` }]))
: prev.concat([row]),
[] as TData,
);

export const deleteAfterRowToDataById = (data: TData, id: string) =>
data.filter((d) => d.id !== id);

export const findDataById = (data: TData, id: string) =>
data.find((d) => d.id === id);
83 changes: 83 additions & 0 deletions packages/table/src/helpers/afterRow/addAfterRowsToRowDefs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { describe, test, expect } from 'vitest';

import { TRowDef } from '../../declarations';
import {
addAfterRowToRowDefs,
getRowDef,
setHideRowDef,
} from './addAfterRowsToRowDefs';

describe('Add afterrow to row defs', () => {
describe('getRowDef', () => {
const cases: [string, TRowDef[], string, TRowDef][] = [
['empty row defs return undefined', [], '1', undefined],
['id null return undefined', [{ id: '1' }], null, undefined],
['rowDef null return undefined', null, '1', undefined],
['return element rowDef', [{ id: '1' }], '1', { id: '1' }],
['id not find in rowDef', [{ id: '1' }], '2', undefined],
];

test.each(cases)('%s', (_title, rowDefs, id, expected) => {
expect(getRowDef(rowDefs, id)).toEqual(expected);
});
});

describe('addAfterRowToRowDefs', () => {
const cases: [string, TRowDef[], string, TRowDef[]][] = [
[
'add element with rowdefs empty',
[],
'1',
[{ id: `afterRow-1`, hide: false }],
],
[
'add element with rowdefs > 0',
[{ id: `afterRow-1`, hide: false }],
'2',
[
{ id: `afterRow-1`, hide: false },
{ id: `afterRow-2`, hide: false },
],
],
['add element with rowdefs empty', null, '1', undefined],
];

test.each(cases)('%s', (_title, rowDefs, id, expected) => {
expect(addAfterRowToRowDefs(rowDefs, id, () => null, 13)).toMatchObject(
expected,
);
});
});

describe('setHideRowDef', () => {
const cases: [string, TRowDef[], string, boolean, TRowDef[]][] = [
['empty rowdefs return empty rowdefs', [], '1', true, []],
['rowdefs null return undefined', null, '1', true, undefined],
[
'changed visible to no visible',
[{ id: `afterRow-1`, hide: false }],
'1',
false,
[{ id: `afterRow-1`, hide: true }],
],
[
'changed no visible to visible',
[{ id: `afterRow-1`, hide: true }],
'1',
false,
[{ id: `afterRow-1`, hide: true }],
],
[
'id and isOpened null return same rowdefs',
[{ id: `afterRow-1`, hide: true }],
null,
null,
[{ id: `afterRow-1`, hide: true }],
],
];

test.each(cases)('%s', (_title, rowDefs, id, isOpened, expected) => {
expect(setHideRowDef(rowDefs, id, isOpened)).toEqual(expected);
});
});
});
51 changes: 45 additions & 6 deletions packages/table/src/helpers/afterRow/addAfterRowsToRowDefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,52 @@ import * as React from 'react';
import { TCellRenderer, TRowDef } from '../../declarations';

export const addAfterRowsToRowDefs = (
rowDefs: TRowDef[],
ids: string[],
afterRowRenderer: React.FC<TCellRenderer>,
afterRowHeight: number
afterRowRenderer: React.FC<TCellRenderer>
| (({ value, colDef, rowIndex, row }: TCellRenderer) => React.ReactNode),
afterRowHeight: number,
) =>
ids.map((id) => ({
id,
hide: true,
ids.map((id) => {
const rowDef = getRowDef(rowDefs, id);

const defaultRowDef = {
id,
hide: true,
cellRenderer: afterRowRenderer,
height: afterRowHeight,
};

return rowDef
? { ...defaultRowDef, ...rowDef }
: {
id,
hide: true,
cellRenderer: afterRowRenderer,
height: afterRowHeight,
};
}) as TRowDef[];

export const getRowDef = (rowDefs: TRowDef[], id: string) =>
rowDefs?.find((r) => r.id === id);

export const addAfterRowToRowDefs = (
rowDefs: TRowDef[],
id,
afterRowRenderer,
afterRowHeight,
) =>
rowDefs?.concat({
id: `afterRow-${id}`,
hide: false,
cellRenderer: afterRowRenderer,
height: afterRowHeight,
})) as TRowDef[];
});

export const setHideRowDef = (rowDefs: TRowDef[], id, isOpened) =>
rowDefs?.map((rowDef: TRowDef) => {
if (rowDef.id === `afterRow-${id}`) {
rowDef.hide = !isOpened;
}
return rowDef;
});
14 changes: 14 additions & 0 deletions packages/table/src/hooks/useAfterRow/selection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, test, expect } from 'vitest';

import { updateSelection } from './selection';

describe('updateSelection', () => {
const cases: [string, string[], string, string[]][] = [
['return array with id', [], '1', ['1']],
['return array without id', ['1'], '1', []],
];

test.each(cases)('%s', (_title, selection, id, expected) => {
expect(updateSelection(selection)(id)).toEqual(expected);
});
});
92 changes: 0 additions & 92 deletions packages/table/src/hooks/useAfterRow/useAfterRow.test.ts

This file was deleted.

74 changes: 71 additions & 3 deletions packages/table/src/hooks/useAfterRow/useAfterRow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,28 @@ import * as React from 'react';
import { RowGroupingRenderer } from '../../renderers';
import { TRowGroupingContext } from '../../facade';
import { updateSelection } from './selection';
import { TColDef, TRowDef } from '../../declarations';
import { TCellRenderer, TColDef, TData, TRowDef } from '../../declarations';
import {
addAfterRowToData,
addAfterRowToRowDefs,
deleteAfterRowToDataById,
findDataById,
getRowDef,
setHideRowDef,
} from '../../helpers';

export const useAfterRow = ({
export const useRenderAfterRow = ({
rowDefs,
initialSelection,
onRowDefsChange,
colDefs: originalColDefs,
}: {
rowDefs: TRowDef[];
initialSelection: string[];
onRowDefsChange: (rowDefs: TRowDef[]) => void;
colDefs: TColDef[];
}) => {
const [selection, setSelection] = React.useState([]);
const [selection, setSelection] = React.useState(initialSelection);

const colDefs = [
{
Expand Down Expand Up @@ -45,3 +55,61 @@ export const useAfterRow = ({
colDefs,
};
};

export const useOnDemandAfterRow = ({
rowDefs,
onRowDefsChange,
colDefs: originalColDefs,
data,
onDataChange,
afterRowRenderer,
afterRowHeight,
}: {
rowDefs: TRowDef[];
onRowDefsChange: (rowDefs: TRowDef[]) => void;
colDefs: TColDef[];
data: TData[];
onDataChange: (data: TData[]) => void;
afterRowRenderer: React.FC<TCellRenderer>
| (({ value, colDef, rowIndex, row }: TCellRenderer) => React.ReactNode);
afterRowHeight: React.CSSProperties['height'];
}) => {
const [selection, setSelection] = React.useState([]);

const colDefs = [
{
id: 'rowGrouping',
cellRenderer: RowGroupingRenderer,
width: 46,
context: {
selection,
onClick: (rowId) => {
const nextSelection = updateSelection(selection)(rowId as string);
setSelection(nextSelection);
const isOpened = nextSelection.includes(rowId);

onRowDefsChange(
!getRowDef(rowDefs, `afterRow-${rowId}`)
? addAfterRowToRowDefs(
rowDefs,
rowId,
afterRowRenderer,
afterRowHeight,
)
: setHideRowDef(rowDefs, rowId, isOpened),
);
onDataChange(
findDataById(data, `afterRow-${rowId}`)
? deleteAfterRowToDataById(data, `afterRow-${rowId}`)
: addAfterRowToData(data, rowId),
);
},
} as TRowGroupingContext,
},
...originalColDefs,
];

return {
colDefs,
};
};
2 changes: 1 addition & 1 deletion packages/table/src/hooks/useTableVirtualizationRow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useVirtualizer } from '@tanstack/react-virtual';

import { TableContext } from '../context/TableContext';
import { ROW_HEIGHT_MD } from '../constants';
import { getRowDef } from '../core/Row/utils';
import { getRowDef } from '../helpers';

type TUseVirtualizationParamsRow = {
ref: React.MutableRefObject<HTMLDivElement>;
Expand Down

0 comments on commit c4ef013

Please sign in to comment.