Skip to content

Commit d6029a1

Browse files
add more tests to router implementations
1 parent bcea134 commit d6029a1

5 files changed

Lines changed: 175 additions & 4 deletions

File tree

src/__test__/inMemoryPagination.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,36 @@ test('change default parameters', () => {
8282
expect(result.current.page).toBe(1)
8383
expect(result.current.size).toBe(10)
8484
})
85+
86+
test('query multiple different properties, should keep them all', () => {
87+
const { result } = renderHook(() =>
88+
useQueryAndPagination({
89+
defaultQueryParameters: { query: '', department: '' },
90+
})
91+
)
92+
93+
act(() => {
94+
result.current.actions.query({ query: 'Max' })
95+
result.current.actions.query({ department: 'IT' })
96+
})
97+
98+
expect(result.current.queryParameters.query).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: { query: '' },
106+
})
107+
)
108+
109+
act(() => {
110+
result.current.actions.query({ department: 'IT' })
111+
})
112+
113+
expect(result.current.queryParameters.query).toBe('')
114+
expect(result.current.queryParameters.department).toBeUndefined()
115+
})
116+
117+
// test('properties in the URL, that are not part of the configuration should be left untouched', () => {})

src/__test__/nextRouterPagination.test.tsx

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,69 @@ test('on search change -> page should be reset', () => {
9999
expect(router.query.search).toBe('Max')
100100
expect(router.query.page).toBeUndefined()
101101
})
102+
103+
test('query multiple different properties, should keep them all', () => {
104+
const { result } = renderHook(() =>
105+
useQueryAndPagination({
106+
defaultQueryParameters: { search: '', department: '' },
107+
})
108+
)
109+
110+
act(() => {
111+
result.current.actions.query({ search: 'Max' })
112+
result.current.actions.query({ department: 'IT' })
113+
})
114+
115+
expect(result.current.queryParameters.search).toBe('Max')
116+
expect(result.current.queryParameters.department).toBe('IT')
117+
})
118+
119+
test('query a property that is not configured, should do nothing', () => {
120+
const { result } = renderHook(() =>
121+
useQueryAndPagination({
122+
defaultQueryParameters: { search: '' },
123+
})
124+
)
125+
126+
act(() => {
127+
result.current.actions.query({ department: 'IT' })
128+
})
129+
130+
expect(result.current.queryParameters.search).toBe('')
131+
expect(result.current.queryParameters.department).toBeUndefined()
132+
})
133+
134+
test('properties in the URL, that are not part of the configuration should be left untouched', () => {
135+
router.query = { greeting: 'hello' }
136+
137+
const { result } = renderHook(() =>
138+
useQueryAndPagination({
139+
defaultQueryParameters: { search: '' },
140+
})
141+
)
142+
143+
act(() => {
144+
result.current.actions.query({ search: 'Max' })
145+
})
146+
147+
expect(result.current.queryParameters.search).toBe('Max')
148+
expect(result.current.queryParameters.greeting).toBeUndefined()
149+
expect(router.query.greeting).toBe('hello')
150+
})
151+
152+
test('query property with default value, should remove it from url', () => {
153+
router.query = { search: 'Max' }
154+
155+
const { result } = renderHook(() =>
156+
useQueryAndPagination({
157+
defaultQueryParameters: { search: '' },
158+
})
159+
)
160+
161+
act(() => {
162+
result.current.actions.query({ search: '' })
163+
})
164+
165+
expect(result.current.queryParameters.search).toBe('')
166+
expect(router.query.search).toBeUndefined()
167+
})

src/__test__/reactRouterPagination.test.tsx

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ test('clear pagination should reset search and page', () => {
4747
{ wrapper }
4848
)
4949

50-
console.log(window.location.search)
51-
5250
act(() => {
5351
result.current.actions.query({ search: 'Max' })
5452
})
@@ -79,3 +77,77 @@ test('change default parameters', () => {
7977
expect(result.current.page).toBe(1)
8078
expect(result.current.size).toBe(10)
8179
})
80+
81+
test('query multiple different properties, should keep them all', () => {
82+
const { result } = renderHook(
83+
() =>
84+
useQueryAndPagination({
85+
defaultQueryParameters: { search: '', department: '' },
86+
}),
87+
{ wrapper }
88+
)
89+
90+
act(() => {
91+
result.current.actions.query({ search: 'Max' })
92+
result.current.actions.query({ department: 'IT' })
93+
})
94+
95+
expect(result.current.queryParameters.search).toBe('Max')
96+
expect(result.current.queryParameters.department).toBe('IT')
97+
})
98+
99+
test('query a property that is not configured, should do nothing', () => {
100+
const { result } = renderHook(
101+
() =>
102+
useQueryAndPagination({
103+
defaultQueryParameters: { search: '' },
104+
}),
105+
{ wrapper }
106+
)
107+
108+
act(() => {
109+
result.current.actions.query({ department: 'IT' })
110+
})
111+
112+
expect(result.current.queryParameters.search).toBe('')
113+
expect(result.current.queryParameters.department).toBeUndefined()
114+
})
115+
116+
test('properties in the URL, that are not part of the configuration should be left untouched', () => {
117+
window.history.pushState({}, '', '/?greeting=hello')
118+
119+
const { result } = renderHook(
120+
() =>
121+
useQueryAndPagination({
122+
defaultQueryParameters: { search: '' },
123+
}),
124+
{ wrapper }
125+
)
126+
127+
act(() => {
128+
result.current.actions.query({ search: 'Max' })
129+
})
130+
131+
expect(result.current.queryParameters.search).toBe('Max')
132+
expect(result.current.queryParameters.greeting).toBeUndefined()
133+
expect(window.location.search).toBe('?greeting=hello&search=Max')
134+
})
135+
136+
test('query property with default value, should remove it from url', () => {
137+
window.history.pushState({}, '', '/?search=Anton')
138+
139+
const { result } = renderHook(
140+
() =>
141+
useQueryAndPagination({
142+
defaultQueryParameters: { search: '' },
143+
}),
144+
{ wrapper }
145+
)
146+
147+
act(() => {
148+
result.current.actions.query({ search: '' })
149+
})
150+
151+
expect(result.current.queryParameters.search).toBe('')
152+
expect(window.location.search).toBe('')
153+
})

src/nextRouterPagination.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function extractCurrentQueryParameters(
1919
return {}
2020
}
2121

22-
const result: QueryParameters = defaultQueryParameters
22+
const result: QueryParameters = { ...defaultQueryParameters }
2323

2424
for (const parameter in defaultQueryParameters) {
2525
if (

src/reactRouterPagination.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function extractCurrentQueryParameters(
1212
return {}
1313
}
1414

15-
const result: QueryParameters = defaultQueryParameters
15+
const result: QueryParameters = { ...defaultQueryParameters }
1616

1717
for (const parameter in defaultQueryParameters) {
1818
if (query.get(parameter)) {

0 commit comments

Comments
 (0)