# Share jest test for SelectV2

**URL:** <https://community.dynatrace.com/t/share-jest-test-for-selectv2/48083>\
**Category:** Developer Q&A Forum\
**Tags:** dynatrace-app-toolkit, strato, custom-app, coding-assistance\
**Created:** [October 2, 2024, 8:56am UTC](https://community.dynatrace.com/t/share-jest-test-for-selectv2/48083 "2024-10-02T08:56:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DaveOps](https://avatars.discourse-cdn.com/v4/letter/d/fbc32d/32.png) [@DaveOps](https://community.dynatrace.com/u/DaveOps)\
**Post date:** [October 2, 2024, 8:56am UTC](https://community.dynatrace.com/t/share-jest-test-for-selectv2/48083/1 "2024-10-02T08:56:54Z")

</div>

Can you share a example jest test for a SelectV2 component?

For some reason I am not able to make it work due to rendering of selectv2-menu options after a click event.

---

<div class="post-metadata">

**Author:** ![educampver](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/educampver/32/29917_2.png) [@educampver](https://community.dynatrace.com/u/educampver)\
**Post date:** [October 4, 2024, 9:07am UTC](https://community.dynatrace.com/t/share-jest-test-for-selectv2/48083/2 "2024-10-04T09:07:45Z")

</div>

Hi&nbsp;[@DaveOps](https://community.dynatrace.com/user/78577), would you please share your failing tests and the errors why they fail? It would greatly help us figure out the issue.

Best, Edu.

---

<div class="post-metadata">

**Author:** ![DaveOps](https://avatars.discourse-cdn.com/v4/letter/d/fbc32d/32.png) [@DaveOps](https://community.dynatrace.com/u/DaveOps)\
**Post date:** [October 4, 2024, 1:12pm UTC](https://community.dynatrace.com/t/share-jest-test-for-selectv2/48083/3 "2024-10-04T13:12:22Z")

</div>

Sharing the code:

```javascript
test("opens the dropdown with options showing", async () => {
    render(
      <AppRoot>
        <GenericDropdown
          id="gd"
          icon={Icon}
          options={options}
          value="30s"
          onChange={mockOnChange}
          ariaLabel="Select an option"
        />
      </AppRoot>,
      { baseElement: document.body }
    );

    const user = userEvent.setup();
    const combobox = screen.getByRole('combobox');
    expect(combobox).toBeInTheDocument();

    await user.click(combobox);

    const listbox = await screen.findByRole('listbox');
    const option1 = await within(listbox).findByRole('option', { name: '5 seconds' });
    const option2 = await within(listbox).findByRole('option', { name: '15 seconds' });
    const option3 = await within(listbox).findByRole('option', { name: '30 seconds' });
    const option4 = await within(listbox).findByRole('option', { name: '1 minute' });
    const option5 = await within(listbox).findByRole('option', { name: '5 minutes' });

    expect(option1).toBeInTheDocument();
    expect(option2).toBeInTheDocument();
    expect(option3).toBeInTheDocument();
    expect(option4).toBeInTheDocument();
    expect(option5).toBeInTheDocument();
  });

```

After a click event on the input with role combobox, the options are not rendered and it can’t find option1.

---

<div class="post-metadata">

**Author:** ![educampver](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/educampver/32/29917_2.png) [@educampver](https://community.dynatrace.com/u/educampver)\
**Post date:** [October 4, 2024, 1:38pm UTC](https://community.dynatrace.com/t/share-jest-test-for-selectv2/48083/4 "2024-10-04T13:38:21Z")

</div>

I see, using the combobox could be the issue, try using&nbsp;`getByTestId('selectv2-trigger')`&nbsp;instead.

Cheers, Edu.

---

<div class="post-metadata">

**Author:** ![DaveOps](https://avatars.discourse-cdn.com/v4/letter/d/fbc32d/32.png) [@DaveOps](https://community.dynatrace.com/u/DaveOps)\
**Post date:** [October 4, 2024, 2:05pm UTC](https://community.dynatrace.com/t/share-jest-test-for-selectv2/48083/5 "2024-10-04T14:05:44Z")

</div>

I made the code change that you suggested which resulted into this error:

```javascript
FAIL tests/components/GenericDropdown.test.tsx (7.413 s)
  GenericDropdown Component
    ✓ renders with the default value (47 ms)
    ✕ opens the dropdown with options showing (1088 ms)
    ○ skipped changes the value when a different option is selected

  ● GenericDropdown Component › opens the dropdown with options showing

    Unable to find role="option" and name "5 seconds"

    Ignored nodes: comments, script, style
    <div
      aria-multiselectable="false"
      class="Select_selectMenuListWrapperCSS__1x49mee9 strato-selectV2-menulist"
      id="react-select-3-listbox"
      role="listbox"
    >
      <div
        class="Select_selectVirtualizationWrapperCSS__1x49mee8"
        style="height: 160px; display: grid; grid-template-rows: 32px 32px 32px 32px 32px;"
      />
    </div>

      65 | const listbox = await screen.findByRole('listbox');
      66 |
    > 67 | const option1 = await within(listbox).findByRole('option', { name: '5 seconds' });
         | ^
      68 | const option2 = await within(listbox).findByRole('option', { name: '15 seconds' });
      69 | const option3 = await within(listbox).findByRole('option', { name: '30 seconds' });
      70 | const option4 = await within(listbox).findByRole('option', { name: '1 minute' });

      at waitForWrapper (../node_modules/@testing-library/dom/dist/wait-for.js:163:27)
      at ../node_modules/@testing-library/dom/dist/query-helpers.js:86:33
      at Object.<anonymous> (components/GenericDropdown.test.tsx:67:43)

```

---

<div class="post-metadata">

**Author:** ![nina](https://avatars.discourse-cdn.com/v4/letter/n/e68b1a/32.png) [@nina](https://community.dynatrace.com/u/nina)\
**Post date:** [October 7, 2024, 8:16am UTC](https://community.dynatrace.com/t/share-jest-test-for-selectv2/48083/6 "2024-10-07T08:16:55Z")

</div>

Hi, [@DaveOps](https://community.dynatrace.com/user/78577)! A virtualization mock is needed for testing as the SelectV2 automatically virtualizes the options. Did you add setupVirtualizationMock() from the [@dynatrace](https://community.dynatrace.com/user/64071)/strato-components-preview/testing package to the test?  
Your tests could look something like this:

```auto
describe('selectv2 testing', () => {
  beforeEach(() => {
    setupVirtualizationMock();
  });
  afterEach(()=> {
    clearVirtualizationMock();
  });
  test(...);
});

```

Alternatively, you should also be able to call the setup() function to set up all the mocks necessary for testing the Strato components.

---

<div class="post-metadata">

**Author:** ![DaveOps](https://avatars.discourse-cdn.com/v4/letter/d/fbc32d/32.png) [@DaveOps](https://community.dynatrace.com/u/DaveOps)\
**Post date:** [October 7, 2024, 3:04pm UTC](https://community.dynatrace.com/t/share-jest-test-for-selectv2/48083/7 "2024-10-07T15:04:01Z")

</div>

Thanks Nina, now it works.
