question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

userEvent.type not changing when is a empty string

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
marquesm91commented, Dec 2, 2019

Terrific ideia! Adding erase and shortcut will be possible to work better with userEvent abstractions!

I will start (maybe tonight) with erase to close this issue!

1reaction
weyertcommented, Dec 1, 2019

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 the type helper method.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found