userEvent in React Testing Library Doesn't Cause onClick To Be Called
Asked Answered
M

4

13

I'm trying to run a really simple test with react-testing-library where a button is given a mock function, the button is clicked, and the test checks that the function was called. However, the test is currently failing because the function isn't being called. Here's the code:

import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

describe('Button', () => {
  test('eventHandler called on click', () => {
    const handleClick = jest.fn();
    render(
      <button onClick={handleClick} type="button">
        Click Me
      </button>
    );

    userEvent.click(screen.getByRole('button'));

    expect(handleClick).toHaveBeenCalledTimes(1);
  });
});

No errors are thrown, the button is successfully found, but for some reason, the function doesn't register that it's been called.

Any help would be appreciated!

Menorah answered 23/9, 2021 at 13:11 Comment(1)
Exact same issue and effective v.14 all api's are async - github.com/testing-library/user-event/pull/790Malvia
C
32

I had the same problem, fireEvent works but the recommended userEvent doesn't trigger click handler. Turned out that userEvent is async. Below is my working test case.

test('Error message should show when all fields are blank', async () => {
  const user = userEvent.setup()
  render(<Signup />)
  
  await user.click(screen.getByRole('button'))

  expect(screen.getByTestId('errors'))
    .toHaveTextContent(dict.fields_must_not_be_empty)
})
Conchaconchie answered 29/4, 2022 at 6:37 Comment(3)
Thank you. I noticed this wasn't a problem when my app was on react scripts 4 and react 17 but became a problem after upgrading.Doldrums
Exact same issue - this works great.Malvia
Effective v.14 all api's are async - github.com/testing-library/user-event/pull/790Malvia
L
3

Worked fine for me Make sure you install this package @testing-library/user-event @testing-library/dom

Steps:

  1. npx create-react-app rtl-typescript --template typescript
  2. npx install --save-dev @testing-library/user-event @testing-library/dom
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

describe('Button', () => {
  test('eventHandler called on click', () => {
    const handleClick = jest.fn();
    render(
      <button onClick={handleClick} type="button">
        Click Me
      </button>
    );

    userEvent.click(screen.getByRole('button'));

    expect(handleClick).toHaveBeenCalledTimes(1);
  });
});

enter image description here

In case needed, my package.json file dependencies

enter image description here

Lulita answered 23/9, 2021 at 20:23 Comment(1)
This is a better answer, is more detailed than the others.Sherlene
E
0

This works

describe('Button', () => {
  test.only('eventHandler called on click', () => {
    const handleClick = jest.fn();
    render(
      <button onClick={handleClick} type="button">
        Click Me
      </button>
    );

    userEvent.click(screen.getByText(/Click me/i));

    expect(handleClick).toHaveBeenCalledTimes(1);
  });
});
Emlyn answered 23/9, 2021 at 20:18 Comment(0)
B
-1

add a role tag to your button

<button 
  onClick={handleClick} 
  role="button" 
  type="button"
>
  Click Me
</button>

or use screen.getByText('Click Me')

Binoculars answered 23/9, 2021 at 13:59 Comment(3)
Doesn't seem to do it. The test is able to find the button successfully but for some reason the userEvent.click doesn't trigger the onClick propMenorah
Did you install userEvent?Binoculars
Yeah, and running other userEvent tests pass (like the one from the docs - testing-library.com/docs/…)Menorah

© 2022 - 2024 — McMap. All rights reserved.