|
| 1 | +import { act, renderHook } from '@testing-library/react' |
| 2 | +import router from 'next/router' |
| 3 | + |
| 4 | +import { useQueryAndPagination } from '../nextRouterPagination' |
| 5 | +import { IndexType } from '../types' |
| 6 | + |
| 7 | +jest.mock('next/router', () => require('next-router-mock')) |
| 8 | + |
| 9 | +beforeEach(() => { |
| 10 | + router.query = {} |
| 11 | +}) |
| 12 | + |
| 13 | +test('should initialize pagination', () => { |
| 14 | + const { result } = renderHook(() => useQueryAndPagination()) |
| 15 | + |
| 16 | + expect(result.current.page).toBe(0) |
| 17 | + expect(result.current.size).toBe(15) |
| 18 | +}) |
| 19 | + |
| 20 | +test('should change page', () => { |
| 21 | + const { result } = renderHook(() => useQueryAndPagination()) |
| 22 | + |
| 23 | + act(() => { |
| 24 | + result.current.actions.setPage(2) |
| 25 | + }) |
| 26 | + |
| 27 | + expect(result.current.page).toBe(2) |
| 28 | + expect(router.query.page).toBe('2') |
| 29 | +}) |
| 30 | + |
| 31 | +test('should change search', () => { |
| 32 | + const { result } = renderHook(() => |
| 33 | + useQueryAndPagination({ defaultQueryParameters: { search: '' } }) |
| 34 | + ) |
| 35 | + |
| 36 | + act(() => { |
| 37 | + result.current.actions.updateQuery({ search: 'Max' }) |
| 38 | + }) |
| 39 | + |
| 40 | + expect(result.current.queryParameters.search).toBe('Max') |
| 41 | + expect(router.query.search).toBe('Max') |
| 42 | +}) |
| 43 | + |
| 44 | +test('clear pagination should reset search and page', () => { |
| 45 | + const { result } = renderHook(() => |
| 46 | + useQueryAndPagination({ defaultQueryParameters: { search: '' } }) |
| 47 | + ) |
| 48 | + |
| 49 | + act(() => { |
| 50 | + result.current.actions.updateQuery({ search: 'Max' }) |
| 51 | + }) |
| 52 | + |
| 53 | + act(() => { |
| 54 | + result.current.actions.setPage(2) |
| 55 | + }) |
| 56 | + |
| 57 | + expect(router.query.search).toBe('Max') |
| 58 | + expect(router.query.page).toBe('2') |
| 59 | + |
| 60 | + act(() => { |
| 61 | + result.current.actions.clear() |
| 62 | + }) |
| 63 | + |
| 64 | + expect(result.current.queryParameters.search).toBe('') |
| 65 | + expect(result.current.page).toBe(0) |
| 66 | + expect(router.query.search).toBeUndefined() |
| 67 | + expect(router.query.page).toBeUndefined() |
| 68 | +}) |
| 69 | + |
| 70 | +test('change default parameters', () => { |
| 71 | + const { result } = renderHook(() => |
| 72 | + useQueryAndPagination({ indexType: IndexType.ONE_BASED, pageSize: 10 }) |
| 73 | + ) |
| 74 | + |
| 75 | + expect(result.current.page).toBe(1) |
| 76 | + expect(result.current.size).toBe(10) |
| 77 | +}) |
| 78 | + |
| 79 | +test('on search change -> page should be reset', () => { |
| 80 | + const { result } = renderHook(() => |
| 81 | + useQueryAndPagination({ defaultQueryParameters: { search: '' } }) |
| 82 | + ) |
| 83 | + |
| 84 | + act(() => { |
| 85 | + result.current.actions.setPage(2) |
| 86 | + }) |
| 87 | + |
| 88 | + expect(result.current.page).toBe(2) |
| 89 | + expect(router.query.page).toBe('2') |
| 90 | + |
| 91 | + act(() => { |
| 92 | + result.current.actions.updateQuery({ search: 'Max' }) |
| 93 | + }) |
| 94 | + |
| 95 | + expect(result.current.queryParameters.search).toBe('Max') |
| 96 | + expect(result.current.page).toBe(0) |
| 97 | + expect(router.query.search).toBe('Max') |
| 98 | + expect(router.query.page).toBeUndefined() |
| 99 | +}) |
| 100 | + |
| 101 | +test('query multiple different properties, should keep them all', () => { |
| 102 | + const { result } = renderHook(() => |
| 103 | + useQueryAndPagination({ |
| 104 | + defaultQueryParameters: { search: '', department: '' }, |
| 105 | + }) |
| 106 | + ) |
| 107 | + |
| 108 | + act(() => { |
| 109 | + result.current.actions.updateQuery({ search: 'Max' }) |
| 110 | + }) |
| 111 | + |
| 112 | + act(() => { |
| 113 | + result.current.actions.updateQuery({ department: 'IT' }) |
| 114 | + }) |
| 115 | + |
| 116 | + expect(result.current.queryParameters.search).toBe('Max') |
| 117 | + expect(result.current.queryParameters.department).toBe('IT') |
| 118 | +}) |
| 119 | + |
| 120 | +test('query a property that is not configured, should do nothing', () => { |
| 121 | + const { result } = renderHook(() => |
| 122 | + useQueryAndPagination({ |
| 123 | + defaultQueryParameters: { search: '' }, |
| 124 | + }) |
| 125 | + ) |
| 126 | + |
| 127 | + act(() => { |
| 128 | + result.current.actions.updateQuery({ department: 'IT' }) |
| 129 | + }) |
| 130 | + |
| 131 | + expect(result.current.queryParameters.search).toBe('') |
| 132 | + expect(result.current.queryParameters.department).toBeUndefined() |
| 133 | +}) |
| 134 | + |
| 135 | +test('properties in the URL, that are not part of the configuration should be left untouched', () => { |
| 136 | + router.query = { greeting: 'hello' } |
| 137 | + |
| 138 | + const { result } = renderHook(() => |
| 139 | + useQueryAndPagination({ |
| 140 | + defaultQueryParameters: { search: '' }, |
| 141 | + }) |
| 142 | + ) |
| 143 | + |
| 144 | + act(() => { |
| 145 | + result.current.actions.updateQuery({ search: 'Max' }) |
| 146 | + }) |
| 147 | + |
| 148 | + expect(result.current.queryParameters.search).toBe('Max') |
| 149 | + expect(result.current.queryParameters.greeting).toBeUndefined() |
| 150 | + expect(router.query.greeting).toBe('hello') |
| 151 | +}) |
| 152 | + |
| 153 | +test('query property with default value, should remove it from url', () => { |
| 154 | + router.query = { search: 'Max' } |
| 155 | + |
| 156 | + const { result } = renderHook(() => |
| 157 | + useQueryAndPagination({ |
| 158 | + defaultQueryParameters: { search: '' }, |
| 159 | + }) |
| 160 | + ) |
| 161 | + |
| 162 | + act(() => { |
| 163 | + result.current.actions.updateQuery({ search: '' }) |
| 164 | + }) |
| 165 | + |
| 166 | + expect(result.current.queryParameters.search).toBe('') |
| 167 | + expect(router.query.search).toBeUndefined() |
| 168 | +}) |
| 169 | + |
| 170 | +test('query property with empty value and different default value', () => { |
| 171 | + const { result } = renderHook(() => |
| 172 | + useQueryAndPagination({ |
| 173 | + defaultQueryParameters: { search: 'Default search' }, |
| 174 | + }) |
| 175 | + ) |
| 176 | + |
| 177 | + act(() => { |
| 178 | + result.current.actions.updateQuery({ search: '' }) |
| 179 | + }) |
| 180 | + |
| 181 | + expect(result.current.queryParameters.search).toBe('') |
| 182 | + expect(router.query.search).toBe('') |
| 183 | +}) |
0 commit comments