fireEvent.change not working on input elements
@testing-library/reactversion: 10.0.2reactversion: 16.13.1nodeversion: 13.13.0npm(oryarn) 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.
Hi @rob-glas.
Please make a real reproduction: https://kcd.im/rtl-help
https://codesandbox.io/s/sparkling-star-04bby
Trying to get as close to my setup as possible but does not like mocha.
Ah, because you're using mocha, could you make a reproduction in a github repo that I can pull down instead?
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
I think the suggestion given by @Lagily is correct. Thanks!
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!
@Lagily but what's the equivalent for the "change" event in the user-event? I couldn't find any
@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
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")