Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add data-testid attribute to react-select components

Using react-testing-library, I wish to test a form implemented in React.

That form includes a React component of type react-select.

It is necessary to click a part of the react-select component that has no label, no text, etc. (E.g. the dropdown arrow).

Ordinarily, the react-testing-library way to do this is to add a 'data-testid' attribute to the item in question.

I've found that it's possible to give each part of the react-select a CSS class attribute, by providing the 'classNamePrefix' prop to the react-select component. Is there some way to do the same for data-testid attribute?

Note: I'm aware I can provide custom implementations of the components of react-select, but that seems like overkill to get one attribute in place.

like image 220
Hirva Savani Avatar asked Sep 05 '19 20:09

Hirva Savani


People also ask

Where do I put Testid data?

Where do we put data-testid ? To enable test automation support in applications using a React testing library, we need to provide a special data-testid attribute for each component. It must be placed on the containing element of each section and all related subsections.

How do you set data attribute in React?

To set a data attribute on an element in React, set the attribute directly on the element, e.g. <button data-test-id="my-btn"> or use the setAttribute() method, e.g. el. setAttribute('data-foo', 'bar') . You can access the element on the event object or using a ref .

How do you mock select in React?

This is an example of how you could mock a select: jest. mock("react-select", () => ({ options, value, onChange }) => { function handleChange(event) { const option = options. find( option => option.

Is it possible to add a class to the react-testid attribute?

Ordinarily, the react-testing-library way to do this is to add a 'data-testid' attribute to the item in question. I've found that it's possible to give each part of the react-select a CSS class attribute, by providing the 'classNamePrefix' prop to the react-select component. Is there some way to do the same for data-testid attribute?

How do you test React components?

When we test our React components, we should aim to test them in the same way that users would interact with them, and thus checking for the values they would see or actions they would carry out. Users aren’t interested in data attributes or classNames.

What is data-testid used for in react testing?

The React testing library is mainly used for functional black-box testing. A selector might imply a test implementation. Selectors can be ambiguous and change unexpectedly when the implementation changes. However, the use of data-testid shows that it is unambiguous and has been added on purpose to facilitate testing.

Should I use data-testid attributes in my project description?

Using data-testid attributes do not resemble how your software is used and should be avoided if possible. Let’s say a consumer of the Project component passes in the project’s description as its title prop instead. The above test would still pass because the data-testid is present.


1 Answers

First of all I'd question why there's no label on the Select as this wouldn't be classed as accessible for screen readers.

But, If you don't want a visible label you could always pass an aria-label prop to the Select and test it that way using getByLabelText.

<Select aria-label="Example Label" ... />
getByLabelText('Example Label')

If you really need to add a data-testid you could replace the specific components you want to add the data-testid too and add it. (See the docs for more info)

e.g.

// @flow

import React from 'react';
import EmojiIcon from '@atlaskit/icon/glyph/emoji';
import Select, { components } from 'react-select';
import { colourOptions } from './docs/data';

const DropdownIndicator = props => {
  return (
    <components.DropdownIndicator {...props}>
      <span data-testid="DropdownIndicator">
        <EmojiIcon primaryColor={colourOptions[2].color} />
      </span>
    </components.DropdownIndicator>
  );
};

export default () => (
  <Select
    closeMenuOnSelect={false}
    components={{ DropdownIndicator }}
    defaultValue={[colourOptions[4], colourOptions[5]]}
    isMulti
    options={colourOptions}
  />
);

Codesandbox link

like image 99
Lee Avatar answered Sep 27 '22 22:09

Lee