Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
Set a dropdown's value in React Testing Library

For testing an input it works like this:

  it('test input', () => {
    const { getByTestId, getByLabelText } = render(<MyComponent />);

    const myButton = getByTestId('submit-button');
    expect(myButton).toBeInTheDocument();

    fireEvent.change(getByLabelText('first-name'), {
      target: { value: 'ted' }
    });

    // do something
  });

the above test is setting the value of first-name input to "ted" and moves forward.

I want to do something similar but for a drop-down selector.

This is the component:

<SelectInput
  label='my-dropdown'
  onChange={onChange}
  options={myOptions}
/>;

myOptions is an array of this shape:

const myOptions = [
  { id: '0', name: 'zero' },
  { id: '1', name: 'one' },
  { id: '2', name: 'two' }
];

it works fine in the application, no errors from this part.

Here comes the testing of it, I did something but it doesn't work:

  it('test dropdpwn', () => {
    const { getByTestId, getByLabelText } = render(<MyComponent />);

    const saveButton = getByTestId('submit-button');
    expect(saveButton).toBeInTheDocument();

    fireEvent.change(getByLabelText('my-dropdown'), {
      target: { value: { id: '0', name: 'zero' } }
    });

    // do something
  });

the above code doesn't work, it doesn't set the dropdown with that value.

Any ideas on how to solve this? Not sure if it's important, but all those inputs are inside a react-hook-form and at the end it should test that the onSubmit is working (it works only if all inputs are set).

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

So, since the select behavior is being achieved using a button and spans.

You need to first click the button this would bring all the options on the screen and then you need to click one of those options.

And then you can finally test that the selected option is now on the screen.

it("test dropdpwn", async () => {
  const { getByTestId, getByLabelText } = renderWithClientInstance(
    <MapSignalModal title={title} open={true} toggle={toggle} />
  );

  userEvent.click(screen.getAllByTestId("selectButton")[0]);
  userEvent.click(screen.getByText("sensor pool 1"));

  expect(
    await screen.findByText(screen.getByText("sensor pool 1"))
  ).toBeInTheDocument();
});

Also, to be really sure you can try the following, this should fail because "sensor pool 1" option is not initially on the screen.

And it should pass when the text is changed to "sensor pool 0" because that's there on the screen initially.

it("test dropdpwn", async () => {
  const { getByTestId, getByLabelText } = renderWithClientInstance(
    <MapSignalModal title={title} open={true} toggle={toggle} />
  );

  expect(screen.getByText("sensor pool 1")).toBeInTheDocument();
  // if you replace the above text to "sensor pool 0", it should work
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!