react-testing-library icon indicating copy to clipboard operation
react-testing-library copied to clipboard

fireEvent.change not working on input elements

Open rob-glas opened this issue 6 years ago • 9 comments

  • @testing-library/react version: 10.0.2
  • react version: 16.13.1
  • node version: 13.13.0
  • npm (or yarn) version: 6.14.4

Using typescript 3.8.3 if that is relevant and mocha 7.1.1

Relevant code or config:

I have sliimed down my react component but this is the relevant part:


// component

const MyComponent: FunctionComponent<Props> ({ classes, e.t.c}) => {

    const content: ReactElement = (
        <>
            <input data-testid="test-field" type="text" onChange={(): void => { console.log("called change") }} />
        </>
    );

    return (
        <>
            <Dialog open={context.open || false}>
                 <DialogContent className={classes.content}>
                       {context.loading ? loadingContent : content}
                  </DialogContent>
            </Dialog>
        </>
    );
}


// test

const defaultContext = {
    open: true
};
const { findByTestId } = render(
    <MyComponentContext.Provider={{
         ...defaultContext
    }}>
        <MyComponent {...someDefaultProps} />
    </MyComponentContext.Provider />
); 

const field = await findByTestId("test-field");
fireEvent.change(field, { target: { value: "13" } });

What you did:

Tyring to test a change event on ALL inputs in my project using the latest version of react testing library.

What happened:

Change event is not being triggered.

Solution

It's not ideal but doing exactly this test with ReactTestUtils.Simulate works flawlessly.

rob-glas avatar Apr 18 '20 17:04 rob-glas

Hi @rob-glas.

Please make a real reproduction: https://kcd.im/rtl-help

kentcdodds avatar Apr 18 '20 19:04 kentcdodds

https://codesandbox.io/s/sparkling-star-04bby

Trying to get as close to my setup as possible but does not like mocha.

rob-glas avatar Apr 19 '20 10:04 rob-glas

Ah, because you're using mocha, could you make a reproduction in a github repo that I can pull down instead?

kentcdodds avatar Apr 19 '20 15:04 kentcdodds

I noticed you are using material-ui, I ran into some problems there as well. the solution was to use 'userEvent' instead of fireEvent.

https://github.com/testing-library/user-event

also see: https://github.com/testing-library/react-testing-library/issues/322

Lagily avatar Apr 21 '20 11:04 Lagily

I think the suggestion given by @Lagily is correct. Thanks!

kentcdodds avatar May 10 '20 01:05 kentcdodds

I just had this error and it turned out that my IDE (webstorm) automatically made the import:

import fireEvent from '@testing-library/user-event'

Rather than:

import { fireEvent } from '@testing-library/react'

Posting in case helpful for anyone else!

ajspencer avatar Oct 29 '20 18:10 ajspencer

@Lagily but what's the equivalent for the "change" event in the user-event? I couldn't find any

heyjouh96 avatar Apr 14 '21 19:04 heyjouh96

@Lagily but what's the equivalent for the "change" event in the user-event? I couldn't find any

It would help if you use "type" this way:

userEvent.type(screen.getByRole('textbox'), 'Hello,{enter}World!');
expect(screen.getByRole('textbox')).toHaveValue('Hello,\nWorld!');

https://github.com/testing-library/user-event

atarazavi avatar May 01 '21 22:05 atarazavi

I noticed you are using material-ui, I ran into some problems there as well. the solution was to use 'userEvent' instead of fireEvent.

https://github.com/testing-library/user-event

also see: #322

This is because Material-UI returns a Div as a container for Input elements.

To workaround you can get firstChild of Input element, like this:

const InputEl = screen.getByTestId('inputSearch').firstChild

fireEvent.change(InputEl, {
  target: {
    value: "YOR VALUE HERE"
  }
})

expect(InputEl.value).toBeCalledWith("YOR VALUE HERE")

heroneto avatar Jul 19 '21 20:07 heroneto