|
| 1 | +import React from 'react'; |
| 2 | +import { act } from 'react-test-renderer'; |
| 3 | + |
| 4 | +import { useSearchPage } from '../useSearchPage'; |
| 5 | +import createTestInstance from '../../../util/createTestInstance'; |
| 6 | + |
| 7 | +jest.mock('react-router-dom', () => ({ |
| 8 | + useHistory: jest.fn(() => ({ push: jest.fn() })), |
| 9 | + useLocation: jest.fn(() => ({ pathname: '', search: '' })) |
| 10 | +})); |
| 11 | + |
| 12 | +jest.mock('../../../context/app', () => { |
| 13 | + const state = {}; |
| 14 | + const api = { |
| 15 | + toggleDrawer: jest.fn() |
| 16 | + }; |
| 17 | + const useAppContext = jest.fn(() => [state, api]); |
| 18 | + |
| 19 | + return { useAppContext }; |
| 20 | +}); |
| 21 | + |
| 22 | +const mockUseSort = jest |
| 23 | + .fn() |
| 24 | + .mockReturnValue([ |
| 25 | + { |
| 26 | + sortText: 'Best Match', |
| 27 | + sortAttribute: 'relevance', |
| 28 | + sortDirection: 'DESC' |
| 29 | + }, |
| 30 | + jest.fn() |
| 31 | + ]) |
| 32 | + .mockName('mockUseSort'); |
| 33 | + |
| 34 | +const mockSetCurrentPage = jest.fn().mockName('mockSetCurrentPage'); |
| 35 | + |
| 36 | +jest.mock('@magento/peregrine', () => { |
| 37 | + const usePagination = jest.fn(() => [ |
| 38 | + { |
| 39 | + currentPage: 3, |
| 40 | + totalPages: 6 |
| 41 | + }, |
| 42 | + { |
| 43 | + setCurrentPage: jest |
| 44 | + .fn() |
| 45 | + .mockImplementation(() => mockSetCurrentPage()), |
| 46 | + setTotalPages: jest.fn() |
| 47 | + } |
| 48 | + ]); |
| 49 | + const useSort = jest.fn().mockImplementation(() => mockUseSort()); |
| 50 | + |
| 51 | + return { |
| 52 | + useSort, |
| 53 | + usePagination |
| 54 | + }; |
| 55 | +}); |
| 56 | + |
| 57 | +jest.mock('@apollo/react-hooks', () => { |
| 58 | + const useQuery = jest.fn().mockReturnValue({ |
| 59 | + data: { |
| 60 | + __type: { |
| 61 | + inputFields: [] |
| 62 | + } |
| 63 | + }, |
| 64 | + error: null, |
| 65 | + loading: false |
| 66 | + }); |
| 67 | + const runQuery = jest.fn(); |
| 68 | + const queryResult = { |
| 69 | + data: { |
| 70 | + products: { |
| 71 | + page_info: { |
| 72 | + total_pages: 6 |
| 73 | + } |
| 74 | + } |
| 75 | + }, |
| 76 | + error: null, |
| 77 | + loading: false |
| 78 | + }; |
| 79 | + const useLazyQuery = jest.fn(() => [runQuery, queryResult]); |
| 80 | + |
| 81 | + return { useLazyQuery, useQuery }; |
| 82 | +}); |
| 83 | + |
| 84 | +const mockProps = { |
| 85 | + queries: { |
| 86 | + filterIntrospection: 'filterIntrospectionQuery', |
| 87 | + getProductFiltersBySearch: 'getProductFiltersBySearchQuery', |
| 88 | + productSearch: 'productSearchQuery' |
| 89 | + } |
| 90 | +}; |
| 91 | + |
| 92 | +const Component = props => { |
| 93 | + const talonProps = useSearchPage(props); |
| 94 | + return <i talonProps={talonProps} />; |
| 95 | +}; |
| 96 | + |
| 97 | +const tree = createTestInstance(<Component {...mockProps} />); |
| 98 | + |
| 99 | +test('returns the correct shape', () => { |
| 100 | + const { root } = tree; |
| 101 | + const { talonProps } = root.findByType('i').props; |
| 102 | + |
| 103 | + expect(talonProps).toMatchSnapshot(); |
| 104 | +}); |
| 105 | + |
| 106 | +const testCases = [ |
| 107 | + [ |
| 108 | + 'sortText does not reset', |
| 109 | + { |
| 110 | + sortText: 'Changed', |
| 111 | + sortAttribute: 'relevance', |
| 112 | + sortDirection: 'DESC' |
| 113 | + }, |
| 114 | + 0 |
| 115 | + ], |
| 116 | + [ |
| 117 | + 'sortAttribute resets', |
| 118 | + { |
| 119 | + sortText: 'Best Match', |
| 120 | + sortAttribute: 'Changed', |
| 121 | + sortDirection: 'DESC' |
| 122 | + }, |
| 123 | + 1 |
| 124 | + ], |
| 125 | + [ |
| 126 | + 'sortDirection resets', |
| 127 | + { |
| 128 | + sortText: 'Best Match', |
| 129 | + sortAttribute: 'relevance', |
| 130 | + sortDirection: 'Changed' |
| 131 | + }, |
| 132 | + 1 |
| 133 | + ] |
| 134 | +]; |
| 135 | + |
| 136 | +test.each(testCases)( |
| 137 | + 'Changing %s current page to 1.', |
| 138 | + (description, sortParams, expected) => { |
| 139 | + mockUseSort.mockReturnValueOnce([sortParams, jest.fn()]); |
| 140 | + act(() => { |
| 141 | + tree.update(<Component {...mockProps} />); |
| 142 | + }); |
| 143 | + |
| 144 | + expect(mockSetCurrentPage).toHaveBeenCalledTimes(expected); |
| 145 | + } |
| 146 | +); |
0 commit comments