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

Upgrade @testing-library/user-event to v14 #1029

Merged
merged 15 commits into from
Feb 27, 2024
Merged

Conversation

jeremywiebe
Copy link
Collaborator

@jeremywiebe jeremywiebe commented Feb 27, 2024

Summary:

This PR upgrades the user-event library to v14. This contains a similar set of changes to what is contained in Khan/webapp#20109. It prepares the way for Khan Academy to move to React v18.

In short:

  • All calls to the userEvent library are now treated as async (prefixed with await)
  • Most usages of the library now need to be setup before we can use it. This is because the new library does not know intrinsically how to advance Jest's fake timers. As a result, we have to tell it how because most of our Perseus tests use fake timers. This is a bit verbose but overall pretty unintrusive based on the pattern I figured out (open to feedback and improvements!!).

Issue: LC-1753

Test plan:

yarn test 👍
yarn lint 👍

@jeremywiebe jeremywiebe self-assigned this Feb 27, 2024
Copy link
Contributor

github-actions bot commented Feb 27, 2024

Size Change: -16 B (0%)

Total Size: 819 kB

Filename Size Change
packages/perseus-editor/dist/es/index.js 266 kB +11 B (0%)
packages/perseus/dist/es/index.js 391 kB -27 B (0%)
ℹ️ View Unchanged
Filename Size
packages/kas/dist/es/index.js 38.1 kB
packages/kmath/dist/es/index.js 4.27 kB
packages/math-input/dist/es/index.js 80 kB
packages/perseus-core/dist/es/index.js 908 B
packages/perseus-error/dist/es/index.js 878 B
packages/perseus-linter/dist/es/index.js 21.8 kB
packages/pure-markdown/dist/es/index.js 3.68 kB
packages/simple-markdown/dist/es/index.js 12.4 kB

compressed-size-action

Copy link

codecov bot commented Feb 27, 2024

Codecov Report

Attention: Patch coverage is 78.94737% with 4 lines in your changes are missing coverage. Please review.

Project coverage is 66.12%. Comparing base (5ec2bb7) to head (979b477).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1029      +/-   ##
==========================================
+ Coverage   64.40%   66.12%   +1.72%     
==========================================
  Files         427      429       +2     
  Lines       97246    97292      +46     
  Branches     6459    10034    +3575     
==========================================
+ Hits        62628    64334    +1706     
+ Misses      34618    32958    -1660     

Impacted file tree graph

Files Coverage Δ
...s/perseus-editor/src/components/graph-settings.tsx 97.81% <100.00%> (+0.01%) ⬆️
packages/perseus/src/widgets/categorizer.tsx 93.31% <73.33%> (-0.88%) ⬇️

... and 109 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5ec2bb7...979b477. Read the comment docs.

@@ -9,6 +9,8 @@ import {StyleSheetTestUtils} from "aphrodite";
import jestSerializerHtml from "jest-serializer-html";
import {addSerializer} from "jest-specific-snapshot";

import "@testing-library/jest-dom";
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We had sprinkled this in most test files. This makes starting a new test file just that little bit easier!

@@ -50,10 +50,9 @@
"@storybook/react-vite": "^7.6.17",
"@swc-node/register": "^1.6.6",
"@swc/core": "^1.3.68",
"@testing-library/dom": "^8.11.0",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

jest-dom already depends on this library and we never use it directly. So this avoids version mismatches (which I did hit while working on this PR) when we bump testing library versions.

import {
screen,
render,
fireEvent,
within,
waitFor,
} from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import {userEvent as userEventLib} from "@testing-library/user-event";
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

By aliasing the library import, we can avoid renaming all usages of the userEvent throughout these tests. See the beforeEach() below.

@@ -91,7 +97,7 @@ describe("math input integration", () => {
).toBeInTheDocument();
});

