import { render } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import React from 'react'; import { Breadcrumb } from '../Breadcrumb'; import type { BreadcrumbProps } from '../Breadcrumb'; describe('Breadcrumb', () => { function createProps({ translations, ...props }: Partial = {}): BreadcrumbProps { const createURL = jest.fn((value) => `#${value}`); const onNavigate = jest.fn(); return { items: [ { label: 'Audio', value: 'Audio' }, { label: 'Home audio', value: 'Home audio' }, ], hasItems: true, createURL, onNavigate, separator: '>', translations: { rootElementText: 'Home', ...translations, }, ...props, }; } test('renders with props', () => { const props = createProps(); const { container } = render(); expect(container).toMatchInlineSnapshot(`
`); }); test('renders with translations', () => { const props = createProps({ translations: { rootElementText: 'Index', }, }); const { container } = render(); expect(container).toMatchInlineSnapshot(`
`); }); test('renders with custom separator', () => { const props = createProps({ separator: '/' }); const { container } = render(); expect(container.querySelectorAll('.ais-Breadcrumb-separator')) .toMatchInlineSnapshot(` NodeList [ , , ] `); }); test('calls an `onNavigate` callback when selecting an item', () => { const props = createProps(); render(); userEvent.click( document.querySelector( '.ais-Breadcrumb-link[href="#Audio"]' ) as HTMLAnchorElement ); expect(props.onNavigate).toHaveBeenCalledTimes(1); }); test('accepts custom class names', () => { const props = createProps({ className: 'MyCustomBreadcrumb', classNames: { root: 'ROOT', noRefinementRoot: 'NOREFINEMENTROOT', list: 'LIST', item: 'ITEM', selectedItem: 'SELECTEDITEM', separator: 'SEPARATOR', link: 'LINK', }, }); const { container } = render(); expect(container).toMatchInlineSnapshot(`
`); }); test('forwards `div` props to the root element', () => { const props = createProps(); const { container } = render( ); expect(container.querySelector('.ais-Breadcrumb')).toHaveAttribute( 'title', 'Some custom title' ); }); });