Skip to content

Input "selectionStart" is not used in userEvent.type(input, "abc") #236

Closed
@ivarprudnikov

Description

@ivarprudnikov

As mentioned in title changing selectionStart has no effect when using userEvent.type(input, "foobar"), eg:

test("selectionStart is not used when updating input", async () => {
  const { getByTestId } = render(<App />);
  await waitForElement(() => getByTestId("testinput"));

  const input = getByTestId("testinput");
  expect(input).not.toBeNull();
  expect(input.value).toBe("xyz");

  // move cursor to the beginning of input
  input.focus();
  input.setSelectionRange(0, 0);
  expect(input.selectionStart).toBe(0);
  expect(input.selectionEnd).toBe(0);

  // start typing
  await userEvent.type(input, "abc");

  expect(input.value).toBe("abcxyz"); // <-- FAILS
  expect(input.selectionStart).toBe(3);
});

Important: in the test case I use react-scripts test --env=jsdom-sixteen as jsdom had fixed selectionStart only recently jsdom/jsdom#2787

Test case on codesandbox:
Edit Input selectionStart test

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions