Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Select] React Testing Library does not find select event on VaSelect component #671

Closed
ptouchton opened this issue Mar 22, 2022 · 4 comments

Comments

@ptouchton
Copy link

What happened

In a recent ticket we upgraded VAOS to use the new VaSelect web component from the VA component library. We came across an issue/concern in our unit testing of this component using RTL (React testing Library). We are not able to use RTL’s native select feature because the VaSelect web component is rendered within a shadow dom.

Below is a snippet of our solution to trigger the event

    // Choose Provider
    userEvent.click(
      await screen.findByRole('button', { name: /Choose a provider/i }),
    );
    // await waitFor(async () => {
    expect(await screen.findByText(/Displaying 1 to/i)).to.be.ok;
    // });
    const providersSelect = await screen.findByTestId('providersSelect');
    // call VaSelect custom event for onChange handling
    providersSelect.__events.vaSelect({
      detail: { value: FACILITY_SORT_METHODS.distanceFromCurrentLocation },
    });

What I expected to happen

To use RTL's click method on the select component.

This is the line of code that was replaced when we refactored to the new component.

userEvent.click(await screen.findByText(/use your current location/i));

@k80bowman
Copy link
Contributor

We'd like to address this by updating the component to use the native change event.

@andresriveratoro
Copy link
Contributor

andresriveratoro commented Apr 14, 2022

Update on PR:

I have been unable to observe the vaChange event fire in vets-website tests.

This code allows you to select a new value using userEvent and the test passes for this very simple example, but vaChange isn't fired.

const select = await screen.findByTestId('providersSelect');
userEvent.selectOptions(select, 'Two');
expect(screen.getByRole('option', { name: 'Two' }).selected).to.equal(true);

This throws a Error: The given element does not have a value setter

fireEvent.change(select, { target: { value: 'Two' } });

Related Issues

testing-library/dom-testing-library#413
testing-library/dom-testing-library#742

@andresriveratoro
Copy link
Contributor

Moving this to Icebox to look at a later time

@humancompanion-usds humancompanion-usds changed the title React Testing Library does not find select event on VaSelect component [Select] React Testing Library does not find select event on VaSelect component Apr 18, 2022
@caw310 caw310 added the va-select DS select component label Aug 17, 2022
@caw310
Copy link
Contributor

caw310 commented Sep 18, 2024

Closing as this is old and the VADS has been updated. If there is still an issue file a new request.

@caw310 caw310 closed this as completed Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants