import { render } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import React from 'react'; import { Hits } from '../Hits'; import type { HitsProps } from '../Hits'; import type { Hit } from 'instantsearch.js'; describe('Hits', () => { function createProps( props: Partial> ) { return { hits: [ { objectID: 'abc', __position: 1 }, { objectID: 'def', __position: 2 }, ] as THit[], sendEvent: jest.fn(), ...props, }; } test('renders with default props', () => { const props = createProps({}); const { container } = render(); expect(container).toMatchInlineSnapshot(`
  1. {"objectID":"abc","__position":1} …
  2. {"objectID":"def","__position":2} …
`); }); test('renders with custom hitComponent', () => { const props = createProps({ hits: [ { objectID: 'abc', __position: 1, something: true }, { objectID: 'def', __position: 2, something: false }, ], hitComponent: ({ hit }) => ( {`${hit.objectID} - ${hit.something}`} ), }); const { container } = render(); expect(container.querySelectorAll('strong')).toHaveLength(2); expect(container).toMatchInlineSnapshot(`
  1. abc - true
  2. def - false
`); }); test('passes sendEvent to hitComponent', () => { const props = createProps({ hitComponent: ({ hit, sendEvent }) => ( ), }); const { container } = render(); userEvent.click(container.querySelector('button')!); expect(props.sendEvent).toHaveBeenCalledTimes(1); expect(props.sendEvent).toHaveBeenLastCalledWith(props.hits[0]); }); test('accepts custom class names', () => { const props = createProps({ className: 'MyCustomHits', classNames: { root: 'ROOT', list: 'LIST', item: 'ITEM', }, }); const { container } = render(); expect(container).toMatchInlineSnapshot(`
  1. {"objectID":"abc","__position":1} …
  2. {"objectID":"def","__position":2} …
`); }); test('accepts custom class names (empty)', () => { const props = createProps({ hits: [], className: 'MyCustomHits', classNames: { root: 'ROOT', emptyRoot: 'EMPTYROOT', list: 'LIST', }, }); const { container } = render(); expect(container).toMatchInlineSnapshot(`
`); }); test('renders with custom div props', () => { const props = createProps({ hidden: true }); const { container } = render(); expect(container.querySelector('.ais-Hits')!.hidden).toBe( true ); }); });