import { render } from '@testing-library/react'; import React from 'react'; import { Highlight } from '../Highlight'; describe('Highlight', () => { test('renders single match', () => { const { container } = render( test', }, }, }} attribute="data" /> ); expect(container).toMatchInlineSnapshot(`
te st
`); }); test('renders list of matches', () => { const { container } = render( test', }, { matchedWords: [], matchLevel: 'none', value: 'nothing' }, ], }, }} attribute="data" /> ); expect(container).toMatchInlineSnapshot(`
te st , nothing
`); }); test('renders path to match', () => { const { container } = render( test', }, }, }, }} // @ts-expect-error TypeScript type does not accept string, as a way to discourage it attribute="data.subdata" /> ); expect(container).toMatchInlineSnapshot(`
te st
`); }); test('renders path to match as array', () => { const { container } = render( test', }, }, }, }} attribute={['data', 'subdata']} /> ); expect(container).toMatchInlineSnapshot(`
te st
`); }); test("renders nothing when there's no match", () => { const { container } = render( ); expect(container).toMatchInlineSnapshot(`
`); }); test("doesn't render html escaped content", () => { const { container } = render( don't <script>alert("xss");</script>', }, }, }} attribute="data" /> ); expect(container).toMatchInlineSnapshot(`
don 't <script>alert("xss");</script>
`); }); test('forwards tag names and separator', () => { function Highlighted({ children }) { return {children}; } function NonHighlighted({ children }) { return {children}; } const { container } = render( - } hit={{ objectID: '1', __position: 1, array: ['item1', 'item2'], _highlightResult: { array: [ { matchLevel: 'partial', matchedWords: [], value: 'item1', }, { matchLevel: 'none', matchedWords: [], value: 'item2' }, ], }, }} attribute="array" /> ); expect(container).toMatchInlineSnapshot(`
it em1 - item2
`); }); test('forwards custom class names and `div` props to the root element', () => { const { container } = render( ); const root = container.firstChild; expect(root).toHaveClass('MyHighlight', 'ROOT'); expect(root).toHaveAttribute('aria-hidden', 'true'); }); });