-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a8e803
commit 3a0d48d
Showing
4 changed files
with
109 additions
and
7 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
67 changes: 67 additions & 0 deletions
67
src/components/table/table-sortable-head-cell/__tests__/table-sortable-head-cell.test.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,67 @@ | ||
import React from 'react'; | ||
|
||
import { render, screen, userEvent } from '@/test-utils/rtl'; | ||
|
||
import { type SortOrder } from '@/utils/sort-by'; | ||
|
||
import TableSortableHeadCell from '../table-sortable-head-cell'; | ||
|
||
describe(TableSortableHeadCell.name, () => { | ||
it('should render unsorted without error', async () => { | ||
setup({ sortColumn: 'column_2', sortOrder: 'DESC' }); | ||
|
||
expect( | ||
await screen.findByLabelText('Column 1, not sorted') | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it('should call onSort when clicked', async () => { | ||
const { mockOnSort, user } = setup({ | ||
sortColumn: 'column_2', | ||
sortOrder: 'DESC', | ||
}); | ||
|
||
const cell = await screen.findByLabelText('Column 1, not sorted'); | ||
|
||
await user.click(cell); | ||
expect(mockOnSort).toHaveBeenCalledWith('column_1'); | ||
}); | ||
|
||
it('should render sorted ASC without error', async () => { | ||
setup({ sortColumn: 'column_1', sortOrder: 'ASC' }); | ||
|
||
expect( | ||
await screen.findByLabelText('Column 1, ascending sorting') | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render sorted DESC without error', async () => { | ||
setup({ sortColumn: 'column_1', sortOrder: 'DESC' }); | ||
|
||
expect( | ||
await screen.findByLabelText('Column 1, descending sorting') | ||
).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
function setup({ | ||
sortColumn, | ||
sortOrder, | ||
}: { | ||
sortColumn: string; | ||
sortOrder: SortOrder; | ||
}) { | ||
const user = userEvent.setup(); | ||
const mockOnSort = jest.fn(); | ||
render( | ||
<TableSortableHeadCell | ||
name="Column 1" | ||
columnID="column_1" | ||
width="20%" | ||
onSort={mockOnSort} | ||
sortColumn={sortColumn} | ||
sortOrder={sortOrder} | ||
/> | ||
); | ||
return { mockOnSort, user }; | ||
} |
4 changes: 3 additions & 1 deletion
4
src/components/table/table-sortable-head-cell/table-sortable-head-cell.types.ts
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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { type SortOrder } from '@/utils/sort-by'; | ||
|
||
export type Props = { | ||
name: string; | ||
columnID: string; | ||
width: string; | ||
sortColumn?: string; | ||
sortOrder?: string; | ||
sortOrder?: SortOrder; | ||
onSort: (column: string) => void; | ||
}; |
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