-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
usePaginationState.spec.ts
80 lines (63 loc) · 2.68 KB
/
usePaginationState.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
72
73
74
75
76
77
78
79
80
import { renderHook } from 'ra-test';
import usePaginationState from './usePaginationState';
import { act } from '@testing-library/react';
describe('usePaginationState', () => {
it('should initialize pagination state with default', () => {
const { hookValue } = renderHook(() => usePaginationState());
expect(hookValue.pagination).toEqual({ page: 1, perPage: 25 });
});
it('should take given page and perPage props to initialize with', () => {
const { hookValue } = renderHook(() =>
usePaginationState({ perPage: 50, page: 10 })
);
expect(hookValue.pagination).toEqual({ page: 10, perPage: 50 });
});
it('should update perPage state when the perPage props update', () => {
const { hookValue, childrenMock, rerender } = renderHook(() =>
usePaginationState({ perPage: 50, page: 10 })
);
expect(hookValue.pagination).toEqual({ page: 10, perPage: 50 });
rerender(() => usePaginationState({ perPage: 100, page: 10 }));
expect(childrenMock).toBeCalledTimes(3);
expect(childrenMock.mock.calls[2][0].pagination).toEqual({
page: 10,
perPage: 100,
});
});
it('should provide a setPagination function to update the pagination state (page + perPage)', () => {
const { hookValue, childrenMock } = renderHook(() =>
usePaginationState()
);
expect(hookValue.pagination).toEqual({ page: 1, perPage: 25 });
act(() => hookValue.setPagination({ perPage: 100, page: 20 }));
expect(childrenMock).toBeCalledTimes(2);
expect(childrenMock.mock.calls[1][0].pagination).toEqual({
page: 20,
perPage: 100,
});
});
it('should provide setPage function to update the page state', () => {
const { hookValue, childrenMock } = renderHook(() =>
usePaginationState()
);
expect(hookValue.pagination).toEqual({ page: 1, perPage: 25 });
act(() => hookValue.setPage(20));
expect(childrenMock).toBeCalledTimes(2);
expect(childrenMock.mock.calls[1][0].pagination).toEqual({
page: 20,
perPage: 25,
});
});
it('should provide a setPerPage function to update the perPage state', () => {
const { hookValue, childrenMock } = renderHook(() =>
usePaginationState()
);
expect(hookValue.pagination).toEqual({ page: 1, perPage: 25 });
act(() => hookValue.setPerPage(100));
expect(childrenMock).toBeCalledTimes(2);
expect(childrenMock.mock.calls[1][0].pagination).toEqual({
page: 1,
perPage: 100,
});
});
});