forked from SigNoz/signoz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
useUrlQuery.test.tsx
56 lines (43 loc) 路 1.71 KB
/
useUrlQuery.test.tsx
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
import { act, renderHook } from '@testing-library/react';
import { createMemoryHistory } from 'history';
import { Router } from 'react-router-dom';
import useUrlQuery from './useUrlQuery';
describe('useUrlQuery', () => {
test('returns URLSearchParams object for the current URL search', () => {
const history = createMemoryHistory({
initialEntries: ['/test?param1=value1¶m2=value2'],
});
const { result } = renderHook(() => useUrlQuery(), {
wrapper: ({ children }) => <Router history={history}>{children}</Router>,
});
expect(result.current.get('param1')).toBe('value1');
expect(result.current.get('param2')).toBe('value2');
});
test('updates URLSearchParams object when URL search changes', () => {
const history = createMemoryHistory({
initialEntries: ['/test?param1=value1'],
});
const { result, rerender } = renderHook(() => useUrlQuery(), {
wrapper: ({ children }) => <Router history={history}>{children}</Router>,
});
expect(result.current.get('param1')).toBe('value1');
expect(result.current.get('param2')).toBe(null);
act(() => {
history.push('/test?param1=newValue1¶m2=value2');
});
rerender();
expect(result.current.get('param1')).toBe('newValue1');
expect(result.current.get('param2')).toBe('value2');
});
test('returns empty URLSearchParams object when no query parameters are present', () => {
const history = createMemoryHistory({
initialEntries: ['/test'],
});
const { result } = renderHook(() => useUrlQuery(), {
wrapper: ({ children }) => <Router history={history}>{children}</Router>,
});
expect(result.current.toString()).toBe('');
expect(result.current.get('param1')).toBe(null);
expect(result.current.get('param2')).toBe(null);
});
});