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

DSD-1748: Adding interaction tests for RadioGroup #1597

Merged
merged 5 commits into from
Jun 10, 2024

Conversation

EdwinGuzman
Copy link
Member

Fixes JIRA ticket DSD-1748

This PR does the following:

  • Adds interaction tests for the RadioGroup component. It checks for both clicking on radio inputs and using up/down and left/right keys presses to select new values.
  • As with other interaction tests, this is viewed in the "Controls" tab in the addon toolbar.

How has this been tested?

Storybook

Accessibility concerns or updates

  • N/A

Checklist:

  • I have updated the Storybook documentation accordingly.
  • I have added relevant accessibility documentation for this pull request.
  • All new and existing tests passed.

Front End Review:

  • Review the Vercel preview deployment once it is ready.

@EdwinGuzman EdwinGuzman added the Needs Review Pull requests that are ready for peer review. label May 13, 2024
Copy link

vercel bot commented May 13, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nypl-design-system ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 10, 2024 2:49pm

@bigfishdesign13 bigfishdesign13 added the x2 This PR needs at least two approvals. label May 16, 2024
Copy link
Collaborator

@bigfishdesign13 bigfishdesign13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Questions.

Comment on lines 86 to 92
await userEvent.click(screen.getByLabelText("Radio 3"));
await userEvent.click(screen.getByLabelText("Radio 2"));
await userEvent.click(screen.getByLabelText("Radio 5"));
await userEvent.keyboard("{arrowdown}");
await userEvent.keyboard("{arrowleft}");
await userEvent.keyboard("{arrowup}");
await userEvent.keyboard("{arrowright}");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code executes events, but shouldn't there also be tests that verify the outcome of the events?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the same. I was a bit confused but the Storybook doc examples don't always have expectations https://storybook.js.org/docs/writing-tests/interaction-testing

The actions and interactions are the tests. When I did expectations, such as expect(screen.getByRole("radiogroup")).toBeInTheDocument(); above, they don't show up in the addon panel. So we can add more expectations but what is shown to users are the interactions.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if you add an expectation and it fails?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error will be shown. It's not very intuitive but it's an implicit success, so you're not told anything until something throws an error.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think this will ever replace the unit tests?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ask because I feel an expectation should be added whenever an userEvent is executed. We do that with the unit tests. Should we also do that here as well. We can add all the click events that we want, but if nothing changes after the event and we don't explicitly test to verify that it has changed, does the fact that we executed a click event mean anything?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding interactions and expectations is required for unit tests since it's testing the component in a fundamental and technical manner. In these tests, it's enough that the interactions work since that's what users do on sites and it's also how Storybook write the example tests as well.

There's also this idea of implicit assertions (it's on the site from the creator of React Testing Library). He posted this recently https://x.com/kentcdodds/status/1765501275003580800 and it's very interesting because it's not explicitly asserting anything, just that the page loads and that's enough.

That's something I'd like to also be considered here where if the keyboard and mouse interactions work, then we don't really need explicit expectations.

Regardless though, I'd like to keep how we think about unit tests and these interaction tests as separate types of tests.

CC @oliviawongnyc @7emansell @jackiequach

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@EdwinGuzman Looking back at the article about implicit assertions, none of the examples talk about testing user events. They are all related to values and formats, but not to an event that may cause something to happen or change. With the interective tests, we are triggering events, but can there always be an implicit assertion that something on screen or in the code has changed simply because an event was triggered?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I'd say yes and no and generally depends on the component we are testing and what we are testing for. If we want to test tabbing and moving arrows around, we visually see that new values get selected so it's working. It doesn't hurt to add expectations though.

I can add some expectations but because of how the addons display the tests, it won't appear in the list of interactions.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add some expectations but because of how the addons display the tests, it won't appear in the list of interactions.

I think that is ok. They won't verify success, but they will throw errors if there is a problem.

@EdwinGuzman
Copy link
Member Author

Ready for another review @7emansell @jackiequach @bigfishdesign13

await userEvent.keyboard("{arrowup}");
await userEvent.keyboard("{arrowright}");
await userEvent.keyboard("{arrowdown}");
expect(screen.getByLabelText("Radio 2")).toBeChecked();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Odd, I now see this in the UI.
Screenshot 2024-06-05 at 3 04 28 PM

Copy link
Collaborator

@bigfishdesign13 bigfishdesign13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonderful.

@bigfishdesign13 bigfishdesign13 added Ship It Pull requests that have been reviewed and approved. and removed Needs Review Pull requests that are ready for peer review. x2 This PR needs at least two approvals. labels Jun 6, 2024
@EdwinGuzman EdwinGuzman merged commit ea3f515 into development Jun 10, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ship It Pull requests that have been reviewed and approved.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants