import { render, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';
import { createSearchClient } from '../../../../../test/mock';
import { InstantSearchHooksTestWrapper } from '../../../../../test/utils';
import { HitsPerPage } from '../HitsPerPage';
describe('HitsPerPage', () => {
test('renders with props', async () => {
const searchClient = createSearchClient({});
const { container } = render(
);
await waitFor(() => expect(searchClient.search).toHaveBeenCalledTimes(1));
expect(container).toMatchInlineSnapshot(`
`);
});
test('selects current value', async () => {
const searchClient = createSearchClient({});
const { getByRole } = render(
);
await waitFor(() => expect(searchClient.search).toHaveBeenCalledTimes(1));
expect(
(getByRole('option', { name: '10' }) as HTMLOptionElement).selected
).toBe(false);
expect(
(getByRole('option', { name: '20' }) as HTMLOptionElement).selected
).toBe(true);
expect(
(getByRole('option', { name: '30' }) as HTMLOptionElement).selected
).toBe(false);
});
test('refines on select', async () => {
const searchClient = createSearchClient({});
const { getByRole } = render(
);
await waitFor(() => expect(searchClient.search).toHaveBeenCalledTimes(1));
userEvent.selectOptions(getByRole('combobox'), ['30']);
expect(searchClient.search).toHaveBeenCalledTimes(2);
expect(searchClient.search).toHaveBeenLastCalledWith(
expect.arrayContaining([
expect.objectContaining({
params: expect.objectContaining({
hitsPerPage: 30,
}),
}),
])
);
});
test('forwards custom class names and `div` props to the root element', () => {
const { container } = render(
);
const root = container.firstChild;
expect(root).toHaveClass('MyHitsPerPage', 'ROOT');
expect(root).toHaveAttribute('title', 'Some custom title');
});
});