it("doesn't show the keypad initially", () => {
it("doesn't show the keypad initially", async () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I used some of the automation John figured out for migrating webapp and so there might be a few tests in this PR that are migrated to be async that don't actually call any async functions. In practice, this didn't seem to bother. I'm happy to roll back these if the reviewer has strong feelings. :)

@@ -260,13 +263,13 @@ describe("renderer", () => {
});

// Assert
expect(sawDropdown2).toBeTrue();
expect(sawDropdown2).toBe(true);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I started getting type errors on .toBeTrue() and .toBeFalse(). They appear to be from Jasmine and I'm unclear how this ever worked. 😦

Comment on lines +1445 to +1448
const textboxes = screen.getAllByRole("textbox");
for (let i = 0; i < textboxes.length; i++) {
await userEvent.type(textboxes[i], i.toString());
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I just could not get .forEach() to work with these async calls.

@@ -23,11 +22,29 @@ const mockSize = (
}
};

// The zoomable does some measuring after the initial render to determine what
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As I dug into these tests for the Zoomable, I discovered that they weren't really validating things correctly. I've fixed them up so that I think they are now correct and compatible with user-event@14

Comment on lines +262 to +264
question: PerseusRenderer,
correct: string,
incorrect: string,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Just added names to the tuple items. Makes it a bit clearer.

@jeremywiebe jeremywiebe marked this pull request as ready for review February 27, 2024 03:35
@khan-actions-bot
Copy link
Contributor

khan-actions-bot commented Feb 27, 2024

Gerald

Required Reviewers
  • @Khan/perseus for changes to package.json, yarn.lock, .changeset/afraid-moons-swim.md, config/test/custom-matchers.ts, config/test/test-setup.ts, packages/perseus/src/__tests__/article-renderer.test.tsx, packages/perseus/src/__tests__/error-boundary.test.tsx, packages/perseus/src/__tests__/renderer.test.tsx, packages/perseus/src/__tests__/server-item-renderer.test.tsx, packages/perseus/src/__tests__/widget-container.test.tsx, packages/perseus/src/widgets/categorizer.tsx, packages/perseus/src/widgets/label-image.test.ts, packages/perseus/src/widgets/video-transcript-link.test.tsx, packages/perseus-editor/src/__tests__/editor.test.tsx, packages/perseus-editor/src/__tests__/hint-editor.test.tsx, packages/perseus-editor/src/__tests__/item-extras-editor.test.tsx, packages/perseus-editor/src/components/graph-settings.tsx, packages/math-input/src/components/__tests__/integration.test.tsx, packages/perseus/src/components/__tests__/math-input.test.tsx, packages/perseus/src/components/__tests__/sortable.test.tsx, packages/perseus/src/components/__tests__/sorter.test.tsx, packages/perseus/src/components/__tests__/zoomable.test.tsx, packages/perseus/src/multi-items/__tests__/multi-renderer.test.tsx, packages/perseus/src/widgets/__testdata__/numeric-input.testdata.ts, packages/perseus/src/widgets/__tests__/categorizer.test.ts, packages/perseus/src/widgets/__tests__/cs-program.test.ts, packages/perseus/src/widgets/__tests__/definition.test.ts, packages/perseus/src/widgets/__tests__/dropdown.test.ts, packages/perseus/src/widgets/__tests__/explanation.test.ts, packages/perseus/src/widgets/__tests__/expression-mobile.test.tsx, packages/perseus/src/widgets/__tests__/expression.test.tsx, packages/perseus/src/widgets/__tests__/graded-group-set.test.ts, packages/perseus/src/widgets/__tests__/graded-group.test.ts, packages/perseus/src/widgets/__tests__/grapher.test.ts, packages/perseus/src/widgets/__tests__/group.test.tsx, packages/perseus/src/widgets/__tests__/iframe.test.ts, packages/perseus/src/widgets/__tests__/input-number.test.ts, packages/perseus/src/widgets/__tests__/matcher.test.tsx, packages/perseus/src/widgets/__tests__/matrix.test.ts, packages/perseus/src/widgets/__tests__/number-line.test.ts, packages/perseus/src/widgets/__tests__/numeric-input.test.ts, packages/perseus/src/widgets/__tests__/orderer.test.ts, packages/perseus/src/widgets/__tests__/passage-ref.test.ts, packages/perseus/src/widgets/__tests__/passage.test.tsx, packages/perseus/src/widgets/__tests__/plotter.test.tsx, packages/perseus/src/widgets/__tests__/python-program.test.ts, packages/perseus/src/widgets/__tests__/radio.test.ts, packages/perseus/src/widgets/__tests__/unit.test.tsx, packages/perseus/src/widgets/__tests__/video.test.ts, packages/perseus-editor/src/components/__tests__/blur-input.test.tsx, packages/perseus-editor/src/components/__tests__/graph-settings.test.tsx, packages/perseus-editor/src/components/__tests__/interactive-graph-settings.test.tsx, packages/perseus-editor/src/widgets/__tests__/categorizer-editor.test.tsx, packages/perseus-editor/src/widgets/__tests__/definition-editor.test.tsx, packages/perseus-editor/src/widgets/__tests__/dropdown-editor.test.tsx, packages/perseus-editor/src/widgets/__tests__/explanation-editor.test.tsx, packages/perseus-editor/src/widgets/__tests__/expression-editor.test.tsx, packages/perseus-editor/src/widgets/__tests__/input-number-editor.test.tsx, packages/perseus-editor/src/widgets/__tests__/matcher-editor.test.tsx, packages/perseus-editor/src/widgets/__tests__/number-line-editor.test.tsx, packages/perseus-editor/src/widgets/__tests__/numeric-input-editor.test.tsx, packages/perseus-editor/src/widgets/__tests__/python-program-editor.test.tsx, packages/perseus-editor/src/widgets/__tests__/radio-editor.test.tsx, packages/perseus-editor/src/widgets/__tests__/sorter-editor.test.tsx, packages/math-input/src/components/keypad/__tests__/keypad-button.test.tsx, packages/math-input/src/components/keypad/__tests__/keypad-v2-mathquill.test.tsx, packages/math-input/src/components/keypad/__tests__/keypad.test.tsx, packages/math-input/src/components/keypad/__tests__/mobile-keypad.test.tsx, packages/math-input/src/components/tabbar/__tests__/tabbar.test.tsx, packages/perseus/src/widgets/__tests__/radio/base-radio.test.tsx, packages/perseus/src/widgets/__tests__/radio/choice-icon.test.tsx, packages/perseus/src/widgets/__tests__/radio/choice.test.tsx, packages/perseus/src/widgets/__tests__/radio/focus-ring.test.tsx, packages/perseus/src/widgets/__tests__/radio/option-status.test.tsx, packages/perseus/src/widgets/deprecated-standin/__tests__/deprecated-standin.test.ts, packages/perseus/src/widgets/label-image/__tests__/answer-pill.test.tsx, packages/perseus/src/widgets/label-image/__tests__/hide-answers-toggle.test.tsx

Don't want to be involved in this pull request? Comment #removeme and we won't notify you of further changes.

Copy link
Contributor

github-actions bot commented Feb 27, 2024

npm Snapshot: Published

Good news!! We've packaged up the latest commit from this PR (979b477) and published it to npm. You
can install it using the tag PR1029.

Example:

yarn add @khanacademy/perseus@PR1029

If you are working in Khan Academy's webapp, you can run:

./dev/tools/bump_perseus_version.sh -t PR1029

Copy link
Member

@jeresig jeresig left a comment

Choose a reason for hiding this comment

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

The general pattern you've used here for user-event makes sense to me. An alternative for the beforeEach would be to change the render() call to have a custom function that renders and returns a new userEvent object correctly-bound to the jest fake timers - but that's possibly more work? I'm defer to others on the review of the specifics of the tests. Thanks for the user-event upgrade!

Base automatically changed from jer/storybook-vite to main February 27, 2024 18:59
@jeremywiebe jeremywiebe changed the base branch from main to radio-widget-flow-tweaks February 27, 2024 19:34
@jeremywiebe jeremywiebe changed the base branch from radio-widget-flow-tweaks to main February 27, 2024 19:34
@jeremywiebe jeremywiebe merged commit 17d05e8 into main Feb 27, 2024
13 checks passed
@jeremywiebe jeremywiebe deleted the jer/user-event-14 branch February 27, 2024 23:02
jeremywiebe pushed a commit that referenced this pull request Feb 27, 2024
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated.


# Releases
## @khanacademy/perseus-editor@4.3.0

### Minor Changes

-   [#1025](#1025) [`5ec2bb71`](5ec2bb7) Thanks [@nishasy](https://github.com/nishasy)! - Create InteractiveGraphSettings to be used in InteractiveGraphEditor in place of GraphSettings

### Patch Changes

-   [#1029](#1029) [`17d05e8e`](17d05e8) Thanks [@jeremywiebe](https://github.com/jeremywiebe)! - Migrate to @testing-library/user-event v14.


-   [#1022](#1022) [`b2649c32`](b2649c3) Thanks [@nishasy](https://github.com/nishasy)! - Correct type for `valid` prop from `boolean` to `boolean | string`


-   [#1024](#1024) [`0c9fe476`](0c9fe47) Thanks [@nishasy](https://github.com/nishasy)! - Add labels to input fields in GraphSettings + tests.

-   Updated dependencies \[[`17d05e8e`](17d05e8), [`7e4a65f0`](7e4a65f)]:
    -   @khanacademy/math-input@17.2.1
    -   @khanacademy/perseus@19.6.2

## @khanacademy/perseus-dev-ui@1.1.5

### Patch Changes

-   [#1029](#1029) [`17d05e8e`](17d05e8) Thanks [@jeremywiebe](https://github.com/jeremywiebe)! - Migrate to @testing-library/user-event v14.


-   [#1012](#1012) [`7e4a65f0`](7e4a65f) Thanks [@jeremywiebe](https://github.com/jeremywiebe)! - Change Vite config so it can be re-used by Storybook

-   Updated dependencies \[[`17d05e8e`](17d05e8), [`7e4a65f0`](7e4a65f)]:
    -   @khanacademy/math-input@17.2.1

## @khanacademy/math-input@17.2.1

### Patch Changes

-   [#1029](#1029) [`17d05e8e`](17d05e8) Thanks [@jeremywiebe](https://github.com/jeremywiebe)! - Migrate to @testing-library/user-event v14.


-   [#1012](#1012) [`7e4a65f0`](7e4a65f) Thanks [@jeremywiebe](https://github.com/jeremywiebe)! - Fix handling of "Fractions" keypad in the keypad tab bar icon component

## @khanacademy/perseus@19.6.2

### Patch Changes

-   [#1029](#1029) [`17d05e8e`](17d05e8) Thanks [@jeremywiebe](https://github.com/jeremywiebe)! - Migrate to @testing-library/user-event v14.

-   Updated dependencies \[[`17d05e8e`](17d05e8), [`7e4a65f0`](7e4a65f)]:
    -   @khanacademy/math-input@17.2.1

Author: khan-actions-bot

Reviewers: jeremywiebe

Required Reviewers:

Approved By: jeremywiebe

Checks: ✅ codecov/project, ✅ codecov/patch, ✅ Upload Coverage, ⏭  Publish npm snapshot, ✅ Extract i18n strings (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ Jest Coverage (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ gerald, ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x)

Pull Request URL: #1030
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants