-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
useSortState.spec.ts
71 lines (58 loc) · 2.39 KB
/
useSortState.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { renderHook } from 'ra-test';
import useSortState, { defaultSort } from './useSortState';
import { act } from '@testing-library/react';
describe('useSortState', () => {
it('should initialize sortState with default sort', () => {
const { hookValue } = renderHook(() => useSortState());
expect(hookValue.sort).toEqual(defaultSort);
});
it('should initialize sortState with given sort', () => {
const { hookValue } = renderHook(() =>
useSortState({
field: 'name',
order: 'ASC',
})
);
expect(hookValue.sort).toEqual({ field: 'name', order: 'ASC' });
});
it('should provide setSort method to change the whole sort', () => {
const { hookValue, childrenMock } = renderHook(() =>
useSortState({ field: 'id', order: 'DESC' })
);
expect(hookValue.sort).toEqual({ field: 'id', order: 'DESC' });
act(() => hookValue.setSort({ field: 'name', order: 'ASC' }));
expect(childrenMock.mock.calls[1][0].sort).toEqual({
field: 'name',
order: 'ASC',
});
});
describe('setSortField in return value', () => {
it('should just change the order if receiving the current field', () => {
const { hookValue, childrenMock } = renderHook(() =>
useSortState({ field: 'id', order: 'DESC' })
);
expect(hookValue.sort).toEqual({ field: 'id', order: 'DESC' });
act(() => hookValue.setSortField('id'));
expect(childrenMock.mock.calls[1][0].sort).toEqual({
field: 'id',
order: 'ASC',
});
});
it('should change the field and set the order to ASC if receiving another field', () => {
const { hookValue, childrenMock } = renderHook(() =>
useSortState({ field: 'id', order: 'ASC' })
);
expect(hookValue.sort).toEqual({ field: 'id', order: 'ASC' });
act(() => hookValue.setSortField('name'));
expect(childrenMock.mock.calls[1][0].sort).toEqual({
field: 'name',
order: 'ASC',
});
act(() => hookValue.setSortField('id'));
expect(childrenMock.mock.calls[2][0].sort).toEqual({
field: 'id',
order: 'ASC',
});
});
});
});