|
| 1 | +import { act, cleanup, renderHook } from '@testing-library/react-hooks' |
| 2 | +import React from 'react' |
| 3 | +import { MemoryRouter } from 'react-router-dom' |
| 4 | +import useQueryParam from '../index' |
| 5 | + |
| 6 | +// eslint-disable-next-line react/prop-types |
| 7 | +const wrapper = ({ pathname = 'one', search }) => ({ children }) => ( |
| 8 | + <MemoryRouter initialIndex={0} initialEntries={[{ pathname, search }]}> |
| 9 | + {children} |
| 10 | + </MemoryRouter> |
| 11 | +) |
| 12 | + |
| 13 | +describe('useQueryParam', () => { |
| 14 | + afterEach(cleanup) |
| 15 | + it('should set one object', async () => { |
| 16 | + const { result } = renderHook(() => useQueryParam(), { |
| 17 | + wrapper: MemoryRouter, |
| 18 | + }) |
| 19 | + |
| 20 | + act(() => { |
| 21 | + result.current.setQueryParams({ user: 'John' }) |
| 22 | + }) |
| 23 | + expect(result.current.queryParams).toEqual({ user: 'John' }) |
| 24 | + }) |
| 25 | + |
| 26 | + it('should correctly set with different value', async () => { |
| 27 | + const { result } = renderHook(() => useQueryParam(), { |
| 28 | + wrapper: MemoryRouter, |
| 29 | + }) |
| 30 | + |
| 31 | + act(() => { |
| 32 | + result.current.setQueryParams({ user: 'John' }) |
| 33 | + }) |
| 34 | + expect(result.current.queryParams).toEqual({ user: 'John' }) |
| 35 | + |
| 36 | + act(() => { |
| 37 | + result.current.setQueryParams({ user: 'Doe' }) |
| 38 | + }) |
| 39 | + expect(result.current.queryParams).toEqual({ user: 'Doe' }) |
| 40 | + |
| 41 | + act(() => { |
| 42 | + result.current.setQueryParams({ user: 'Scaleway' }) |
| 43 | + }) |
| 44 | + expect(result.current.queryParams).toEqual({ user: 'Scaleway' }) |
| 45 | + }) |
| 46 | + |
| 47 | + it('should set one complexe object', async () => { |
| 48 | + const { result } = renderHook(() => useQueryParam(), { |
| 49 | + wrapper: MemoryRouter, |
| 50 | + }) |
| 51 | + |
| 52 | + act(() => { |
| 53 | + result.current.setQueryParams({ |
| 54 | + user: 'John Doe', |
| 55 | + name: 'John', |
| 56 | + lastName: 'Doe', |
| 57 | + version: 1234, |
| 58 | + lib: 'useQueryParams', |
| 59 | + }) |
| 60 | + }) |
| 61 | + expect(result.current.queryParams).toEqual({ |
| 62 | + user: 'John Doe', |
| 63 | + name: 'John', |
| 64 | + lastName: 'Doe', |
| 65 | + version: 1234, |
| 66 | + lib: 'useQueryParams', |
| 67 | + }) |
| 68 | + }) |
| 69 | + |
| 70 | + it('should get queryParams from existing location', () => { |
| 71 | + const { result } = renderHook(() => useQueryParam(), { |
| 72 | + wrapper: wrapper({ search: 'user=john' }), |
| 73 | + }) |
| 74 | + |
| 75 | + expect(result.current.queryParams).toEqual({ user: 'john' }) |
| 76 | + }) |
| 77 | + it('should should handle array, boolean, number and string from existing location', () => { |
| 78 | + const { result } = renderHook(() => useQueryParam(), { |
| 79 | + wrapper: wrapper({ |
| 80 | + search: 'string=john&boolean=true&number=123&array=handle,array,format', |
| 81 | + }), |
| 82 | + }) |
| 83 | + |
| 84 | + expect(result.current.queryParams).toEqual({ |
| 85 | + string: 'john', |
| 86 | + boolean: true, |
| 87 | + number: 123, |
| 88 | + array: ['handle', 'array', 'format'], |
| 89 | + }) |
| 90 | + }) |
| 91 | + |
| 92 | + it('should get queryParams from existing location and set new params', () => { |
| 93 | + const { result } = renderHook(() => useQueryParam(), { |
| 94 | + wrapper: wrapper({ search: 'user=john' }), |
| 95 | + }) |
| 96 | + |
| 97 | + expect(result.current.queryParams).toEqual({ user: 'john' }) |
| 98 | + |
| 99 | + act(() => { |
| 100 | + result.current.setQueryParams({ |
| 101 | + lastName: 'Doe', |
| 102 | + version: 1234, |
| 103 | + lib: 'useQueryParams', |
| 104 | + }) |
| 105 | + }) |
| 106 | + |
| 107 | + expect(result.current.queryParams).toEqual({ |
| 108 | + user: 'john', |
| 109 | + lastName: 'Doe', |
| 110 | + version: 1234, |
| 111 | + lib: 'useQueryParams', |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + it('should modify updater and erase old params', () => { |
| 116 | + const updater = (prevState, nextState) => nextState |
| 117 | + const { result } = renderHook(() => useQueryParam({ updater }), { |
| 118 | + wrapper: wrapper({ search: 'user=john' }), |
| 119 | + }) |
| 120 | + |
| 121 | + expect(result.current.queryParams).toEqual({ user: 'john' }) |
| 122 | + act(() => { |
| 123 | + result.current.setQueryParams({ |
| 124 | + lastName: 'Doe', |
| 125 | + }) |
| 126 | + }) |
| 127 | + expect(result.current.queryParams).toEqual({ |
| 128 | + lastName: 'Doe', |
| 129 | + }) |
| 130 | + }) |
| 131 | + |
| 132 | + it('should modify updater and uppercase all news params', () => { |
| 133 | + const updater = (prevState, nextState) => ({ |
| 134 | + ...prevState, |
| 135 | + ...Object.keys(nextState).reduce( |
| 136 | + (acc, key) => ({ ...acc, [key]: nextState[key].toUpperCase() }), |
| 137 | + {}, |
| 138 | + ), |
| 139 | + }) |
| 140 | + |
| 141 | + const { result } = renderHook(() => useQueryParam({ updater }), { |
| 142 | + wrapper: wrapper({ search: 'user=john' }), |
| 143 | + }) |
| 144 | + |
| 145 | + expect(result.current.queryParams).toEqual({ user: 'john' }) |
| 146 | + |
| 147 | + act(() => { |
| 148 | + result.current.setQueryParams({ |
| 149 | + lastName: 'Doe', |
| 150 | + }) |
| 151 | + }) |
| 152 | + |
| 153 | + expect(result.current.queryParams).toEqual({ |
| 154 | + user: 'john', |
| 155 | + lastName: 'DOE', |
| 156 | + }) |
| 157 | + }) |
| 158 | +}) |
0 commit comments