import { act, render, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import React from 'react'; import { createSearchClient } from '../../../../../test/mock'; import { createMultiSearchResponse, createSingleSearchResponse, } from '../../../../../test/mock/createAPIResponse'; import { InstantSearchHooksTestWrapper } from '../../../../../test/utils'; import { InfiniteHits } from '../InfiniteHits'; type CustomHit = { somethingSpecial: string; }; function createMockedSearchClient() { return createSearchClient({ search: jest.fn((requests) => Promise.resolve( createMultiSearchResponse( ...requests.map((request) => { const { hitsPerPage = 3, page = 0 } = request.params!; const hits = Array.from({ length: hitsPerPage }, (_, i) => { const offset = hitsPerPage * page; return { objectID: (i + offset).toString(), somethingSpecial: String.fromCharCode( 'a'.charCodeAt(0) + i + offset ), }; }); return createSingleSearchResponse({ hits, page, nbPages: 10, hitsPerPage, index: request.indexName, }); }) ) ) ), }); } describe('InfiniteHits', () => { test('renders with default props', async () => { const searchClient = createMockedSearchClient(); const { container } = render( ); await waitFor(() => expect(container.querySelectorAll('.ais-InfiniteHits-item')).toHaveLength( 3 ) ); expect(container.querySelector('.ais-InfiniteHits')).toMatchInlineSnapshot(`
  1. {"objectID":"0","somethingSpecial":"a","__position":1} …
  2. {"objectID":"1","somethingSpecial":"b","__position":2} …
  3. {"objectID":"2","somethingSpecial":"c","__position":3} …
`); }); test('renders with a custom hit component', async () => { const searchClient = createMockedSearchClient(); const { container } = render( hitComponent={({ hit }) => ( {`${hit.__position} - ${hit.somethingSpecial}`} )} /> ); await waitFor(() => expect(container.querySelectorAll('strong')).toHaveLength(3) ); expect(container.querySelector('.ais-InfiniteHits')).toMatchInlineSnapshot(`
  1. 1 - a
  2. 2 - b
  3. 3 - c
`); }); test('displays more hits when clicking the "Show More" button', async () => { const searchClient = createMockedSearchClient(); const { container } = render( ( {__position} )} /> ); await waitFor(() => expect(container.querySelectorAll('strong')).toHaveLength(3) ); expect(searchClient.search).toHaveBeenCalledTimes(1); expect(searchClient.search).toHaveBeenLastCalledWith([ { indexName: 'indexName', params: { facets: [], highlightPostTag: '__/ais-highlight__', highlightPreTag: '__ais-highlight__', page: 0, tagFilters: '', }, }, ]); act(() => { userEvent.click(container.querySelector('.ais-InfiniteHits-loadMore')!); }); await waitFor(() => { expect(container.querySelectorAll('strong')).toHaveLength(6); expect(searchClient.search).toHaveBeenCalledTimes(2); expect(searchClient.search).toHaveBeenLastCalledWith([ { indexName: 'indexName', params: { facets: [], highlightPostTag: '__/ais-highlight__', highlightPreTag: '__ais-highlight__', page: 1, tagFilters: '', }, }, ]); }); }); test('displays previous hits when clicking the "Show Previous" button', async () => { const searchClient = createMockedSearchClient(); const { container } = render( ( {__position} )} /> ); await waitFor(() => expect(container.querySelectorAll('strong')).toHaveLength(3) ); expect(searchClient.search).toHaveBeenCalledTimes(1); expect(searchClient.search).toHaveBeenLastCalledWith([ { indexName: 'indexName', params: { facets: [], highlightPostTag: '__/ais-highlight__', highlightPreTag: '__ais-highlight__', page: 3, tagFilters: '', }, }, ]); act(() => { userEvent.click( container.querySelector('.ais-InfiniteHits-loadPrevious')! ); }); await waitFor(() => { expect(container.querySelectorAll('strong')).toHaveLength(6); expect(searchClient.search).toHaveBeenCalledTimes(2); expect(searchClient.search).toHaveBeenLastCalledWith([ { indexName: 'indexName', params: { facets: [], highlightPostTag: '__/ais-highlight__', highlightPreTag: '__ais-highlight__', page: 2, tagFilters: '', }, }, ]); }); }); test('hides the "Show Previous" button when `showPrevious` is `false`', async () => { const searchClient = createMockedSearchClient(); const { container } = render( ); await waitFor(() => { expect(container.querySelector('.ais-InfiniteHits-loadPrevious')).toBe( null ); expect(container).toMatchInlineSnapshot(`
`); }); }); test('marks the "Show Previous" button as disabled on first page', async () => { const searchClient = createMockedSearchClient(); const { container } = render( ); await waitFor(() => expect(searchClient.search).toHaveBeenCalledTimes(1)); expect( container.querySelector( '.ais-InfiniteHits-loadPrevious' ) ).toBeDisabled(); expect( container.querySelector( '.ais-InfiniteHits-loadPrevious' ) ).toHaveClass( 'ais-InfiniteHits-loadPrevious', 'ais-InfiniteHits-loadPrevious--disabled' ); }); test('marks the "Show More" button as disabled on last page', async () => { const searchClient = createMockedSearchClient(); const { container } = render( ); await waitFor(() => expect(searchClient.search).toHaveBeenCalledTimes(1)); expect( container.querySelector('.ais-InfiniteHits-loadMore') ).toBeDisabled(); expect( container.querySelector('.ais-InfiniteHits-loadMore') ).toHaveClass( 'ais-InfiniteHits-loadMore', 'ais-InfiniteHits-loadMore--disabled' ); }); test('forwards custom class names and `div` props to the root element', () => { const searchClient = createMockedSearchClient(); const { container } = render( ); const root = container.firstChild; expect(root).toHaveClass('MyInfiniteHits', 'ROOT'); expect(root).toHaveAttribute('aria-hidden', 'true'); }); test('renders with translations', async () => { const searchClient = createMockedSearchClient(); const { getByRole } = render( ); await waitFor(() => expect(searchClient.search).toHaveBeenCalledTimes(1)); expect(getByRole('button', { name: 'Display more' })).toBeInTheDocument(); expect( getByRole('button', { name: 'Display previous' }) ).toBeInTheDocument(); }); });