Skip to content

Commit c6a5cbe

Browse files
authored
React 18 (#1064)
## Summary: ~I've got the changes ready, but we need to wait on WB and webapp to be ready before I land this PR and do a release.~ **We're now ready!** This PR moves this repo to use React 18. There were a few changes that needed to be made to be fully compatible with React 18. * Migrate a few tests from the old `ReactDOM.TestUtils` renderer to @testing-library. * Update some snapshots as the snapshot renderer seems to have slightly different whitespace settings. * Fixes to the `ErrorBoundary` tests as React's `componentDidCatch` provides a different stack trace format (which is irrelevat to our code). * Migrate direct uses of `ReactDOM.render()` to `ReactDOMClient.createRoot()` (we hope to move these to use Portals in the future, but I couldn't get them working). * Move all `devDependencies` in the root `package.json` to be `dependencies` (`import/no-extraneous-dependencies` was complaining about `@types/react` being in `devDependencies`). Since the root package.json is just a host for the workspace and doesn't build any production code, the distinction between `dependencies` and `devDependencies` is irrelevant. * Force Storybook to use `react-docgen` instead of the default `react-docgen-typescript` (`react-docgen` is the default in Storybook 8 so we can revert this once we upgrade). See [this](storybookjs/storybook#24665 (comment)) comment that pointed to the fix. Issue: LEMS-1802 ## Test plan: `yarn test` ✅ `yarn tsc` ✅ `yarn storybook` ✅ - I sampled many of the stories. I also checked the stories that were affected by the move from `ReactDOM.render()` to `ReactDOMClient.createRoot()` Author: jeremywiebe Reviewers: jeresig, jeremywiebe, #perseus, handeyeco Required Reviewers: Approved By: jeresig Checks: ✅ Upload Coverage (ubuntu-latest, 20.x), ✅ Publish npm snapshot (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), ✅ 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: #1064
1 parent 4b6fc71 commit c6a5cbe

File tree

71 files changed

+1031
-825
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1031
-825
lines changed

.changeset/mean-roses-invite.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@khanacademy/math-input": patch
3+
"@khanacademy/perseus": patch
4+
---
5+
6+
Fix: prevent `react` and `react-dom` from being bundled

.changeset/nervous-ads-check.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@khanacademy/perseus-dev-ui": major
3+
"@khanacademy/math-input": major
4+
"@khanacademy/perseus": major
5+
"@khanacademy/perseus-editor": major
6+
"@khanacademy/simple-markdown": minor
7+
---
8+
9+
React 18
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@khanacademy/simple-markdown": minor
3+
---
4+
5+
React 18

.storybook/main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ const config: StorybookConfig = {
6666
});
6767
},
6868

69+
typescript: {
70+
reactDocgen: "react-docgen",
71+
},
6972
docs: {
7073
autodocs: true,
7174
},

config/test/log-on-fail-reporter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable import/no-commonjs */
22
/**
3-
* Only log console statements on when the test errors out.
3+
* Only log console statements when the test errors out.
44
* From: https://gist.github.com/GeeWee/71db0d9911b4a087e4b2486386168b05
55
*/
66
const {getConsoleOutput} = require("@jest/console");

dev/index.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import {RenderStateRoot} from "@khanacademy/wonder-blocks-core";
33
import * as React from "react";
44
import {useEffect, useReducer} from "react";
5-
import {render} from "react-dom";
5+
import {createRoot} from "react-dom/client";
66

77
import {setDependencies} from "../packages/perseus/src/dependencies";
88
import {storybookTestDependencies} from "../testing/test-dependencies";
@@ -12,11 +12,15 @@ import {Gallery} from "./gallery";
1212

1313
setDependencies(storybookTestDependencies);
1414

