import React from 'react';
import { render, screen } from '@testing-library/react';
import { Input } from '../Input';
import { Select } from '../Select';
import { TextArea } from '../TextArea';
import { FormField } from './';
import { OptionGroup } from '../OptionGroup';
import { GrapesProvider } from '../GrapesProvider/GrapesProvider';
import { LOCALES } from '../GrapesProvider/exampleLocales';
describe('FormField component', () => {
const value = 'abc';
const onChange = vi.fn();
describe('given default props', () => {
it('has a label and an input as child', () => {
render(
,
);
expect(screen.getByRole('textbox', { name: 'A label' })).toBeVisible();
expect(
screen.getByRole('textbox', { name: 'A label' }),
).toHaveAccessibleDescription('An optional description');
});
it('has a label and a select as child', () => {
render(
,
);
expect(screen.getByRole('combobox', { name: 'A label' })).toBeVisible();
expect(
screen.getByRole('combobox', { name: 'A label' }),
).toHaveAccessibleDescription('An optional description');
});
it('has a label and a textarea as child', () => {
render(
,
);
expect(screen.getByRole('textbox', { name: 'A label' })).toBeVisible();
});
it('has a label, a description and a textarea as child', () => {
render(
,
);
expect(screen.getByRole('textbox', { name: 'A label' })).toBeVisible();
expect(
screen.getByRole('textbox', { name: 'A label' }),
).toHaveAccessibleDescription('An optional description');
});
it('has a label and an optiongroup as child', () => {
const options = [
{ value: 'daily', label: 'Daily' },
{ value: 'weekly', label: 'Weekly' },
{ value: 'yearly', label: 'Yearly' },
];
const Wrapper = () => {
const [value, setValue] = React.useState('');
return (
setValue(e.currentTarget.value)}
/>
);
};
render();
expect(screen.getByRole('radiogroup', { name: 'A label' })).toBeVisible();
expect(
screen.getByRole('radiogroup', { name: 'A label' }),
).toHaveAccessibleDescription('An optional description');
});
});
describe('given a `message` props', () => {
it('displays an alert message in priority', () => {
render(
,
);
const input = screen.getByRole('textbox', { name: 'Heating' });
expect(input).toBeVisible();
expect(input).toHaveAccessibleErrorMessage('EVERYTHING IS ON FIRE!');
});
it('invalides the inner input when alertMessage is given', () => {
render(
,
);
const input = screen.getByRole('textbox', { name: 'email' });
expect(input).toBeInvalid();
expect(input).toHaveAccessibleErrorMessage('Email is not valid');
});
it("shows warning when there's no alert", () => {
render(
,
);
const input = screen.getByRole('textbox', { name: 'Heating' });
expect(input).toBeVisible();
expect(input).not.toBeInvalid();
expect(screen.getByRole('status', { name: `It's hot` })).toBeVisible();
});
it('allows input to override the error state', () => {
render(
,
);
const input = screen.getByRole('textbox', { name: 'email' });
expect(input).toBeVisible();
expect(input).not.toBeInvalid();
expect(screen.getByText('Email is not valid')).toBeVisible();
});
});
});