I'm writing a test case for a feature I'm unit testing. I want to edit a text field that's embedded within several custom components but I can't figure out how to access it:
TestFile.test.tsx
it('should be able to change quantity', () =>{
//Mock out dependent function with jest
//Nothing here right now
//Render with the props you want
render(
<MainComponent /> );
//Locate screen components for test
const counter = screen.queryByTestId("counter");
//Perform user actions
fireEvent.change(counter, 4); <--This is the line where I'm trying to access what I want
//Measure against expect cases
expect(counter).toBe(4);
});
MainComponent.tsx
This is the component I'm trying to access. The problem is, `fireEvent.change` won't work because it's a custom component. At least, I think that's why it doesn't work.<Counter
data-testid='counter' <--data-testid is here right now
containerMargin={{ marginTop: -5 }}
/>
Counter.tsx
Inside `Counter` is another custom component, `TextField`<TextField
{...props}
/>
TextField.tsx
And inside TextField is where the text input I want to access is:<TextInput
{...props}
/>
Error Message
When I try to run the test, I get this error: ● Test › should be able to change quantity
Unable to fire a "change" event - please provide a DOM element.
189 |
190 | //Perform user actions
> 191 | fireEvent.change(counter, 4);
| ^
192 |
193 | //Measure against expect cases
194 | expect(counter).toBe(4);
I can't put the data-testid property inside TextField or the TextInput itself, because there's another Counter in the render of the page I'm testing. Any advice or troubleshooting would be helpful, thanks!