15-
render(
15+
const rootEl = document.getElementById("app-root");
16+
if (rootEl === null) {
17+
throw new Error("No root element found");
18+
}
19+
const root = createRoot(rootEl);
20+
root.render(
1621
<RenderStateRoot>
1722
<Router />
1823
</RenderStateRoot>,
19-
document.getElementById("app-root"),
2024
);
2125

2226
function Router() {

package.json

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"engines": {
1414
"node": ">=20"
1515
},
16-
"devDependencies": {
16+
"dependencies": {
1717
"@babel/core": "^7.23.2",
1818
"@babel/eslint-parser": "^7.22.15",
1919
"@babel/eslint-plugin": "^7.22.10",
@@ -50,15 +50,16 @@
5050
"@storybook/react-vite": "^7.6.17",
5151
"@swc-node/register": "^1.6.6",
5252
"@swc/core": "^1.3.68",
53-
"@testing-library/jest-dom": "^5.15.0",
54-
"@testing-library/react": "^12.1.2",
53+
"@testing-library/dom": "^10.3.1",
54+
"@testing-library/jest-dom": "^6.4.6",
55+
"@testing-library/react": "^16.0.0",
5556
"@testing-library/user-event": "^14.5.2",
5657
"@types/classnames": "^2.3.1",
5758
"@types/jest": "29",
5859
"@types/jquery": "^3.5.16",
5960
"@types/node": "^18.14.1",
60-
"@types/react": "16",
61-
"@types/react-dom": "16",
61+
"@types/react": "18",
62+
"@types/react-dom": "18",
6263
"@types/underscore": "^1.11.4",
6364
"@typescript-eslint/eslint-plugin": "^7.3.1",
6465
"@typescript-eslint/parser": "^7.3.1",
@@ -90,7 +91,7 @@
9091
"eslint-plugin-react-hooks": "^4.6.0",
9192
"eslint-plugin-react-native": "^4.1.0",
9293
"eslint-plugin-storybook": "^0.8.0",
93-
"eslint-plugin-testing-library": "^6.2.0",
94+
"eslint-plugin-testing-library": "^6.2.2",
9495
"fast-glob": "^3.2.11",
9596
"jest": "^29.7.0",
9697
"jest-environment-jsdom": "^29.7.0",
@@ -100,8 +101,8 @@
100101
"less-loader": "^11.1.3",
101102
"nyc": "^15.1.0",
102103
"prettier": "^3",
103-
"react": "16.14.0",
104-
"react-dom": "16.14.0",
104+
"react": "18",
105+
"react-dom": "18",
105106
"react-json-view": "^1.19.1",
106107
"react-popper": "^2.2.5",
107108
"react-router": "5.2.1",
@@ -117,17 +118,13 @@
117118
"sloc": "^0.2.1",
118119
"storybook": "^7.6.17",
119120
"style-loader": "^3.3.3",
121+
"tiny-invariant": "^1.3.1",
120122
"typescript": "^5.4.2",
121123
"typescript-coverage-report": "^0.7.0",
122124
"vite-plugin-istanbul": "^5.0.0",
123125
"vite-plugin-turbosnap": "^1.0.3",
124126
"winston": "^3.7.2"
125127
},
126-
"resolutions": {
127-
"@types/jest": "29",
128-
"@types/react": "16",
129-
"@types/react-dom": "16"
130-
},
131128
"scripts": {
132129
"gen:parsers": "yarn --cwd packages/kas gen:parsers",
133130
"prebuild": "yarn gen:parsers",
@@ -151,8 +148,5 @@
151148
"cypress:ci": "cross-env BABEL_COVERAGE=1 cypress run --component --env CYPRESS_COVERAGE=1",
152149
"format": "prettier --write .",
153150
"typecheck": "tsc"
154-
},
155-
"dependencies": {
156-
"tiny-invariant": "^1.3.1"
157151
}
158152
}

packages/math-input/package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@
5454
"katex": "0.11.1",
5555
"perseus-build-settings": "^0.4.1",
5656
"prop-types": "15.6.1",
57-
"react": "16.14.0",
58-
"react-dom": "^16.8.0",
59-
"react-router": "5.2.1",
60-
"react-router-dom": "5.3.0",
57+
"react": ">= 18",
58+
"react-dom": ">= 18",
59+
"react-router": "^5.2.1",
60+
"react-router-dom": "^5.3.0",
6161
"react-transition-group": "^4.4.1"
6262
},
6363
"peerDependencies": {
@@ -73,10 +73,10 @@
7373
"jquery": "^2.1.1",
7474
"katex": "0.11.1",
7575
"prop-types": "15.6.1",
76-
"react": "16.14.0",
77-
"react-dom": "^16.8.0",
78-
"react-router": "5.2.1",
79-
"react-router-dom": "5.3.0",
76+
"react": ">= 18",
77+
"react-dom": ">= 18",
78+
"react-router": "^5.2.1",
79+
"react-router-dom": "^5.3.0",
8080
"react-transition-group": "^4.4.1"
8181
},
8282
"keywords": []

packages/math-input/src/components/keypad/__tests__/keypad-button.test.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {render, screen} from "@testing-library/react";
1+
import {act, render, screen} from "@testing-library/react";
22
import {userEvent as userEventLib} from "@testing-library/user-event";
33
import * as React from "react";
44

@@ -72,7 +72,9 @@ describe("<KeypadButton />", () => {
7272
);
7373

7474
// Act
75-
screen.getByRole("button", {name: "Right parenthesis"}).focus();
75+
act(() =>
76+
screen.getByRole("button", {name: "Right parenthesis"}).focus(),
77+
);
7678
await userEvent.keyboard("{enter}");
7779

7880
// Assert

packages/math-input/src/components/keypad/__tests__/keypad-v2-mathquill.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Popover} from "@khanacademy/wonder-blocks-popover";
1+
import {Popover, PopoverContentCore} from "@khanacademy/wonder-blocks-popover";
22
import {color} from "@khanacademy/wonder-blocks-tokens";
33
import {render, screen} from "@testing-library/react";
44
import {userEvent as userEventLib} from "@testing-library/user-event";
@@ -69,7 +69,7 @@ function V2KeypadWithMathquill(props: Props) {
6969
<div style={{maxWidth: "400px", margin: "2em"}}>
7070
<Popover
7171
content={
72-
<div>
72+
<PopoverContentCore>
7373
<Keypad
7474
extraKeys={["a", "b", "c"]}
7575
onClickKey={handleClickKey}
@@ -87,7 +87,7 @@ function V2KeypadWithMathquill(props: Props) {
8787
}
8888
showDismiss
8989
/>
90-
</div>
90+
</PopoverContentCore>
9191
}
9292
dismissEnabled
9393
opened={keypadOpen}

0 commit comments

Comments
 (0)