3

I am trying to add a stable data-testid to a react-select component (specifically AsyncSelect) so that I can reliably select it in my Playwright E2E tests.

When I try to pass the data-testid prop directly to the <AsyncSelect> component, the prop is not rendered on the component's root <div> in the DOM.

My current workaround is to wrap the entire component in a new and apply the data-testid to that wrapper.

 <div data-testid="my-selector-wrapper">
   <AsyncSelect>....</>
  </div>

This works, but it adds an extra, seemingly unnecessary DOM element just for testing.

My question is:

  1. Is wrapping react-select in a div the standard, accepted pattern for adding a test ID?
  2. Is there a built-in react-select prop or a different, cleaner method (perhaps using the components prop or classNamePrefix) that allows me to apply a data-testid to the component's root element without this wrapper?
2
  • 1
    This is a good example of why not to use test ids in the first place, which add tons of unnecessary DOM attributes just for testing, and avoid Playwright's suggested best practice of using user-visible locators like getByRole. That said, I'm assuming you're stuck with test ids--in that case, if the library throws out props you add to it and the docs don't provide an alternative, then you're out of luck, and yes, you likely need to add an element just for testing. Commented Nov 13 at 20:52
  • 1
    Perhaps you can find some inspiration in the tests in the react-select repo? github.com/JedWatson/react-select/tree/master/packages/… Commented Nov 13 at 21:20

1 Answer 1

3

Is wrapping react-select in a div the standard, accepted pattern for adding a test ID?

I'm inclined to say no. If you need access to any data-X attributes this should just generally already be available to you or passed through (automagically) to any underlying components. However, generally speaking for lack thereof this is a viable workaround.

Is there a built-in react-select prop or a different, cleaner method (perhaps using the components prop or classNamePrefix) that allows me to apply a data-testid to the component's root element without this wrapper?

There is the ability to pass additional input element properties using the Select component's components prop and a custom input component.

For more details see https://react-select.com/components.

Example:

import AsyncSelect from "react-select/async";
import { components } from "react-select";

...

<AsyncSelect
  options={options}
  components={{
    Input: (props) => (
      <components.Input {...props} data-testid="my-selector-wrapper" />
    ),
  }}
/>

example DOM output

That said, using data-testid for testing the DOM is generally not advised as you should really be accessing your UI/DOM the same way you users do, i.e. by the visible text and semantic/accessible labels/roles/etc. data-testid should be a last resort after all other methods of accessing/interacting with the DOM have failed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.