-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinMemoryPagination.test.ts
More file actions
129 lines (99 loc) · 3.25 KB
/
Copy pathinMemoryPagination.test.ts
File metadata and controls
129 lines (99 loc) · 3.25 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { act, renderHook } from '@testing-library/react'
import { useQueryAndPagination } from '../inMemoryPagination'
import { IndexType } from '../types'
test('should initialize pagination', () => {
const { result } = renderHook(() => useQueryAndPagination())
expect(result.current.page).toBe(0)
expect(result.current.size).toBe(15)
})
test('should change page', () => {
const { result } = renderHook(() => useQueryAndPagination())
act(() => {
result.current.actions.setPage(2)
})
expect(result.current.page).toBe(2)
})
test('should change search', () => {
const { result } = renderHook(() =>
useQueryAndPagination({ defaultQueryParameters: { search: '' } })
)
act(() => {
result.current.actions.updateQuery({ search: 'Max' })
})
expect(result.current.queryParameters.search).toBe('Max')
})
test('on search change -> page should be reset', () => {
const { result } = renderHook(() =>
useQueryAndPagination({ defaultQueryParameters: { search: '' } })
)
act(() => {
result.current.actions.setPage(2)
})
expect(result.current.page).toBe(2)
act(() => {
result.current.actions.updateQuery({ search: 'Max' })
})
expect(result.current.queryParameters.search).toBe('Max')
expect(result.current.page).toBe(0)
})
test('clear pagination should reset search and page', () => {
const { result } = renderHook(() =>
useQueryAndPagination({ defaultQueryParameters: { search: '' } })
)
act(() => {
result.current.actions.updateQuery({ search: 'Max' })
})
act(() => {
result.current.actions.setPage(2)
})
expect(result.current.queryParameters.search).toBe('Max')
expect(result.current.page).toBe(2)
act(() => {
result.current.actions.clear()
})
expect(result.current.queryParameters.search).toBe('')
expect(result.current.page).toBe(0)
})
test('change default parameters', () => {
const { result } = renderHook(() =>
useQueryAndPagination({ indexType: IndexType.ONE_BASED, pageSize: 10 })
)
expect(result.current.page).toBe(1)
expect(result.current.size).toBe(10)
})
test('query multiple different properties, should keep them all', () => {
const { result } = renderHook(() =>
useQueryAndPagination({
defaultQueryParameters: { search: '', department: '' },
})
)
act(() => {
result.current.actions.updateQuery({ search: 'Max' })
result.current.actions.updateQuery({ department: 'IT' })
})
expect(result.current.queryParameters.search).toBe('Max')
expect(result.current.queryParameters.department).toBe('IT')
})
test('query a property that is not configured, should do nothing', () => {
const { result } = renderHook(() =>
useQueryAndPagination({
defaultQueryParameters: { search: '' },
})
)
act(() => {
result.current.actions.updateQuery({ department: 'IT' })
})
expect(result.current.queryParameters.search).toBe('')
expect(result.current.queryParameters.department).toBeUndefined()
})
test('query property with empty value and different default value', () => {
const { result } = renderHook(() =>
useQueryAndPagination({
defaultQueryParameters: { search: 'Default search' },
})
)
act(() => {
result.current.actions.updateQuery({ search: '' })
})
expect(result.current.queryParameters.search).toBe('')
})