import React from 'react';
import { render, screen, waitFor, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import type { Option } from './option';
import { Select } from './';
import { FormField } from '../FormField';
import { GrapesProvider } from '../GrapesProvider';
import { LOCALES } from '../GrapesProvider/exampleLocales';
const options: Option[] = [
{ key: '1', label: 'option 1' },
{ key: '2', label: 'option 2' },
];
describe('Select', () => {
describe('given some option groups', () => {
const options = [
{ key: '1', label: 'option 1' },
{
key: 'group',
label: 'Group',
options: [{ key: '2', label: 'option 2' }],
},
{ key: '3', label: 'option 3' },
];
it('allows user to select an option in a group', async () => {
const handleSelect = vi.fn();
const Wrapper = () => {
return (
);
};
render(
,
);
expect(screen.queryByRole('option')).not.toBeInTheDocument();
await userEvent.click(screen.getByRole('button'));
expect(screen.getByRole('listbox')).toBeVisible();
expect(
await screen.findByRole('option', { name: 'option 1' }),
).toBeVisible();
expect(
await screen.findByRole('option', { name: 'option 2' }),
).toBeVisible();
expect(
await screen.findByRole('option', { name: 'option 3' }),
).toBeVisible();
const optionChosen = options[1]?.options?.[0];
await userEvent.click(
screen.getByRole('option', { name: optionChosen?.label }),
);
await waitFor(() =>
expect(screen.queryByRole('option')).not.toBeInTheDocument(),
);
expect(handleSelect).toHaveBeenCalledWith(optionChosen);
});
});
it('allows user to select an option', async () => {
const Wrapper = () => {
const [selected, setSelected] = React.useState