Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda