|
| 1 | +import { |
| 2 | + createSearchClient, |
| 3 | + createMultiSearchResponse, |
| 4 | + createSingleSearchResponse, |
| 5 | +} from '@instantsearch/mocks'; |
| 6 | +import { wait } from '@instantsearch/testutils'; |
| 7 | +import userEvent from '@testing-library/user-event'; |
| 8 | + |
| 9 | +import type { BreadcrumbWidgetSetup } from '.'; |
| 10 | +import type { TestOptions } from '../../common'; |
| 11 | + |
| 12 | +export function createLinksTests( |
| 13 | + setup: BreadcrumbWidgetSetup, |
| 14 | + { act }: Required<TestOptions> |
| 15 | +) { |
| 16 | + describe('links', () => { |
| 17 | + test("a click on a link with modifier doesn't search", async () => { |
| 18 | + const delay = 100; |
| 19 | + const margin = 10; |
| 20 | + const attributes = ['1', '2']; |
| 21 | + const options = { |
| 22 | + instantSearchOptions: { |
| 23 | + indexName: 'indexName', |
| 24 | + initialUiState: { |
| 25 | + indexName: { |
| 26 | + hierarchicalMenu: { |
| 27 | + [attributes[0]]: ['Cameras & Camcorders > Digital Cameras'], |
| 28 | + }, |
| 29 | + }, |
| 30 | + }, |
| 31 | + searchClient: createSearchClient({ |
| 32 | + search: jest.fn(async (requests) => { |
| 33 | + await wait(delay); |
| 34 | + return createMultiSearchResponse( |
| 35 | + ...requests.map(() => |
| 36 | + createSingleSearchResponse({ |
| 37 | + facets: { |
| 38 | + [attributes[0]]: { |
| 39 | + 'Cameras & Camcorders': 1369, |
| 40 | + }, |
| 41 | + [attributes[1]]: { |
| 42 | + 'Cameras & Camcorders > Digital Cameras': 170, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }) |
| 46 | + ) |
| 47 | + ); |
| 48 | + }), |
| 49 | + }), |
| 50 | + }, |
| 51 | + widgetParams: { attributes }, |
| 52 | + }; |
| 53 | + |
| 54 | + await setup(options); |
| 55 | + |
| 56 | + // Wait for initial results to populate widgets with data |
| 57 | + await act(async () => { |
| 58 | + await wait(margin + delay); |
| 59 | + await wait(0); |
| 60 | + }); |
| 61 | + |
| 62 | + const container = document.querySelector('.ais-Breadcrumb')!; |
| 63 | + |
| 64 | + // Initial state, before interaction |
| 65 | + { |
| 66 | + expect(container.querySelectorAll('a')).toHaveLength(2); |
| 67 | + expect( |
| 68 | + options.instantSearchOptions.searchClient.search |
| 69 | + ).toHaveBeenCalledTimes(1); |
| 70 | + } |
| 71 | + |
| 72 | + // Click all links with a modifier |
| 73 | + { |
| 74 | + await act(async () => { |
| 75 | + container.querySelectorAll('a').forEach((link) => { |
| 76 | + userEvent.click(link, { ctrlKey: true }); |
| 77 | + }); |
| 78 | + |
| 79 | + await wait(0); |
| 80 | + await wait(0); |
| 81 | + }); |
| 82 | + |
| 83 | + // No UI has changed |
| 84 | + expect(container.querySelectorAll('a')).toHaveLength(2); |
| 85 | + expect( |
| 86 | + options.instantSearchOptions.searchClient.search |
| 87 | + ).toHaveBeenCalledTimes(1); |
| 88 | + } |
| 89 | + |
| 90 | + // Wait for new results to come in |
| 91 | + { |
| 92 | + await act(async () => { |
| 93 | + await wait(delay + margin); |
| 94 | + }); |
| 95 | + |
| 96 | + expect(container.querySelectorAll('a')).toHaveLength(2); |
| 97 | + expect( |
| 98 | + options.instantSearchOptions.searchClient.search |
| 99 | + ).toHaveBeenCalledTimes(1); |
| 100 | + } |
| 101 | + }); |
| 102 | + }); |
| 103 | +} |
0 commit comments