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

React 18: Wrap non-RTL actions in act() #1401

Merged
merged 4 commits into from
Jul 12, 2024

Conversation

jeremywiebe
Copy link
Collaborator

@jeremywiebe jeremywiebe commented Jul 10, 2024

Summary:

This PR is the part of a chain of PRs upgrading Perseus to React 18.

  1. Upgrade testing-library to React 18-compatible version #1400
  2. This PR --> React 18: Wrap non-RTL actions in act() #1401
  3. React 18: Use RTL functions instead of direct HTML DOM APIs #1402
  4. React 18: Custom test fixes for React 18/RTL update #1403

It appears that React 18 and RTL are more sensitive to not having mutating calls wrapped in act(). The changes in this PR are all simple changes that wrap function calls that cause DOM changes in act() calls. There are a few special cases that are failing yet, but this PR cleans up the straightforward ones.

Issue: LEMS-1802

Test plan:

yarn test should have many fewer test failures than PR #1400

Upcoming PRs will address the remaining test failures.

Before

$ yarn test 
...
Test Suites: 20 failed, 1 skipped, 168 passed, 188 of 189 total
Tests:       104 failed, 14 skipped, 2469 passed, 2587 total
Snapshots:   22 failed, 1 file obsolete, 137 passed, 159 total
Time:        25.267 s, estimated 26 s
Ran all test suites.
error Command failed with exit code 1.

After

$ yarn test 
... 
Test Suites: 4 failed, 1 skipped, 184 passed, 188 of 189 total
Tests:       9 failed, 14 skipped, 2564 passed, 2587 total
Snapshots:   4 failed, 1 file obsolete, 155 passed, 159 total
Time:        24.056 s, estimated 25 s
Ran all test suites.
error Command failed with exit code 1.

@jeremywiebe jeremywiebe self-assigned this Jul 10, 2024
Copy link
Contributor

github-actions bot commented Jul 10, 2024

Size Change: 0 B

Total Size: 948 kB

ℹ️ View Unchanged
Filename Size
packages/kas/dist/es/index.js 38.2 kB
packages/kmath/dist/es/index.js 4.26 kB
packages/math-input/dist/es/index.js 127 kB
packages/math-input/dist/es/strings.js 1.73 kB
packages/perseus-core/dist/es/index.js 906 B
packages/perseus-editor/dist/es/index.js 272 kB
packages/perseus-error/dist/es/index.js 866 B
packages/perseus-linter/dist/es/index.js 21.8 kB
packages/perseus/dist/es/index.js 462 kB
packages/perseus/dist/es/strings.js 3.21 kB
packages/pure-markdown/dist/es/index.js 3.67 kB
packages/simple-markdown/dist/es/index.js 12.4 kB

compressed-size-action

jeremywiebe added a commit that referenced this pull request Jul 11, 2024
## Summary:

This PR is the part of a chain of PRs upgrading Perseus to React 18.

  1. This PR --> #1400
  2. #1401
  3. #1402
  4. #1403

