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

chore(deps): update testing library user events to v14 #1419

Merged

Conversation

Francois-Esquire
Copy link
Contributor

@Francois-Esquire Francois-Esquire commented Oct 7, 2022

Description

This PR updates @testing-library/user-events to v14.

Details

Some notable differences in the migration.

  • With any usage of userEvent in a test file, a setup function call was made const user = userEvents.setup(), where user was used in place of userEvent
  • User events now run async
  • For tests using fake timers, userEvent needed to be setup with userEvents.setup({ delay: null })
  • Some tests were not responding as expected and the use of fireEvent did take their place (datepickers and dropdowns).

Checklist

  • 👌 design updates will be Garden Designer approved (add the designer as a reviewer)
  • 🌐 demo is up-to-date (yarn start)
  • ⬅️ renders as expected with reversed (RTL) direction
  • 🤘 renders as expected with Bedrock CSS (?bedrock)
  • 💂‍♂️ includes new unit tests
  • tested for WCAG 2.1 AA compliance
  • 📝 tested in Chrome, Firefox, Safari, and Edge

await user.type(combobox, '{arrowup}');
await user.type(combobox, '{enter}');
await user.type(input, '{arrowup}');
await user.type(input, '{enter}');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test case has been questionable. I was only able to get it passing when targeting the input in the Combobox and the only test that has been "altered". When I tested manually, this behavior didn't behave as expected - nothing happened. Is there anything historical to note with this test case?

Copy link
Member

Choose a reason for hiding this comment

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

🤔 I wonder if the combobox is somehow no longer considered to be the recipient of the keystrokes. I don't see a problem with the updated code.

for (const _ of new Array(5).fill(null)) {
// eslint-disable-next-line no-await-in-loop
await user.click(getByRole('button'));
}
Copy link
Contributor Author

@Francois-Esquire Francois-Esquire Oct 10, 2022

Choose a reason for hiding this comment

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

The for of loop was favorable here to keep the async calls that was originally updated like so:

await Promise.all(new Array(5).fill(null).map(() => user.click(getByRole('button'))));

as it was throwing a warning on what appeared to be stacking act calls being done internally by the user.click API.
This was a for loop before the need to be async.

Copy link
Member

@jzempel jzempel Oct 11, 2022

Choose a reason for hiding this comment

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

Can this be refactored as an internal async function as shown by https://eslint.org/docs/latest/rules/no-await-in-loop?

Copy link
Contributor Author

@Francois-Esquire Francois-Esquire Oct 19, 2022

Choose a reason for hiding this comment

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

Having a classic for loop as it's shown above is equivalent to:

await Promise.all(new Array(5).fill(null).map(() => user.click(getByRole('button'))));

Since this mixes a sync invocation to an async operation, which makes all the async operations run simultaneously (which is undesired). This doesn't guarantee sequence nor waiting for atomic completion per invocation before the next call is started synchronously - this caused warnings to show up. An alternative to for of loop that would wait per call is to:

await user.click(getByRole('button'));
await user.click(getByRole('button'));
await user.click(getByRole('button'));
await user.click(getByRole('button'));
await user.click(getByRole('button'));

which is verbose, but effective.

Comment on lines +64 to 68
// eslint-disable-next-line @typescript-eslint/no-unused-vars
for (const _ of new Array(10).fill(null)) {
// eslint-disable-next-line no-await-in-loop
await user.click(getByRole('button'));
}
Copy link
Member

Choose a reason for hiding this comment

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

Should this be refactored as shown by https://eslint.org/docs/latest/rules/no-await-in-loop?

Copy link
Contributor Author

@Francois-Esquire Francois-Esquire Oct 19, 2022

Choose a reason for hiding this comment

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

It's not the most elegant way of doing it (adding the disable rule comments does not help) but the for of loop in this use case, allows us to handle async calls in a step by step fashion. I mentioned more here: #1419 (comment)

for (const _ of new Array(5).fill(null)) {
// eslint-disable-next-line no-await-in-loop
await user.click(getByRole('button'));
}
Copy link
Member

@jzempel jzempel Oct 11, 2022

Choose a reason for hiding this comment

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

Can this be refactored as an internal async function as shown by https://eslint.org/docs/latest/rules/no-await-in-loop?

Copy link
Member

@jzempel jzempel left a comment

Choose a reason for hiding this comment

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

Copy link
Member

@jzempel jzempel left a comment

Choose a reason for hiding this comment

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

@Francois-Esquire Francois-Esquire force-pushed the francois-esquire/update-testing-library-user-events branch from 4819337 to 953a723 Compare October 20, 2022 18:28
@coveralls
Copy link

Coverage Status

Coverage remained the same at 96.152% when pulling 953a723 on francois-esquire/update-testing-library-user-events into 6daac42 on main.

@Francois-Esquire
Copy link
Contributor Author

@Francois-Esquire Francois-Esquire merged commit f6cf7d3 into main Oct 21, 2022
@Francois-Esquire Francois-Esquire deleted the francois-esquire/update-testing-library-user-events branch October 21, 2022 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

4 participants