Skip to content

Commit b1c1270

Browse files
add tests for in memory pagination
1 parent b7ec40d commit b1c1270

2 files changed

Lines changed: 70 additions & 1 deletion

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"devDependencies": {
4444
"@testing-library/jest-dom": "^5.11.10",
4545
"@testing-library/react": "^11.2.6",
46-
"@testing-library/react-hooks": "^5.1.1",
46+
"@testing-library/react-hooks": "^5.1.3",
4747
"@types/jest": "^26.0.22",
4848
"@types/react": "^17.0.3",
4949
"@typescript-eslint/eslint-plugin": "^4.21.0",
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { act, renderHook } from '@testing-library/react-hooks'
2+
import { useSearchAndPaginationInMemory } from '../inMemoryPagination'
3+
4+
test('should initialize pagination', () => {
5+
const { result } = renderHook(() => useSearchAndPaginationInMemory())
6+
7+
expect(result.current.page).toBe(0)
8+
expect(result.current.search).toBe('')
9+
expect(result.current.page).toBe(0)
10+
})
11+
12+
test('should change page', () => {
13+
const { result } = renderHook(() => useSearchAndPaginationInMemory())
14+
15+
act(() => {
16+
result.current.paginationActions.setPage(2)
17+
})
18+
19+
expect(result.current.page).toBe(2)
20+
})
21+
22+
test('should change search', () => {
23+
const { result } = renderHook(() => useSearchAndPaginationInMemory())
24+
25+
act(() => {
26+
result.current.searchActions.search('Max')
27+
})
28+
29+
expect(result.current.search).toBe('Max')
30+
})
31+
32+
test('on search change -> page should be reset', () => {
33+
const { result } = renderHook(() => useSearchAndPaginationInMemory())
34+
35+
act(() => {
36+
result.current.paginationActions.setPage(2)
37+
})
38+
39+
expect(result.current.page).toBe(2)
40+
41+
act(() => {
42+
result.current.searchActions.search('Max')
43+
})
44+
45+
expect(result.current.search).toBe('Max')
46+
expect(result.current.page).toBe(0)
47+
})
48+
49+
test('clear pagination should reset search and page', () => {
50+
const { result } = renderHook(() => useSearchAndPaginationInMemory())
51+
52+
act(() => {
53+
result.current.searchActions.search('Max')
54+
})
55+
56+
act(() => {
57+
result.current.paginationActions.setPage(2)
58+
})
59+
60+
expect(result.current.search).toBe('Max')
61+
expect(result.current.page).toBe(2)
62+
63+
act(() => {
64+
result.current.searchActions.clear()
65+
})
66+
67+
expect(result.current.search).toBe('')
68+
expect(result.current.page).toBe(0)
69+
})

0 commit comments

Comments
 (0)