hello. @bonnie ! I'm working on popover testing in section 4 right now.
However, userEvent.click(someElement) is not executed when it is executed like the code in the lecture.
test('Initial conditions', () => {
render(<SummaryForm />);
const checkbox = screen.getByRole('checkbox', {
name: /terms and conditions/i,
});
//find Checkbox
expect(checkbox).not.toBeChecked();
const confirmButton = screen.getByRole('button', { name: /confirm order/i });
expect(confirmButton).toBeDisabled();
});
test('Checkbox disables button on first click and enables on second click', () => {
render(<SummaryForm />);
const checkbox = screen.getByRole('checkbox', {
name: /terms and conditions/i,
});
const confirmButton = screen.getByRole('button', { name: /confirm order/i });
userEvent.click(checkbox);
expect(confirmButton).toBeEnabled();
userEvent.click(checkbox);
expect(confirmButton).toBeDisabled();
});
After digging a little further into the documentation, I found that with asynchronous test execution, I had to do userEvent.click with userEvent.setup() before rendering the component.
Writing tests with userEvent
We recommend invoking [userEvent.setup()](https://testing-library.com/docs/user-event/setup)
before the component is rendered. This can be done in the test itself, or by using a setup function. We discourage rendering or using any userEvent
functions outside of the test itself - e.g. in a before
/after
hook - for reasons described in "Avoid Nesting When You're Testing"
import { render, screen } from '@testing-library/react';
import SummaryForm from '../SummaryForm';
import userEvent from '@testing-library/user-event';
test('Initial conditions', () => {
render(<SummaryForm />);
const checkbox = screen.getByRole('checkbox', {
name: /terms and conditions/i,
});
//find Checkbox
expect(checkbox).not.toBeChecked();
const confirmButton = screen.getByRole('button', { name: /confirm order/i });
expect(confirmButton).toBeDisabled();
});
test('Checkbox disables button on first click and enables on second click', async () => {
const user = userEvent.setup();
render(<SummaryForm />);
const checkbox = screen.getByRole('checkbox', {
name: /terms and conditions/i,
});
const confirmButton = screen.getByRole('button', { name: /confirm order/i });
await user.click(checkbox);
expect(confirmButton).toBeEnabled();
await user.click(checkbox);
expect(confirmButton).toBeDisabled();
});
I'm wondering if the lecture needs an update or if I'm doing something wrong.