userEvent.type not changing when is a empty string
See original GitHub issueI am experimenting this lib and may reached a possible bug. I have been writting a test for Input
to check if erases it correctly and found something interesting.
import React from 'react';
import { render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
function Input(props) {
return <input {...props} />;
}
it('should handle input erases', async () => {
let value = '';
const onChange = jest.fn(e => {
value = e.target.value;
});
const { getByPlaceholderText, rerender } = render(
<Input placeholder="Name" value={value} onChange={onChange} />
);
const input = getByPlaceholderText('Name');
userEvent.type(input, 'test');
// In order to see some "value" changes, we need to rerender the component
rerender(<Input placeholder="Name" value={value} onChange={onChange} />);
expect(input).toHaveAttribute('value', 'test');
// When type empty string simulating an erase test will fail
userEvent.type(input, '');
// When using { allAtOnce: true } test will pass
// userEvent.type(input, '', { allAtOnce: true });
// In order to see some "value" changes, we need to rerender the component
rerender(<Input placeholder="Name" value={value} onChange={onChange} />);
expect(input).toHaveAttribute('value', '');
});
When you use userEvent.type
without { allAtOnce: true }
on this case, the Input
value will not get any updates. Looking at the type
implementation I could identify what is possible wrong.
if (opts.allAtOnce) {
if (element.readOnly) return;
fireEvent.input(element, { target: { value: text } });
} else {
let actuallyTyped = '';
// If text is a empty string, actuallyTyped will never be used inside fireEvent.input
for (let index = 0; index < text.length; index++) {
...
}
}
If this is a real bug, I can help fix this! Maybe a naive solution could be adding a extra condition
if (opts.allAtOnce || text === '')
What do you think guys?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:6 (1 by maintainers)
Top Results From Across the Web
userEvent.type not updating value of input in test
I am testing this component, which is simply changing its state depending on new input into the input field and setting ...
Read more >user-event v13 - Testing Library
Simulates the keyboard events described by text . This is similar to userEvent.type() but without any clicking or changing the selection range.
Read more >Simulate Typing into Form Fields in Tests with the User Event ...
Now, type userevent.clearinput. We're going to clear out whatever is in there. We're going to clear out the input field. Now that we...
Read more >userEvent.type not updating value of input in test-Reactjs
[Solved]-userEvent.type not updating value of input in test-Reactjs · score:0. Your attribute "type" is invalid. The value "name" doesn't exist, try "text".
Read more >user-event - Bountysource
userEvent.type does not dispatch change events on input with ... userEvent.type with empty string causes a warning about an unhandled promise rejection $...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Terrific ideia! Adding
erase
andshortcut
will be possible to work better with userEvent abstractions!I will start (maybe tonight) with
erase
to close this issue!Sounds like a brilliant idea! I think this makes totally sense and feels to match the approach of this library (in my opinion). Typing and Erasing seems like a good combination.
I was thinking to write a helper
userEvent.shortcut("{ctrl}{alt}m")
to allow to type shortcuts easier. Not sure if it can be part of thetype
helper method.