so I had written a sample test case for an input, where if I press enter it clears the input field. It works till this point, but when I try to type after that, somehow the previously typed text comes back as part of the input value, and it's driving me crazy.
I have tested this visually, and it works perfectly fine visually (same props and all), so it does not make sense for the test to behave this way. I'm using jest, react-testing-library and user events. Attaching a minimal reproducible test with comments to explain what the test says.
it('reproducible issue', async () => {
const { findByLabelText } = renderWithChakraProvider(
<TagInput label="tag-input" id="tag-input" name="tag-input" />
);
const inputElement = await findByLabelText('tag-input');
await userEvents.type(inputElement, 'test 1');
expect(inputElement).toHaveValue('test 1'); // passing
await userEvents.type(inputElement, '{enter}'); // this presses the enter/return key on the input field
expect(inputElement).toHaveValue(''); // passing, this should mean input has been cleared
await userEvents.type(inputElement, 'test 2');
expect(inputElement).toHaveValue('test 2'); // FAILING, it expects `test 1test 2`, HOW is `test 1` value coming back after having been apparently cleared????
});
I know I can probably just run clear()
after asserting that the value is empty, but any idea why it is behaving like this?