Skip to content

Commit d7aaa7b

Browse files
author
devgioele
committed
prototype 1
1 parent d60509d commit d7aaa7b

14 files changed

Lines changed: 1041 additions & 77 deletions

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@
7373
"rimraf": "^5.0.1",
7474
"typescript": "^5.1.6",
7575
"vite": "^4.4.4",
76-
"vitest": "^0.33.0"
76+
"vitest": "^0.33.0",
77+
"zod": "^3.21.4"
7778
},
7879
"peerDependencies": {
7980
"next": "^12.0.0 || ^13.0.0",
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import { act, renderHook } from '@testing-library/react'
2+
import { useQueryAndPagination } from '../inMemoryPagination'
3+
import { IndexType } from '../types'
4+
5+
test('should initialize pagination', () => {
6+
const { result } = renderHook(() => useQueryAndPagination())
7+
8+
expect(result.current.page).toBe(0)
9+
expect(result.current.size).toBe(15)
10+
})
11+
12+
test('should change page', () => {
13+
const { result } = renderHook(() => useQueryAndPagination())
14+
15+
act(() => {
16+
result.current.actions.setPage(2)
17+
})
18+
19+
expect(result.current.page).toBe(2)
20+
})
21+
22+
test('should change search', () => {
23+
const { result } = renderHook(() =>
24+
useQueryAndPagination({ defaultQueryParameters: { search: '' } })
25+
)
26+
27+
act(() => {
28+
result.current.actions.updateQuery({ search: 'Max' })
29+
})
30+
31+
expect(result.current.queryParameters.search).toBe('Max')
32+
})
33+
34+
test('on search change -> page should be reset', () => {
35+
const { result } = renderHook(() =>
36+
useQueryAndPagination({ defaultQueryParameters: { search: '' } })
37+
)
38+
39+
act(() => {
40+
result.current.actions.setPage(2)
41+
})
42+
43+
expect(result.current.page).toBe(2)
44+
45+
act(() => {
46+
result.current.actions.updateQuery({ search: 'Max' })
47+
})
48+
49+
expect(result.current.queryParameters.search).toBe('Max')
50+
expect(result.current.page).toBe(0)
51+
})
52+
53+
test('clear pagination should reset search and page', () => {
54+
const { result } = renderHook(() =>
55+
useQueryAndPagination({ defaultQueryParameters: { search: '' } })
56+
)
57+
58+
act(() => {
59+
result.current.actions.updateQuery({ search: 'Max' })
60+
})
61+
62+
act(() => {
63+
result.current.actions.setPage(2)
64+
})
65+
66+
expect(result.current.queryParameters.search).toBe('Max')
67+
expect(result.current.page).toBe(2)
68+
69+
act(() => {
70+
result.current.actions.clear()
71+
})
72+
73+
expect(result.current.queryParameters.search).toBe('')
74+
expect(result.current.page).toBe(0)
75+
})
76+
77+
test('change default parameters', () => {
78+
const { result } = renderHook(() =>
79+
useQueryAndPagination({ indexType: IndexType.ONE_BASED, pageSize: 10 })
80+
)
81+
82+
expect(result.current.page).toBe(1)
83+
expect(result.current.size).toBe(10)
84+
})
85+
86+
test('query multiple different properties, should keep them all', () => {
87+
const { result } = renderHook(() =>
88+
useQueryAndPagination({
89+
defaultQueryParameters: { search: '', department: '' },
90+
})
91+
)
92+
93+
act(() => {
94+
result.current.actions.updateQuery({ search: 'Max' })
95+
result.current.actions.updateQuery({ department: 'IT' })
96+
})
97+
98+
expect(result.current.queryParameters.search).toBe('Max')
99+
expect(result.current.queryParameters.department).toBe('IT')
100+
})
101+
102+
test('query a property that is not configured, should do nothing', () => {
103+
const { result } = renderHook(() =>
104+
useQueryAndPagination({
105+
defaultQueryParameters: { search: '' },
106+
})
107+
)
108+
109+
act(() => {
110+
result.current.actions.updateQuery({ department: 'IT' })
111+
})
112+
113+
expect(result.current.queryParameters.search).toBe('')
114+
expect(result.current.queryParameters.department).toBeUndefined()
115+
})
116+
117+
test('query property with empty value and different default value', () => {
118+
const { result } = renderHook(() =>
119+
useQueryAndPagination({
120+
defaultQueryParameters: { search: 'Default search' },
121+
})
122+
)
123+
124+
act(() => {
125+
result.current.actions.updateQuery({ search: '' })
126+
})
127+
128+
expect(result.current.queryParameters.search).toBe('')
129+
})
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
import { act, renderHook } from '@testing-library/react'
2+
import router from 'next/router'
3+
4+
import { useQueryAndPagination } from '../nextRouterPagination'
5+
import { IndexType } from '../types'
6+
7+
jest.mock('next/router', () => require('next-router-mock'))
8+
9+
beforeEach(() => {
10+
router.query = {}
11+
})
12+
13+
test('should initialize pagination', () => {
14+
const { result } = renderHook(() => useQueryAndPagination())
15+
16+
expect(result.current.page).toBe(0)
17+
expect(result.current.size).toBe(15)
18+
})
19+
20+
test('should change page', () => {
21+
const { result } = renderHook(() => useQueryAndPagination())
22+
23+
act(() => {
24+
result.current.actions.setPage(2)
25+
})
26+
27+
expect(result.current.page).toBe(2)
28+
expect(router.query.page).toBe('2')
29+
})
30+
31+
test('should change search', () => {
32+
const { result } = renderHook(() =>
33+
useQueryAndPagination({ defaultQueryParameters: { search: '' } })
34+
)
35+
36+
act(() => {
37+
result.current.actions.updateQuery({ search: 'Max' })
38+
})
39+
40+
expect(result.current.queryParameters.search).toBe('Max')
41+
expect(router.query.search).toBe('Max')
42+
})
43+
44+
test('clear pagination should reset search and page', () => {
45+
const { result } = renderHook(() =>
46+
useQueryAndPagination({ defaultQueryParameters: { search: '' } })
47+
)
48+
49+
act(() => {
50+
result.current.actions.updateQuery({ search: 'Max' })
51+
})
52+
53+
act(() => {
54+
result.current.actions.setPage(2)
55+
})
56+
57+
expect(router.query.search).toBe('Max')
58+
expect(router.query.page).toBe('2')
59+
60+
act(() => {
61+
result.current.actions.clear()
62+
})
63+
64+
expect(result.current.queryParameters.search).toBe('')
65+
expect(result.current.page).toBe(0)
66+
expect(router.query.search).toBeUndefined()
67+
expect(router.query.page).toBeUndefined()
68+
})
69+
70+
test('change default parameters', () => {
71+
const { result } = renderHook(() =>
72+
useQueryAndPagination({ indexType: IndexType.ONE_BASED, pageSize: 10 })
73+
)
74+
75+
expect(result.current.page).toBe(1)
76+
expect(result.current.size).toBe(10)
77+
})
78+
79+
test('on search change -> page should be reset', () => {
80+
const { result } = renderHook(() =>
81+
useQueryAndPagination({ defaultQueryParameters: { search: '' } })
82+
)
83+
84+
act(() => {
85+
result.current.actions.setPage(2)
86+
})
87+
88+
expect(result.current.page).toBe(2)
89+
expect(router.query.page).toBe('2')
90+
91+
act(() => {
92+
result.current.actions.updateQuery({ search: 'Max' })
93+
})
94+
95+
expect(result.current.queryParameters.search).toBe('Max')
96+
expect(result.current.page).toBe(0)
97+
expect(router.query.search).toBe('Max')
98+
expect(router.query.page).toBeUndefined()
99+
})
100+
101+
test('query multiple different properties, should keep them all', () => {
102+
const { result } = renderHook(() =>
103+
useQueryAndPagination({
104+
defaultQueryParameters: { search: '', department: '' },
105+
})
106+
)
107+
108+
act(() => {
109+
result.current.actions.updateQuery({ search: 'Max' })
110+
})
111+
112+
act(() => {
113+
result.current.actions.updateQuery({ department: 'IT' })
114+
})
115+
116+
expect(result.current.queryParameters.search).toBe('Max')
117+
expect(result.current.queryParameters.department).toBe('IT')
118+
})
119+
120+
test('query a property that is not configured, should do nothing', () => {
121+
const { result } = renderHook(() =>
122+
useQueryAndPagination({
123+
defaultQueryParameters: { search: '' },
124+
})
125+
)
126+
127+
act(() => {
128+
result.current.actions.updateQuery({ department: 'IT' })
129+
})
130+
131+
expect(result.current.queryParameters.search).toBe('')
132+
expect(result.current.queryParameters.department).toBeUndefined()
133+
})
134+
135+
test('properties in the URL, that are not part of the configuration should be left untouched', () => {
136+
router.query = { greeting: 'hello' }
137+
138+
const { result } = renderHook(() =>
139+
useQueryAndPagination({
140+
defaultQueryParameters: { search: '' },
141+
})
142+
)
143+
144+
act(() => {
145+
result.current.actions.updateQuery({ search: 'Max' })
146+
})
147+
148+
expect(result.current.queryParameters.search).toBe('Max')
149+
expect(result.current.queryParameters.greeting).toBeUndefined()
150+
expect(router.query.greeting).toBe('hello')
151+
})
152+
153+
test('query property with default value, should remove it from url', () => {
154+
router.query = { search: 'Max' }
155+
156+
const { result } = renderHook(() =>
157+
useQueryAndPagination({
158+
defaultQueryParameters: { search: '' },
159+
})
160+
)
161+
162+
act(() => {
163+
result.current.actions.updateQuery({ search: '' })
164+
})
165+
166+
expect(result.current.queryParameters.search).toBe('')
167+
expect(router.query.search).toBeUndefined()
168+
})
169+
170+
test('query property with empty value and different default value', () => {
171+
const { result } = renderHook(() =>
172+
useQueryAndPagination({
173+
defaultQueryParameters: { search: 'Default search' },
174+
})
175+
)
176+
177+
act(() => {
178+
result.current.actions.updateQuery({ search: '' })
179+
})
180+
181+
expect(result.current.queryParameters.search).toBe('')
182+
expect(router.query.search).toBe('')
183+
})

0 commit comments

Comments
 (0)