-
-
Notifications
You must be signed in to change notification settings - Fork 642
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
Showing
1 changed file
with
60 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { act, renderHook } from '@testing-library/react'; | ||
import { SortDirection, useTableColumnSorts } from './table-columns'; | ||
|
||
describe('useTableColumnSorts', () => { | ||
it('use cases', () => { | ||
const { result } = renderHook(() => useTableColumnSorts([])); | ||
|
||
// Toggle on one column | ||
act(() => { | ||
const [_columnSorts, toggleColumnSort] = result.current; | ||
toggleColumnSort('one', false, SortDirection.ASC)(); | ||
}); | ||
|
||
let [columnSorts] = result.current; | ||
expect(columnSorts).toEqual([{ columnId: 'one', sort: SortDirection.ASC }]); | ||
|
||
// Now toggle that same column | ||
act(() => { | ||
const [_columnSorts, toggleColumnSort] = result.current; | ||
toggleColumnSort('one', false, SortDirection.ASC)(); | ||
}); | ||
|
||
// Now we should see the sort inverted | ||
[columnSorts] = result.current; | ||
expect(columnSorts).toEqual([{ columnId: 'one', sort: SortDirection.DESC }]); | ||
|
||
// Now toggle another column | ||
act(() => { | ||
const [_columnSorts, toggleColumnSort] = result.current; | ||
toggleColumnSort('two', false, SortDirection.ASC)(); | ||
}); | ||
|
||
// Now we should see only the second column | ||
[columnSorts] = result.current; | ||
expect(columnSorts).toEqual([{ columnId: 'two', sort: SortDirection.ASC }]); | ||
|
||
// Toggle the first column, but with additive=true | ||
act(() => { | ||
const [_columnSorts, toggleColumnSort] = result.current; | ||
toggleColumnSort('one', true, SortDirection.ASC)(); | ||
}); | ||
|
||
// Now we should see both columns | ||
[columnSorts] = result.current; | ||
expect(columnSorts).toEqual([ | ||
{ columnId: 'two', sort: SortDirection.ASC }, | ||
{ columnId: 'one', sort: SortDirection.ASC }, | ||
]); | ||
|
||
// Toggle a third column with additive=false, but this one has a different default sort | ||
act(() => { | ||
const [_columnSorts, toggleColumnSort] = result.current; | ||
toggleColumnSort('three', false, SortDirection.DESC)(); | ||
}); | ||
|
||
// Now we should see only the third column, but with its default sort | ||
[columnSorts] = result.current; | ||
expect(columnSorts).toEqual([{ columnId: 'three', sort: SortDirection.DESC }]); | ||
}); | ||
}); |