-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Table): add tests for Table component
- Loading branch information
Artur Fracala
authored and
Artur Fracala
committed
Nov 27, 2024
1 parent
ba60c5f
commit 1f6bc12
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
packages/react-components/src/components/Table/Table.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,59 @@ | ||
import { render, fireEvent, vi } from 'test-utils'; | ||
|
||
import { Table } from './Table'; | ||
import { Column } from './types'; | ||
|
||
type Data = { | ||
id: number; | ||
name: string; | ||
age: number; | ||
role: string; | ||
}; | ||
|
||
const columns: Column<Data>[] = [ | ||
{ key: 'id', header: 'ID', sortable: true }, | ||
{ key: 'name', header: 'Name', sortable: true }, | ||
{ key: 'age', header: 'Age', sortable: true }, | ||
{ key: 'role', header: 'Role', sortable: true }, | ||
]; | ||
|
||
const data: Data[] = [ | ||
{ id: 1, name: 'John Doe', age: 28, role: 'Developer' }, | ||
{ id: 2, name: 'Jane Smith', age: 34, role: 'Designer' }, | ||
{ id: 3, name: 'Alice Johnson', age: 42, role: 'Manager' }, | ||
]; | ||
|
||
describe('<Table> component', () => { | ||
const getRowId = (row: Data) => row.id; | ||
|
||
it('should render a table with provided data and columns', () => { | ||
const { getByRole, getAllByRole } = render( | ||
<Table data={data} columns={columns} getRowId={getRowId} /> | ||
); | ||
|
||
const table = getByRole('table'); | ||
expect(table).toBeInTheDocument(); | ||
|
||
const rows = getAllByRole('row'); | ||
expect(rows.length).toBe(data.length + 1); | ||
}); | ||
|
||
it('should allow row selection if selectable prop is enabled', () => { | ||
const onSelectionChange = vi.fn(); | ||
const { getAllByRole } = render( | ||
<Table | ||
data={data} | ||
columns={columns} | ||
getRowId={getRowId} | ||
selectable={true} | ||
onSelectionChange={onSelectionChange} | ||
/> | ||
); | ||
|
||
const checkboxes = getAllByRole('checkbox'); | ||
expect(checkboxes.length).toBe(data.length + 1); | ||
|
||
fireEvent.click(checkboxes[1]); | ||
expect(onSelectionChange).toHaveBeenCalledWith(new Set([1])); | ||
}); | ||
}); |