import { render, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import React from 'react'; import { useRefinementList } from 'react-instantsearch-hooks'; import { createSearchClient } from '../../../../../test/mock'; import { InstantSearchHooksTestWrapper } from '../../../../../test/utils'; import { CurrentRefinements } from '../CurrentRefinements'; import type { UseRefinementListProps } from 'react-instantsearch-hooks'; describe('CurrentRefinements', () => { test('renders with default props', async () => { const { container } = render( ); await waitFor(() => { expect( document.querySelectorAll('.ais-CurrentRefinements-item') ).toHaveLength(2); expect( document.querySelectorAll('.ais-CurrentRefinements-category') ).toHaveLength(3); }); expect(container).toMatchInlineSnapshot(`
`); }); test('renders with a specific set of class names when there are no refinements', async () => { const searchClient = createSearchClient({}); const { container } = render( ); await waitFor(() => expect(searchClient.search).toHaveBeenCalledTimes(1)); expect(container).toMatchInlineSnapshot(`
`); }); test('clears a refinement', async () => { const { container, queryByText } = render( ); await waitFor(() => { expect( document.querySelectorAll('.ais-CurrentRefinements-item') ).toHaveLength(2); expect( document.querySelectorAll('.ais-CurrentRefinements-category') ).toHaveLength(3); }); expect(container).toMatchInlineSnapshot(`
`); const [button1, button2, button3] = document.querySelectorAll( '.ais-CurrentRefinements-delete' ); userEvent.click(button3); await waitFor(() => { expect(queryByText('Audio')).toBeNull(); expect(queryByText('Apple')).not.toBeNull(); expect(queryByText('Samsung')).not.toBeNull(); expect( document.querySelectorAll('.ais-CurrentRefinements-item') ).toHaveLength(1); expect( document.querySelectorAll('.ais-CurrentRefinements-category') ).toHaveLength(2); }); userEvent.click(button1); await waitFor(() => { expect(queryByText('Audio')).toBeNull(); expect(queryByText('Apple')).toBeNull(); expect(queryByText('Samsung')).not.toBeNull(); expect( document.querySelectorAll('.ais-CurrentRefinements-item') ).toHaveLength(1); expect( document.querySelectorAll('.ais-CurrentRefinements-category') ).toHaveLength(1); }); userEvent.click(button2); await waitFor(() => { expect(queryByText('Audio')).toBeNull(); expect(queryByText('Apple')).toBeNull(); expect(queryByText('Samsung')).toBeNull(); expect( document.querySelectorAll('.ais-CurrentRefinements-item') ).toHaveLength(0); expect( document.querySelectorAll('.ais-CurrentRefinements-category') ).toHaveLength(0); }); }); test('does not trigger default event', async () => { const searchClient = createSearchClient({}); const onSubmit = jest.fn(); render(
); await waitFor(() => expect(searchClient.search).toHaveBeenCalledTimes(1)); userEvent.click( document.querySelector( '.ais-CurrentRefinements-delete' ) as HTMLButtonElement ); await waitFor(() => expect(onSubmit).not.toHaveBeenCalled()); }); test('does not clear when pressing a modifier key', async () => { const searchClient = createSearchClient({}); const { container } = render( ); await waitFor(() => expect(searchClient.search).toHaveBeenCalledTimes(1)); expect( document.querySelectorAll('.ais-CurrentRefinements-item') ).toHaveLength(1); expect(container).toMatchInlineSnapshot(`
`); const button = document.querySelector( '.ais-CurrentRefinements-delete' ) as HTMLButtonElement; userEvent.click(button, { button: 1 }); userEvent.click(button, { altKey: true }); userEvent.click(button, { ctrlKey: true }); userEvent.click(button, { metaKey: true }); userEvent.click(button, { shiftKey: true }); await waitFor(() => expect( document.querySelectorAll('.ais-CurrentRefinements-item') ).toHaveLength(1) ); expect(container).toMatchInlineSnapshot(`
`); }); test('inclusively restricts what refinements to display', async () => { const searchClient = createSearchClient({}); const { container, queryByText } = render( ); await waitFor(() => expect(searchClient.search).toHaveBeenCalledTimes(1)); expect(queryByText('Apple')).toBeNull(); expect(queryByText('Audio')).not.toBeNull(); expect(container).toMatchInlineSnapshot(`
`); }); test('exclusively restricts what refinements to display', async () => { const searchClient = createSearchClient({}); const { container, queryByText } = render( ); await waitFor(() => expect(searchClient.search).toHaveBeenCalledTimes(1)); expect(queryByText('Apple')).toBeNull(); expect(queryByText('Audio')).not.toBeNull(); expect(container).toMatchInlineSnapshot(`
`); }); test('restricts what refinements to display with custom logic', async () => { const searchClient = createSearchClient({}); const { container, queryByText } = render( items.filter((item) => item.attribute !== 'brand') } /> ); await waitFor(() => expect(searchClient.search).toHaveBeenCalledTimes(1)); expect(queryByText('Apple')).toBeNull(); expect(queryByText('Audio')).not.toBeNull(); expect(container).toMatchInlineSnapshot(`
`); }); test('forwards custom class names and `div` props to the root element', () => { const { container } = render( ); const root = container.firstChild; expect(root).toHaveClass('MyCurrentRefinements', 'ROOT'); expect(root).toHaveAttribute('title', 'Some custom title'); }); }); function VirtualRefinementList(props: UseRefinementListProps) { useRefinementList(props); return null; }