score:1

Accepted answer

try

test("select", async () => {
  render(<app />);

  // get and click in the dropdown
  const dropdownbutton = screen.getbyrole("button", { name: /dog name ​/i });
  fireevent.click(dropdownbutton);

  // wait for dropdown item and click it
  const dropdownitem = await screen.findbytestid("name-item-2");
  fireevent.click(dropdownitem);
});

there are some ms in between the click and the apearance on the screen of the element, so you have declare your function as async and to await for your element to appear (and use findby instead of getby in order to work as async).

you can read more about testing-library async methods here

score:1

first of all, for component interaction, prefer the use of userevent instead fireevent as testing-library describes:

the built-in fireevent is a utility to easily dispatch events. it dispatches exactly the events you tell it to and just those - even if those exact events never had been dispatched in a real interaction in a browser.

user-event on the other hand dispatches the events like they would happen if a user interacted with the document. that might lead to the same events you previously dispatched per fireevent directly, but it also might catch bugs that make it impossible for a user to trigger said events. this is why you should use user-event to test interaction with your components.

you can check more about it here.

related to test not find option role, it´s because the element take some milliseconds to appear on screen and because of that your test should be async and you need to use await and findby query to succeed in the test:

import { render, screen, cleanup } from "@testing-library/react";
import userevent from "@testing-library/user-event";

test("select", async () => {
  render(<app />);

  const dropdownbutton = screen.getbyrole("button");

  userevent.click(dropdownbutton);

  const dropdownitem = await screen.findbyrole("option", { name: /ninica/i });

  userevent.click(dropdownitem);

  const typographyel = await screen.findbytext(/chosen name: ninica/i);

  expect(typographyel).tobeinthedocument();
});

you can check more about async/await and findby query here.

you can also check the difference of queries here.


Related Query

More Query from same tag