|
| 1 | +import * as React from 'react' |
| 2 | +import { render } from '@testing-library/react' |
| 3 | +import { MemoryRouter } from 'react-router-dom' |
| 4 | + |
| 5 | +import Results from './Results' |
| 6 | + |
| 7 | +jest.mock('@material-ui/core', () => ({ |
| 8 | + Button: ({ children }: React.PropsWithChildren<{}>) => <button>{children}</button>, |
| 9 | + Icon: ({ children }: React.PropsWithChildren<{}>) => <span>{children}</span>, |
| 10 | + makeStyles: () => () => ({}), |
| 11 | + useTheme: () => ({ |
| 12 | + breakpoints: { down: () => false }, |
| 13 | + spacing: (x: number) => x * 8, |
| 14 | + }), |
| 15 | + useMediaQuery: () => false, |
| 16 | +})) |
| 17 | + |
| 18 | +jest.mock('@material-ui/icons', () => ({ |
| 19 | + GridOn: () => 'table icon', |
| 20 | + List: () => 'list icon', |
| 21 | +})) |
| 22 | + |
| 23 | +jest.mock('@material-ui/lab', () => ({ |
| 24 | + Skeleton: () => <div>Loading…</div>, |
| 25 | + ToggleButtonGroup: ({ |
| 26 | + value, |
| 27 | + children, |
| 28 | + }: React.PropsWithChildren<{ value: string }>) => ( |
| 29 | + <ul data-selected={value}>{children}</ul> |
| 30 | + ), |
| 31 | + ToggleButton: ({ children, value }: React.PropsWithChildren<{ value: string }>) => ( |
| 32 | + <li data-value={value}>{children}</li> |
| 33 | + ), |
| 34 | +})) |
| 35 | + |
| 36 | +const model = { |
| 37 | + state: { |
| 38 | + resultType: 'p', // QuiltPackage |
| 39 | + view: 'l', // List |
| 40 | + searchString: 'test', |
| 41 | + buckets: ['test-bucket'], |
| 42 | + order: 'BEST_MATCH', |
| 43 | + filter: { |
| 44 | + predicates: {}, |
| 45 | + order: [], |
| 46 | + }, |
| 47 | + userMetaFilters: { |
| 48 | + filters: new Map(), |
| 49 | + }, |
| 50 | + latestOnly: true, |
| 51 | + }, |
| 52 | + actions: { |
| 53 | + setView: jest.fn(), |
| 54 | + setOrder: jest.fn(), |
| 55 | + }, |
| 56 | + firstPageQuery: { |
| 57 | + _tag: 'fetching', |
| 58 | + } as any, |
| 59 | + baseSearchQuery: { |
| 60 | + _tag: 'fetching', |
| 61 | + } as any, |
| 62 | +} |
| 63 | + |
| 64 | +jest.mock('../model', () => ({ |
| 65 | + use: () => model, |
| 66 | + ResultType: { |
| 67 | + QuiltPackage: 'p', |
| 68 | + S3Object: 'o', |
| 69 | + }, |
| 70 | + View: { |
| 71 | + Table: 't', |
| 72 | + List: 'l', |
| 73 | + }, |
| 74 | +})) |
| 75 | + |
| 76 | +jest.mock('containers/Bucket/PackageDialog/PackageCreationForm', () => ({ |
| 77 | + usePackageCreationDialog: () => ({ |
| 78 | + open: jest.fn(), |
| 79 | + render: () => <>Don't forget to render dialog</>, |
| 80 | + }), |
| 81 | +})) |
| 82 | + |
| 83 | +jest.mock('containers/Bucket/Routes', () => ({ |
| 84 | + useBucketStrict: () => 'test-bucket', |
| 85 | +})) |
| 86 | + |
| 87 | +jest.mock('./ColumnTitle', () => ({ children }: React.PropsWithChildren<{}>) => ( |
| 88 | + <div>Column Title: {children}</div> |
| 89 | +)) |
| 90 | + |
| 91 | +jest.mock('../Sort', () => () => <div>Sort Selector</div>) |
| 92 | + |
| 93 | +jest.mock('utils/NamedRoutes', () => ({ |
| 94 | + use: () => ({ |
| 95 | + paths: { |
| 96 | + bucketRoot: '/b/:bucket', |
| 97 | + }, |
| 98 | + }), |
| 99 | +})) |
| 100 | + |
| 101 | +describe('containers/Search/Layout/Results', () => { |
| 102 | + beforeEach(() => { |
| 103 | + jest.clearAllMocks() |
| 104 | + model.firstPageQuery = { _tag: 'fetching' } |
| 105 | + model.state.resultType = 'p' |
| 106 | + }) |
| 107 | + |
| 108 | + it('renders with loading state', () => { |
| 109 | + const { container } = render( |
| 110 | + <MemoryRouter> |
| 111 | + <Results /> |
| 112 | + </MemoryRouter>, |
| 113 | + ) |
| 114 | + expect(container).toMatchSnapshot() |
| 115 | + }) |
| 116 | + |
| 117 | + it('renders with data and shows number of results', () => { |
| 118 | + model.firstPageQuery = { |
| 119 | + _tag: 'data', |
| 120 | + data: { |
| 121 | + __typename: 'PackagesSearchResultSet', |
| 122 | + total: 5, |
| 123 | + }, |
| 124 | + } |
| 125 | + |
| 126 | + const { container } = render( |
| 127 | + <MemoryRouter> |
| 128 | + <Results /> |
| 129 | + </MemoryRouter>, |
| 130 | + ) |
| 131 | + expect(container).toMatchSnapshot() |
| 132 | + }) |
| 133 | + |
| 134 | + it('renders with FiltersButton when onFilters prop is provided', () => { |
| 135 | + model.firstPageQuery = { |
| 136 | + _tag: 'data', |
| 137 | + data: { |
| 138 | + __typename: 'PackagesSearchResultSet', |
| 139 | + total: 3, |
| 140 | + }, |
| 141 | + } |
| 142 | + |
| 143 | + const { container } = render( |
| 144 | + <MemoryRouter> |
| 145 | + <Results onFilters={jest.fn()} /> |
| 146 | + </MemoryRouter>, |
| 147 | + ) |
| 148 | + expect(container).toMatchSnapshot() |
| 149 | + }) |
| 150 | + |
| 151 | + it('does not show ToggleResultsView for S3Object result type', () => { |
| 152 | + model.state.resultType = 'o' // S3Object |
| 153 | + model.firstPageQuery = { |
| 154 | + _tag: 'data', |
| 155 | + data: { |
| 156 | + __typename: 'ObjectsSearchResultSet', |
| 157 | + total: 5, |
| 158 | + }, |
| 159 | + } |
| 160 | + |
| 161 | + const { container } = render( |
| 162 | + <MemoryRouter> |
| 163 | + <Results /> |
| 164 | + </MemoryRouter>, |
| 165 | + ) |
| 166 | + expect(container).toMatchSnapshot() |
| 167 | + }) |
| 168 | + |
| 169 | + it('renders error state', () => { |
| 170 | + model.firstPageQuery = { |
| 171 | + _tag: 'error', |
| 172 | + error: new Error('Test error'), |
| 173 | + } |
| 174 | + |
| 175 | + const { container } = render( |
| 176 | + <MemoryRouter> |
| 177 | + <Results /> |
| 178 | + </MemoryRouter>, |
| 179 | + ) |
| 180 | + expect(container).toMatchSnapshot() |
| 181 | + }) |
| 182 | + |
| 183 | + it('shows Create Package button in bucket', () => { |
| 184 | + model.firstPageQuery = { |
| 185 | + _tag: 'data', |
| 186 | + data: { |
| 187 | + __typename: 'PackagesSearchResultSet', |
| 188 | + total: 5, |
| 189 | + }, |
| 190 | + } |
| 191 | + const { container } = render( |
| 192 | + <MemoryRouter initialEntries={['/b/test-bucket/packages/my-package']}> |
| 193 | + <Results /> |
| 194 | + </MemoryRouter>, |
| 195 | + ) |
| 196 | + expect(container).toMatchSnapshot() |
| 197 | + }) |
| 198 | +}) |
0 commit comments