What is react-select-event?
The react-select-event package is a utility for testing React Select components with ease. It provides a set of functions to simulate user interactions with React Select components, making it easier to write tests for applications that use these components.
What are react-select-event's main functionalities?
selectOption
The `selectOption` function allows you to simulate selecting an option from a React Select component. This is useful for testing scenarios where you need to verify that the correct option is selected based on user interaction.
const { select } = require('react-select-event');
const { render, screen } = require('@testing-library/react');
const React = require('react');
const Select = require('react-select');
const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' }
];
render(<Select options={options} />);
await select(screen.getByLabelText('Select...'), 'Chocolate');
clearFirst
The `clearFirst` function allows you to simulate clearing the first selected option in a multi-select React Select component. This is useful for testing scenarios where you need to verify that the correct options are cleared based on user interaction.
const { clearFirst } = require('react-select-event');
const { render, screen } = require('@testing-library/react');
const React = require('react');
const Select = require('react-select');
const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' }
];
render(<Select options={options} isMulti />);
await select(screen.getByLabelText('Select...'), 'Chocolate');
await select(screen.getByLabelText('Select...'), 'Vanilla');
await clearFirst(screen.getByLabelText('Select...'));
createNewOption
The `createNewOption` function allows you to simulate creating a new option in a Creatable React Select component. This is useful for testing scenarios where you need to verify that new options can be added based on user interaction.
const { createNewOption } = require('react-select-event');
const { render, screen } = require('@testing-library/react');
const React = require('react');
const CreatableSelect = require('react-select/creatable');
const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' }
];
render(<CreatableSelect options={options} />);
await createNewOption(screen.getByLabelText('Select...'), 'Mint');
Other packages similar to react-select-event
@testing-library/user-event
@testing-library/user-event is a package that provides utilities to simulate user interactions with DOM elements. It is more general-purpose compared to react-select-event and can be used to test a wide range of user interactions beyond just React Select components.
react-testing-library
react-testing-library is a lightweight solution for testing React components. It encourages testing components in a way that resembles how they are used by end-users. While it does not provide specific utilities for React Select components, it can be used in conjunction with user-event to achieve similar results.
Install
npm install --save-dev react-select-event
Import react-select-event
in your unit tests:
import selectEvent from "react-select-event";
const selectEvent = require("react-select-event");
Supported versions of react-select
This library is tested against all versions of react-select
starting from 2.1.0
.
API
Every helper exported by react-select-event
takes a handle on the react-select
input field as its first argument. For instance, this can be: getByLabelText("Your label name")
.
select(input: HTMLElement, optionOrOptions: string | Array<string>): Promise<void>
Select one or more values in a react-select dropdown.
const { getByTestId, getByLabelText } = render(
<form data-testid="form">
<label htmlFor="food">Food</label>
<Select options={OPTIONS} name="food" inputId="food" isMulti />
</form>
);
expect(getByTestId("form")).toHaveFormValues({ food: "" });
await selectEvent.select(getByLabelText("Food"), ["Strawberry", "Mango"]);
expect(getByTestId("form")).toHaveFormValues({ food: ["strawberry", "mango"] });
await selectEvent.select(getByLabelText("Food"), "Chocolate");
expect(getByTestId("form")).toHaveFormValues({
food: ["strawberry", "mango", "chocolate"]
});
create(input: HTMLElement, option: string): void
Creates and selects a new item. Only applicable to react-select
Creatable
elements.
const { getByTestId, getByLabelText } = render(
<form data-testid="form">
<label htmlFor="food">Food</label>
<Creatable options={OPTIONS} name="food" inputId="food" />
</form>
);
expect(getByTestId("form")).toHaveFormValues({ food: "" });
await selectEvent.create(getByLabelText("Food"), "papaya");
expect(getByTestId("form")).toHaveFormValues({ food: "papaya" });
clearFirst(input: HTMLElement): void
Clears the first value in the dropdown.
const { getByTestId, getByLabelText } = render(
<form data-testid="form">
<label htmlFor="food">Food</label>
<Creatable
defaultValue={OPTIONS[0]}
options={OPTIONS}
name="food"
inputId="food"
isMulti
/>
</form>
);
expect(getByTestId("form")).toHaveFormValues({ food: "chocolate" });
selectEvent.clearFirst(getByLabelText("Food"));
await wait(() => {
expect(getByTestId("form")).toHaveFormValues({ food: "" });
});
clearAll(input: HTMLElement): void
Clears all values in the dropdown.
const { getByTestId, getByLabelText } = render(
<form data-testid="form">
<label htmlFor="food">Food</label>
<Creatable
defaultValue={[OPTIONS[0], OPTIONS[1], OPTIONS[2]]}
options={OPTIONS}
name="food"
inputId="food"
isMulti
/>
</form>
);
expect(getByTestId("form")).toHaveFormValues({
food: ["chocolate", "vanilla", "strawberry"]
});
selectEvent.clearFirst(getByLabelText("Food"));
await wait(() => {
expect(getByTestId("form")).toHaveFormValues({ food: "" });
});
Credits
All the credit goes to Daniel and his StackOverflow answer: https://stackoverflow.com/a/56085734.