I thought I had the React 18 (#1064) upgrade all ready (and it was even approved), but when I returned to that PR, I noticed linter and test failures. This lead me to discover that I need to upgrade testing-library. 

This then breaks a bunch of tests (which I'll fix in future PRs). 

Issue: LEMS-1802

## Test plan:

This PR will introduce many test failures. That's ok, they'll be fixed in follow-up PRs so that hopefully each step is relatively easy to review.

Author: jeremywiebe

Reviewers: jeresig, #perseus, jandrade, somewhatabstract

Required Reviewers:

Approved By: jeresig

Checks: ⏭️  Upload Coverage, ✅ gerald, ⏭️  Publish npm snapshot, ❌ Jest Coverage (ubuntu-latest, 20.x), ❌ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ gerald, ⏭️  Upload Coverage, ⏭️  Publish npm snapshot, ❌ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ❌ Jest Coverage (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ gerald, ⏭️  Upload Coverage, ⏭️  Publish npm snapshot, ❌ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ❌ Jest Coverage (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), ✅ gerald

Pull Request URL: #1400
@jeremywiebe jeremywiebe changed the base branch from jer/react-18-upgrade-rtl to jer/react-18 July 11, 2024 16:09
An error occurred while trying to automatically change base from jer/react-18-upgrade-rtl to jer/react-18 July 11, 2024 16:09
@@ -57,7 +57,7 @@ describe("<TabbarItem />", () => {
onClick={() => {}}
/>,
);
jest.runAllTimers();
act(() => jest.runOnlyPendingTimers());
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

React 18/RTL are much more sensitive to element updates. Most of the time when we call runOnlyPendingTimers() in Perseus tests it's because we're waiting for scheduled async work to complete and then re-render (via setTimeout, setState, etc).

https://legacy.reactjs.org/docs/test-utils.html#act

@@ -0,0 +1,2 @@
// eslint-disable-next-line import/no-unassigned-import
import "jest-extended"; // we love the side effects
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This fixes a Typescript check issue where some of the Jest matchers we use (such as toBeTrue()) were no longer recognized by Typescript.

https://khanacademy.atlassian.net/browse/LEMS-2176

@jeremywiebe jeremywiebe marked this pull request as ready for review July 12, 2024 18:19
@khan-actions-bot khan-actions-bot requested a review from a team July 12, 2024 18:19
@khan-actions-bot
Copy link
Contributor

Gerald

Required Reviewers
  • @Khan/perseus for changes to types/global.d.ts, config/test/log-on-fail-reporter.js, packages/perseus/src/__tests__/article-renderer.test.tsx, packages/perseus/src/__tests__/renderer-api.test.tsx, packages/perseus/src/__tests__/renderer.test.tsx, packages/perseus/src/__tests__/server-item-renderer.test.tsx, packages/perseus-editor/src/__tests__/editor.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/__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__/group.test.tsx, packages/perseus/src/widgets/__tests__/input-number.test.ts, packages/perseus/src/widgets/__tests__/interactive-graph.test.tsx, packages/perseus/src/widgets/__tests__/matcher.test.tsx, 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__/radio.test.ts, packages/perseus-editor/src/widgets/__tests__/expression-editor.test.tsx, packages/math-input/src/components/keypad/__tests__/keypad-button.test.tsx, packages/math-input/src/components/tabbar/__tests__/item.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
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.

Looks good to me - thank you Jeremy!

@jeremywiebe jeremywiebe merged commit fe19cf7 into jer/react-18 Jul 12, 2024
14 of 18 checks passed
@jeremywiebe jeremywiebe deleted the jer/react-18-acting-up branch July 12, 2024 19:22
jeremywiebe added a commit that referenced this pull request Jul 15, 2024
## Summary:

This PR is the part of a chain of PRs upgrading Perseus to React 18.

  1. #1400
  2. #1401
  3. This PR --> #1402
  4. #1403

This PR fixes a few instances where there exists an RTL API for tests instead of making direct DOM object calls. RTL recommends this approach and this fixes a few more tests.

Issue: LEMS-1802

## Test plan:

`yarn test` more tests pass

### Before

```sh
$ yarn test 
... 
Test Suites: 4 failed, 1 skipped, 184 passed, 188 of 189 total
Tests:       9 failed, 14 skipped, 2564 passed, 2587 total
Snapshots:   4 failed, 1 file obsolete, 155 passed, 159 total
Time:        24.056 s, estimated 25 s
Ran all test suites.
error Command failed with exit code 1.
```

### After 

```sh
$ yarn test 
...
Test Suites: 3 failed, 1 skipped, 185 passed, 188 of 189 total
Tests:       6 failed, 14 skipped, 2567 passed, 2587 total
Snapshots:   4 failed, 1 file obsolete, 155 passed, 159 total
Time:        20.517 s, estimated 24 s
Ran all test suites.
error Command failed with exit code 1.
```

Close! :)

Author: jeremywiebe

Reviewers: jeremywiebe, jeresig, #perseus, somewhatabstract

Required Reviewers:

Approved By: jeresig

Checks: ⏭️  Upload Coverage, ✅ gerald, ⏭️  Publish npm snapshot, ❌ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ❌ Jest Coverage (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ gerald, ⏭️  Upload Coverage, ⏭️  Publish npm snapshot, ❌ Lint, Typecheck, Format, and Test (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), ❌ Jest Coverage (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ gerald

Pull Request URL: #1402
jeremywiebe added a commit that referenced this pull request Jul 15, 2024
## Summary:

This PR is the part of a chain of PRs upgrading Perseus to React 18.

  1. #1400
  2. #1401
  3. #1402
  4. This PR --> #1403

A few tests needed larger changes than simply wrapping the actions in `act()` (see #1402). This PR covers the remaining tests that were failing for various reasons. In some cases the tests were largely re-written (sometimes for better test clarity while re-working them to pass). 

In the case of the Zoomable tests, we were waiting for the text to be updated in the element, but the styling changes had not yet settled as the updates caused multiple `setState()` calls to be made which all needed to be called and then resolved by a subsequent render (thus the new `waitForStyle()` helper).

In the Graded Group test update, I'm not sure how it worked as written. In the test that was fixed, we were testing mobile and in that case the "Check" button reads "Try again" on the second and subsequent attempts. 

In the Group tests, I needed to wrap some actions in `act()` as usual, but I also took the opportunity to change the element finding logic away from the `getAll...()` functions to depend more on the aria labels. This makes these tests read more clearly (instead of accessing item at index `[0]` we get the textbox with the string `"value rounded to the nearest ten"`).

Lastly, there was one outdated snapshot due to a test file being renamed.

Issue: LEMS-1802

## Test plan:

`yarn test` all tests now pass!

### Before 

```sh
$ yarn test 
...
Test Suites: 3 failed, 1 skipped, 185 passed, 188 of 189 total
Tests:       6 failed, 14 skipped, 2567 passed, 2587 total
Snapshots:   4 failed, 1 file obsolete, 155 passed, 159 total
Time:        22.175 s, estimated 27 s
Ran all test suites.
```

### After 

```sh
$ yarn test 
...
Test Suites: 1 skipped, 188 passed, 188 of 189 total
Tests:       14 skipped, 2573 passed, 2587 total
Snapshots:   159 passed, 159 total
Time:        26.765 s
Ran all test suites.
```

Author: jeremywiebe

Reviewers: jeremywiebe, jeresig, #perseus, somewhatabstract

Required Reviewers:

Approved By: jeresig

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

Pull Request URL: #1403
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