Skip to content

fix(userEvent): don't select option if select is disabled #641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 15, 2020

Conversation

nickserv
Copy link
Member

@nickserv nickserv commented Jun 14, 2020

Migrated from testing-library/user-event#344 by @ben-dyer

What: Prevent a select's value from changing with selectOptions if it is disabled

Why: It's possible to change the value of a disabled select using selectOptions. For instance this is passing:

  const {
    container: {firstChild: select},
  } = render(
    <select disabled>
      <option>No value selected</option>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>,
  )

  expect(select.selectedIndex).toBe(0)

  userEvent.selectOptions(select, '1')
  expect(select.selectedIndex).toBe(1)

How: Exit early in selectOptions if the element is disabled, similar to what is done in clear and upload

Checklist:

  • Documentation added to the
    docs site N/A
  • Tests
  • Typescript definitions updated N/A
  • Ignore disabled options
  • Ready to be merged

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jun 14, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 2708676:

Sandbox Source
hungry-night-s6dpi Configuration

@nickserv nickserv force-pushed the pr/user-event-disabled-select-options branch from b5b374a to 3155890 Compare June 14, 2020 05:14
@nickserv nickserv changed the title fix(selectOptions): don't select option if select is disabled fix(userEvent): don't select option if select is disabled Jun 14, 2020
Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, just one thing.

@nickserv nickserv marked this pull request as ready for review June 15, 2020 08:31
Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solid. Thank you!

@kentcdodds kentcdodds merged commit 480fe24 into pr/user-event Jun 15, 2020
@nickserv nickserv deleted the pr/user-event-disabled-select-options branch June 15, 2020 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants