This is an onclick handler test example from the storybook testing library repository:
test('onclick handler is called', async () => {
const onClickSpy = jest.fn();
render(<Secondary onClick={onClickSpy} />);
const buttonElement = screen.getByRole('button');
buttonElement.click();
expect(onClickSpy).toHaveBeenCalled();
});
https://github.com/storybookjs/testing-react/blob/main/example/src/components/Button.test.tsx
But userEvent import is also available in storybook testing library, should it be used instead?
test('onclick handler is called', async () => {
const onClickSpy = jest.fn();
render(<Secondary onClick={onClickSpy} />);
const buttonElement = screen.getByRole('button');
await userEvent.click(buttonElement)
expect(onClickSpy).toHaveBeenCalled();
});
Or better to stick with the first example approach?, which would be the best practice?