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 gets only last letter on v12.0.9

See original GitHub issue
  • @testing-library/user-event version: 12.0.9
  • Testing Framework and version: jest@26.0.1
  • DOM Environment: jsdom@16.2.2, jest-environment-jsdom@26.1.0
  • node@12.14.0

Relevant code or config

name-edit.js

import React from 'react';

export const NameEdit = () => {
  const [userName, setUserName] = React.useState('');

  return (
    <input value={userName} onChange={(e) => setUserName(e.target.value)} />
  );
};

name-edit.spec.js

import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { NameEdit } from './name-edit';

describe('NameEdit component specs', () => {
  it('should update value when input changes', () => {
    // Arrange

    // Act
    render(<NameEdit />);

    const inputElement = screen.getByRole('textbox', {
      name: '',
    });

    userEvent.type(inputElement, 'John');

    // Assert
    expect(inputElement.value).toEqual('John');
  });
});

What happened:

Since I update to v12.0.9 this spec fails because it gets only last letter.

image

It’s working on v12.0.8

Reproduction repository:

Sandbox with v12.0.9: https://codesandbox.io/s/quizzical-surf-wpkq1 Sandbox with v12.0.8: https://codesandbox.io/s/eager-lamport-2hwhb

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:11
  • Comments:33 (14 by maintainers)

github_iconTop GitHub Comments

13reactions
mavv-codercommented, Nov 23, 2020

I am still suffering this error while testing next component:

import React from 'react';
import { useField } from 'formik';
import MuiTextArea, { TextFieldProps } from '@material-ui/core/TextField';

export const TextAreaComponent: React.FunctionComponent<TextFieldProps> = props => {
  const [field, meta] = useField(props.name);
  const textAreaProps = Boolean(field) ? field : props;
  const hasError = Boolean(meta && meta.touched && meta.error);

  return (
    <MuiTextArea
      {...props}
      name={textAreaProps.name}
      onChange={textAreaProps.onChange}
      onBlur={textAreaProps.onBlur}
      value={textAreaProps.value ?? ''}
      multiline={true}
      type={'TextareaAutosize'}
      variant={'standard'}
      error={hasError}
      helperText={hasError ? meta.error : ''}
    />
  );
};

Using this test:

import React from 'react';
import { Formik, Form } from 'formik';
import { render, screen, act } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { TextAreaComponent } from './textArea-field.component';

describe('textfield component specs', () => {
  const renderWithFormik = (component, initialValues) => ({
    ...render(
      <Formik initialValues={initialValues} onSubmit={console.log}>
        {() => <Form>{component}</Form>}
      </Formik>
    ),
  });

 it('"input" should change value when typing', () => {
    // Arrange

    // Act
    renderWithFormik(<TextAreaComponent name={name} />, { name: 'test name' });

    const textarea = screen.getByRole('textbox') as HTMLInputElement;

    userEvent.type(textarea, 'test input text');
    
    // Assert
    expect(textarea.value).toEqual('test input text');
  });
});

The test doesn’t pass because expect receives ‘est input textt’ instead of ‘test input text’. If I use act function to wrap userEvent expect only receives the last letter.

What’s going on here? Does anyone know the solution to this?


10reactions
wangxx1412commented, Nov 24, 2020

Hi guys, I have a similar issue with userEvent.type, is only typed the first letter of the string. Does anyone have idea about this weird behavior?

 const emailInput = getByTestId(container, "login-form-email");

 userEvent.type(emailInput,`EmailThatIsCorrect@Email.com`)

 expect(emailInput).toHaveValue('EmailThatIsCorrect@Email.com' );

Terminal:

Expected the element to have value:
  EmailThatIsCorrect@Email.com
Received:
  E
Read more comments on GitHub >

github_iconTop Results From Across the Web

userEvent.type inputs only last character when wrapped in act
I found an issues in the user-event github and it says its resolved in version 12.0.1 but I'm on version 12.1.5 and I...
Read more >
user-event v13 - Testing Library
user-event is a companion library for Testing Library that provides more. ... You should use userEvent.type if you just want to conveniently ...
Read more >
Relay
Relay generates Flow or TypeScript types for each of your React components that use Relay, which represent the data that each component receives, ......
Read more >
Operations Guide: TPMC-V12 & TPMC-V15 - Crestron
NOTE: Clear History only takes effect after restarting Internet Explorer. NOTE: To use the on-screen keyboard for security settings, touch Keyboard on the...
Read more >
Open Source Used In slido-test 3.0 - Cisco
1.14.1 Available under license. 1.15 tokenizers 0.9.2. 1.15.1 Available under license. 1.16 types-webpack-env 1.16.3. 1.16.1 Available under license.
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