#Best practice for testing click using storybook testing library

1 messages · Page 1 of 1 (latest)

tidal mantle
#

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?

#

Best practice for esting click using storybook testing library