import { render, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';
import {
useCurrentRefinements,
useRefinementList,
} from 'react-instantsearch-hooks';
import { createSearchClient } from '../../../../../test/mock';
import { InstantSearchHooksTestWrapper } from '../../../../../test/utils';
import { ClearRefinements } from '../ClearRefinements';
import type {
UseRefinementListProps,
UseCurrentRefinementsProps,
} from 'react-instantsearch-hooks';
describe('ClearRefinements', () => {
test('renders with default props', async () => {
const searchClient = createSearchClient({});
const { container } = render(
);
await waitFor(() => expect(searchClient.search).toHaveBeenCalledTimes(1));
expect(container).toMatchInlineSnapshot(`
`);
});
test('renders with a disabled button when there are no refinements', async () => {
const searchClient = createSearchClient({});
const { container } = render(
);
await waitFor(() => expect(searchClient.search).toHaveBeenCalledTimes(1));
const button = document.querySelector('.ais-ClearRefinements-button');
expect(button).toBeDisabled();
expect(button).toHaveClass('ais-ClearRefinements-button--disabled');
expect(container).toMatchInlineSnapshot(`
`);
});
test('clears all refinements', async () => {
const searchClient = createSearchClient({});
const { container, queryAllByRole } = render(
);
await waitFor(() => expect(searchClient.search).toHaveBeenCalledTimes(1));
expect(queryAllByRole('listitem')).toHaveLength(1);
expect(container).toMatchInlineSnapshot(`
`);
userEvent.click(
document.querySelector(
'.ais-ClearRefinements-button'
) as HTMLButtonElement
);
await waitFor(() => expect(queryAllByRole('listitem')).toHaveLength(0));
expect(container).toMatchInlineSnapshot(`
`);
});
test('inclusively restricts what refinements to clear', async () => {
const searchClient = createSearchClient({});
const { container, queryAllByRole } = render(
);
await waitFor(() => expect(searchClient.search).toHaveBeenCalledTimes(1));
expect(queryAllByRole('listitem')).toHaveLength(2);
expect(container).toMatchInlineSnapshot(`
-
brand
:
-
categories
:
`);
userEvent.click(
document.querySelector(
'.ais-ClearRefinements-button'
) as HTMLButtonElement
);
await waitFor(() => expect(queryAllByRole('listitem')).toHaveLength(1));
expect(queryAllByRole('listitem')[0]).toHaveTextContent('brand:Apple');
expect(container).toMatchInlineSnapshot(`
`);
});
test('exclusively restricts what refinements to clear', async () => {
const searchClient = createSearchClient({});
const { container, queryAllByRole } = render(
);
await waitFor(() => expect(searchClient.search).toHaveBeenCalledTimes(1));
expect(queryAllByRole('listitem')).toHaveLength(2);
expect(container).toMatchInlineSnapshot(`
-
brand
:
-
categories
:
`);
userEvent.click(
document.querySelector(
'.ais-ClearRefinements-button'
) as HTMLButtonElement
);
await waitFor(() => expect(queryAllByRole('listitem')).toHaveLength(1));
expect(queryAllByRole('listitem')[0]).toHaveTextContent('categories:Audio');
expect(container).toMatchInlineSnapshot(`
`);
});
test('restricts what refinements to clear with custom logic', async () => {
const searchClient = createSearchClient({});
const { container, queryAllByRole } = render(
items.filter((item) => item !== 'brand')}
/>
);
await waitFor(() => expect(searchClient.search).toHaveBeenCalledTimes(1));
expect(queryAllByRole('listitem')).toHaveLength(2);
expect(container).toMatchInlineSnapshot(`
-
brand
:
-
categories
:
`);
userEvent.click(
document.querySelector(
'.ais-ClearRefinements-button'
) as HTMLButtonElement
);
await waitFor(() => expect(queryAllByRole('listitem')).toHaveLength(1));
expect(queryAllByRole('listitem')[0]).toHaveTextContent('brand:Apple');
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('MyClearsRefinements', 'ROOT');
expect(root).toHaveAttribute('title', 'Some custom title');
});
test('renders with translations', async () => {
const searchClient = createSearchClient({});
const { getByRole } = render(
);
await waitFor(() => expect(searchClient.search).toHaveBeenCalledTimes(1));
expect(getByRole('button', { name: 'Reset' })).toBeInTheDocument();
});
});
function CurrentRefinements(props: UseCurrentRefinementsProps) {
const { items } = useCurrentRefinements(props);
return (
{items.map((item) => (
-
{item.attribute}:
{item.refinements.map(({ label }) => (
))}
))}
);
}
function RefinementList(props: UseRefinementListProps) {
useRefinementList(props);
return null;
}