1 import { useHistory, useLocation } from 'react-router-dom';
3 import { act, renderHook } from '@testing-library/react-hooks';
5 import useSearchParams from './useSearchParams';
7 // Mocking dependencies like useLocation and useHistory
8 jest.mock('react-router-dom', () => ({
9 useLocation: jest.fn(),
10 useHistory: jest.fn(),
13 describe('useSearchParams', () => {
14 const mockUseLocation = useLocation as jest.Mock;
15 const mockUseHistory = useHistory as jest.Mock;
18 // Reset mocks before each test
22 it('should set search params correctly', () => {
23 mockUseLocation.mockReturnValueOnce({ pathname: '/path', hash: '' });
24 const mockPush = jest.fn();
25 mockUseHistory.mockReturnValueOnce({ push: mockPush });
27 const { result } = renderHook(() => useSearchParams());
30 const [, setSearchParams] = result.current;
31 setSearchParams({ key: 'value' });
34 expect(mockPush).toHaveBeenCalledWith('/path#key=value');
37 it('should update search params correctly', () => {
38 // Mock initial location
39 mockUseLocation.mockReturnValueOnce({ pathname: '/path', hash: '#key1=value1&key2=value2' });
40 const mockPush = jest.fn();
41 mockUseHistory.mockReturnValueOnce({ push: mockPush });
44 const { result } = renderHook(() => useSearchParams());
46 // Destructure setSearchParams from the hook result
47 const [, setSearchParams] = result.current;
49 // Update the search params
51 setSearchParams({ key2: 'updatedValue' });
54 // Assert that history.push is called with the correct updated route
55 expect(mockPush).toHaveBeenCalledWith('/path#key1=value1&key2=updatedValue');
58 it('should update search params with function correctly', () => {
59 mockUseLocation.mockReturnValueOnce({ pathname: '/path', hash: '#key1=value1' });
60 const mockPush = jest.fn();
61 mockUseHistory.mockReturnValueOnce({ push: mockPush });
63 const { result } = renderHook(() => useSearchParams());
66 const [, setSearchParams] = result.current;
67 setSearchParams((params) => ({ ...params, key2: 'value2' }));
70 expect(mockPush).toHaveBeenCalledWith('/path#key1=value1&key2=value2');
73 it('should handle init prop correctly', () => {
74 const initParams = { key1: 'value1', key2: 'value2' };
76 mockUseLocation.mockReturnValueOnce({ pathname: '/path', hash: '#key1=value1' });
77 const mockPush = jest.fn();
78 mockUseHistory.mockReturnValueOnce({ push: mockPush });
80 renderHook(() => useSearchParams(initParams));
82 expect(mockPush).toHaveBeenCalledTimes(1);
83 expect(mockPush).toHaveBeenCalledWith('/path#key1=value1&key2=value2');
86 it('should handle init prop with undefined correctly', () => {
87 mockUseLocation.mockReturnValueOnce({ pathname: '/path', hash: '#key1=value1' });
88 const mockPush = jest.fn();
89 mockUseHistory.mockReturnValueOnce({ push: mockPush });
91 renderHook(() => useSearchParams(undefined));
93 expect(mockPush).not.toHaveBeenCalled();
96 it('should handle unmounting correctly', () => {
97 mockUseLocation.mockReturnValueOnce({ pathname: '/path', hash: '#key1=value1' });
98 const mockPush = jest.fn();
99 mockUseHistory.mockReturnValueOnce({ push: mockPush });
101 const { unmount } = renderHook(() => useSearchParams());
103 // Assert that setSearchParams is not called when the component is unmounted
105 expect(mockPush).not.toHaveBeenCalled();