-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(mantine, table): add missing property expanded from the state (#3730
- Loading branch information
1 parent
f602812
commit aaa55b1
Showing
4 changed files
with
113 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
packages/mantine/src/components/table/__tests__/TableCollapsibleColumn.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import {Box} from '@mantine/core'; | ||
import {ColumnDef, createColumnHelper} from '@tanstack/table-core'; | ||
import {render, screen, userEvent} from '@test-utils'; | ||
import {waitFor} from '@testing-library/react'; | ||
import {Table} from '../Table'; | ||
import {useTable} from '../use-table'; | ||
|
||
const mockData = [ | ||
{ | ||
id: 'a', | ||
name: 'John Doe', | ||
body: 'coucou', | ||
}, | ||
{ | ||
id: 'b', | ||
name: 'Jane Doe', | ||
body: 'coucou 2', | ||
}, | ||
] satisfies RowData[]; | ||
|
||
type RowData = { | ||
id: string; | ||
name: string; | ||
body: string; | ||
}; | ||
|
||
const columnHelper = createColumnHelper<RowData>(); | ||
const baseColumns: Array<ColumnDef<RowData>> = [ | ||
columnHelper.accessor('name', {header: 'Name', enableSorting: false}), | ||
Table.CollapsibleColumn as ColumnDef<RowData>, | ||
]; | ||
|
||
describe('TableCollapsibleColumn', () => { | ||
it('expands the rows when the user clicks on a cell', async () => { | ||
const user = userEvent.setup(); | ||
const Fixture = () => { | ||
const store = useTable<RowData>(); | ||
return ( | ||
<Table | ||
store={store} | ||
data={mockData} | ||
columns={baseColumns} | ||
getRowId={(datum: RowData) => datum.id} | ||
getRowExpandedContent={(datum: RowData) => ( | ||
<Box data-testid={`row-content-${datum.id}`}>{datum.body}</Box> | ||
)} | ||
/> | ||
); | ||
}; | ||
render(<Fixture />); | ||
|
||
expect(screen.queryByTestId('row-content-a')).not.toBeVisible(); | ||
expect(screen.queryByTestId('row-content-b')).not.toBeVisible(); | ||
await user.click(screen.getByRole('cell', {name: 'Jane Doe'})); | ||
|
||
await waitFor(() => expect(screen.getByTestId('row-content-b')).toBeVisible()); | ||
expect(screen.queryByTestId('row-content-a')).not.toBeVisible(); | ||
expect(screen.getByTestId('row-content-b')).toHaveTextContent('coucou 2'); | ||
}); | ||
|
||
it('expands the rows according to the initial state', () => { | ||
const Fixture = ({expanded}: {expanded: string}) => { | ||
const store = useTable<RowData>({initialState: {expanded: {[expanded]: true}}}); | ||
return ( | ||
<Table | ||
store={store} | ||
data={mockData} | ||
columns={baseColumns} | ||
getRowId={(datum: RowData) => datum.id} | ||
getRowExpandedContent={(datum: RowData) => ( | ||
<Box data-testid={`row-content-${datum.id}`}>{datum.body}</Box> | ||
)} | ||
/> | ||
); | ||
}; | ||
const {unmount} = render(<Fixture expanded="b" />); | ||
|
||
expect(screen.queryByTestId('row-content-a')).not.toBeVisible(); | ||
expect(screen.queryByTestId('row-content-b')).toBeVisible(); | ||
|
||
// unmount and mount a new component to be able to use initialState | ||
unmount(); | ||
render(<Fixture expanded="a" />); | ||
|
||
expect(screen.queryByTestId('row-content-a')).toBeVisible(); | ||
expect(screen.queryByTestId('row-content-b')).not.toBeVisible(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters