vitess.io/vitess@v0.16.2/web/vtadmin/src/hooks/useURLPagination.test.tsx (about) 1 /* eslint-disable jest/no-conditional-expect */ 2 /** 3 * Copyright 2021 The Vitess Authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 import { renderHook } from '@testing-library/react-hooks'; 18 import { createMemoryHistory, To } from 'history'; 19 import { Router } from 'react-router-dom'; 20 import { PaginationOpts, PaginationParams, useURLPagination } from './useURLPagination'; 21 22 describe('useURLPagination', () => { 23 const tests: { 24 name: string; 25 url: string; 26 opts: PaginationOpts; 27 // URL query parameters after any redirects have taken place 28 expected: PaginationParams; 29 // If defined, checks whether history.replace was called with the given parameters. 30 // If null, checks that history.replace was not called. (Unfortunately, we can't 31 // make this an optional param, else Jest times out because the length of 32 // callback args must be consistent between tests.) 33 redirectParams: To | null; 34 }[] = [ 35 { 36 name: 'returns pagination parameters in the URL', 37 url: '/test?page=1&foo=bar', 38 opts: { totalPages: 10 }, 39 expected: { page: 1 }, 40 redirectParams: null, 41 }, 42 { 43 name: 'assumes an undefined page parameter is the first page', 44 url: '/test?foo=bar', 45 opts: { totalPages: 10 }, 46 expected: { page: 1 }, 47 redirectParams: null, 48 }, 49 { 50 name: 'redirects to the first page if current page > total pages', 51 url: '/test?page=100&foo=bar', 52 opts: { totalPages: 10 }, 53 expected: { page: 1 }, 54 redirectParams: { search: '?foo=bar&page=1' }, 55 }, 56 { 57 name: 'redirects to the first page if current page is a negative number', 58 url: '/test?page=-123&foo=bar', 59 opts: { totalPages: 10 }, 60 expected: { page: 1 }, 61 redirectParams: { search: '?foo=bar&page=1' }, 62 }, 63 { 64 name: 'redirects to the first page if current page is not a number', 65 url: '/test?page=abc&foo=bar', 66 opts: { totalPages: 10 }, 67 expected: { page: 1 }, 68 redirectParams: { search: '?foo=bar&page=1' }, 69 }, 70 { 71 name: 'does not redirect if totalPages is 0', 72 url: '/test?page=100&foo=bar', 73 opts: { totalPages: 0 }, 74 expected: { page: 100 }, 75 redirectParams: null, 76 }, 77 ]; 78 79 test.concurrent.each(tests.map(Object.values))( 80 '%s', 81 (name: string, url: string, opts: PaginationOpts, expected: PaginationParams, redirectParams: To | null) => { 82 const history = createMemoryHistory({ initialEntries: [url] }); 83 jest.spyOn(history, 'replace'); 84 85 const { result } = renderHook(() => useURLPagination(opts), { 86 wrapper: ({ children }) => { 87 return <Router history={history}>{children}</Router>; 88 }, 89 }); 90 91 expect(result.current).toEqual(expected); 92 93 if (redirectParams) { 94 expect(history.replace).toHaveBeenCalledWith(redirectParams); 95 } else { 96 expect(history.replace).not.toHaveBeenCalled(); 97 } 98 } 99 ); 100 });