Skip to content

Commit f1983e3

Browse files
add unit tests to useQueryParams
1 parent dcb4491 commit f1983e3

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

__tests__/useQueryParams.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { act, renderHook } from '@testing-library/react-hooks';
2+
import useQueryParams from '../src/useQueryParams';
3+
4+
describe('useQueryParams', () => {
5+
it('should be able to set query params', () => {
6+
const newParams = { page: '1', order: 'ASC' };
7+
const { result } = renderHook(() => useQueryParams());
8+
9+
act(() => {
10+
result.current.setParams(newParams);
11+
});
12+
const params = result.current.getParams();
13+
expect(params).toStrictEqual(newParams);
14+
});
15+
16+
it('should be able to clear query params', () => {
17+
const { result } = renderHook(() => useQueryParams());
18+
19+
act(() => {
20+
result.current.setParams({});
21+
});
22+
const params = result.current.getParams();
23+
expect(params).toStrictEqual({});
24+
});
25+
26+
it('should be able to read query params', () => {
27+
delete window.location;
28+
window.location = new URL('https://test.com/testing?param1=test&testing=true');
29+
30+
const { result } = renderHook(() => useQueryParams());
31+
let params;
32+
act(() => {
33+
params = result.current.getParams();
34+
});
35+
expect(params).toStrictEqual({ param1: 'test', testing: 'true' });
36+
});
37+
});

0 commit comments

Comments
 (0)