Skip to content

Commit b834e5c

Browse files
committed
tests[react-devtools]: added tests for Compiler integration
1 parent 6cf8518 commit b834e5c

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

.github/workflows/devtools_regression_tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ jobs:
103103
- "16.8" # hooks
104104
- "17.0"
105105
- "18.0"
106+
- "18.2" # compiler polyfill
106107
continue-on-error: true
107108
steps:
108109
- uses: actions/checkout@v4
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import {getVersionedRenderImplementation} from './utils';
11+
12+
describe('CompilerIntegration', () => {
13+
global.IS_REACT_ACT_ENVIRONMENT = true;
14+
let React;
15+
let act;
16+
let useMemoCache;
17+
18+
beforeEach(() => {
19+
React = require('react');
20+
require('react-dom');
21+
require('react-dom/client');
22+
useMemoCache = require('react/compiler-runtime').c;
23+
24+
const utils = require('./utils');
25+
act = utils.act;
26+
});
27+
28+
afterEach(() => {
29+
jest.restoreAllMocks();
30+
});
31+
32+
const {render} = getVersionedRenderImplementation();
33+
34+
// @reactVersion >= 18.2
35+
it('By default, component display names should not have Forget prefix', () => {
36+
const hook = global.__REACT_DEVTOOLS_GLOBAL_HOOK__;
37+
const reactDOMFiberRendererInterface = hook.rendererInterfaces.get(1);
38+
expect(reactDOMFiberRendererInterface).not.toBeFalsy();
39+
40+
const Foo = () => {
41+
// eslint-disable-next-line no-unused-vars
42+
const [val, setVal] = React.useState(null);
43+
44+
return (
45+
<div>
46+
<Bar />
47+
</div>
48+
);
49+
};
50+
const Bar = () => <div>Hi!</div>;
51+
52+
act(() => render(<Foo />));
53+
54+
expect(
55+
reactDOMFiberRendererInterface
56+
.getDisplayNameForElementID(2)
57+
.indexOf('Forget'),
58+
).toBe(-1);
59+
expect(
60+
reactDOMFiberRendererInterface
61+
.getDisplayNameForElementID(3)
62+
.indexOf('Forget'),
63+
).toBe(-1);
64+
});
65+
66+
// @reactVersion >= 18.2
67+
it('If useMemoCache from source is used, the corresponding displayName for a component should have Forget prefix', () => {
68+
const hook = global.__REACT_DEVTOOLS_GLOBAL_HOOK__;
69+
const reactDOMFiberRendererInterface = hook.rendererInterfaces.get(1);
70+
expect(reactDOMFiberRendererInterface).not.toBeFalsy();
71+
72+
const Foo = () => {
73+
// eslint-disable-next-line no-unused-vars
74+
const $ = useMemoCache(1);
75+
// eslint-disable-next-line no-unused-vars
76+
const [val, setVal] = React.useState(null);
77+
78+
return (
79+
<div>
80+
<Bar />
81+
</div>
82+
);
83+
};
84+
const Bar = () => <div>Hi!</div>;
85+
86+
act(() => render(<Foo />));
87+
88+
// useMemoCache is only used by Foo component
89+
expect(
90+
reactDOMFiberRendererInterface
91+
.getDisplayNameForElementID(2)
92+
.indexOf('Forget'),
93+
).toBe(0);
94+
expect(
95+
reactDOMFiberRendererInterface
96+
.getDisplayNameForElementID(3)
97+
.indexOf('Forget'),
98+
).toBe(-1);
99+
});
100+
});

scripts/ci/download_devtools_regression_build.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,22 @@ async function downloadRegressionBuild() {
108108
)} ${buildPath}`
109109
);
110110
}
111+
112+
if (semver.gte(semver.coerce(version).version, '18.2.0')) {
113+
console.log(chalk.white(`Downloading react-compiler-runtime\n`));
114+
await exec(
115+
`npm install --prefix ${REGRESSION_FOLDER} react-compiler-runtime`
116+
);
117+
118+
console.log(
119+
chalk.white(
120+
`Moving react-compiler-runtime to react/compiler-runtime.js\n`
121+
)
122+
);
123+
await exec(
124+
`mv ${REGRESSION_FOLDER}/node_modules/react-compiler-runtime/dist/index.js ${buildPath}/react/compiler-runtime.js`
125+
);
126+
}
111127
}
112128

113129
async function main() {

0 commit comments

Comments
 (0)