Skip to content

Commit e4d93a7

Browse files
author
Sunil Pai
committed
warn when using the wrong act() around create/updates
via facebook#15319 This solves 2 specific problems - - using the 'wrong' act() doesn't silence the warning - using the wrong act logs a warning It does this by using an empty object on the reconciler as the identity of ReactShouldWarnActingUpdates.current. We also add this check when calling createContainer() to catch the common failure of this happening right in the beginning. make a proper fixture for act() made fixtures/act for act(). specifically, this lets us tests how act behaves when you use the wrong act() for the wrong renderer. also mvoes dom/../act-dom.html to this fixture. cleanup fixtures/dom/.gitignore verify that it 'works' with art+dom verify that it 'works' with art+test augh prettier tweak warning messages
1 parent 46a4237 commit e4d93a7

26 files changed

+10644
-82
lines changed

fixtures/act/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SKIP_PREFLIGHT_CHECK=true

fixtures/act/.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*

fixtures/act/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
act() fixtures
2+
---
3+
4+
- `yarn`
5+
- `yarn start` - follow instructions on the page
6+
- `yarn test` - all green

fixtures/act/package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "act",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"art": "^0.10.3",
7+
"react": "^16.8.6",
8+
"react-dom": "^16.8.6",
9+
"react-scripts": "2.1.8"
10+
},
11+
"scripts": {
12+
"prestart": "cp -a ../../build/node_modules/. node_modules",
13+
"start": "react-scripts start",
14+
"build": "react-scripts build",
15+
"test": "react-scripts test",
16+
"eject": "react-scripts eject"
17+
},
18+
"eslintConfig": {
19+
"extends": "react-app"
20+
},
21+
"browserslist": [
22+
">0.2%",
23+
"not dead",
24+
"not ie <= 11",
25+
"not op_mini all"
26+
]
27+
}

fixtures/act/public/favicon.ico

3.78 KB
Binary file not shown.

fixtures/act/public/index.html

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
6+
<meta
7+
name="viewport"
8+
content="width=device-width, initial-scale=1, shrink-to-fit=no"
9+
/>
10+
<meta name="theme-color" content="#000000" />
11+
<!--
12+
manifest.json provides metadata used when your web app is installed on a
13+
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
14+
-->
15+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
16+
<!--
17+
Notice the use of %PUBLIC_URL% in the tags above.
18+
It will be replaced with the URL of the `public` folder during the build.
19+
Only files inside the `public` folder can be referenced from the HTML.
20+
21+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
22+
work correctly both with client-side routing and a non-root public URL.
23+
Learn how to configure a non-root public URL by running `npm run build`.
24+
-->
25+
<title>React App</title>
26+
</head>
27+
<body>
28+
<noscript>You need to enable JavaScript to run this app.</noscript>
29+
this page tests whether act runs properly in a browser. your console should say "5", a warning about not using the right act(), and some act() errors
30+
<div id="root"></div>
31+
<!--
32+
This HTML file is a template.
33+
If you open it directly in the browser, you will see an empty page.
34+
35+
You can add webfonts, meta tags, or analytics to this file.
36+
The build step will place the bundled scripts into the <body> tag.
37+
38+
To begin the development, run `npm start` or `yarn start`.
39+
To create a production bundle, use `npm run build` or `yarn build`.
40+
-->
41+
</body>
42+
</html>

fixtures/act/public/manifest.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"short_name": "React App",
3+
"name": "Create React App Sample",
4+
"icons": [
5+
{
6+
"src": "favicon.ico",
7+
"sizes": "64x64 32x32 24x24 16x16",
8+
"type": "image/x-icon"
9+
}
10+
],
11+
"start_url": ".",
12+
"display": "standalone",
13+
"theme_color": "#000000",
14+
"background_color": "#ffffff"
15+
}

fixtures/act/src/index.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import ReactTestUtils from 'react-dom/test-utils';
4+
import ReactTestRenderer from 'react-test-renderer';
5+
6+
import ReactART from 'react-art';
7+
import ARTSVGMode from 'art/modes/svg';
8+
import ARTCurrentMode from 'art/modes/current';
9+
10+
const {Shape, Surface} = ReactART;
11+
12+
window.jest = {}; // to enable warnings
13+
function App() {
14+
let [state, setState] = React.useState(0);
15+
async function ticker() {
16+
await null;
17+
setState(x => x + 1);
18+
}
19+
React.useEffect(
20+
() => {
21+
ticker();
22+
},
23+
[Math.min(state, 4)]
24+
);
25+
return state;
26+
}
27+
28+
async function testDOMAsynAct() {
29+
// from ReactTestUtilsAct-test.js
30+
31+
const el = document.createElement('div');
32+
await ReactTestUtils.act(async () => {
33+
ReactDOM.render(React.createElement(App), el);
34+
});
35+
// all 5 ticks present and accounted for
36+
console.log(el.innerHTML);
37+
}
38+
39+
async function testMixRenderers() {
40+
await ReactTestUtils.act(async () => {
41+
ReactTestRenderer.create(React.createElement(App));
42+
});
43+
}
44+
45+
async function testARTDOM() {
46+
ARTCurrentMode.setCurrent(ARTSVGMode);
47+
let setState;
48+
function TestComponent(props) {
49+
// eslint-disable-next-line no-unused-vars
50+
const [state, _setState] = React.useState(0);
51+
setState = _setState;
52+
53+
return (
54+
<Surface width={150} height={200}>
55+
<Shape
56+
fill="#3C5A99"
57+
key="b"
58+
scale={0.5}
59+
x={50}
60+
y={50}
61+
title="This is an F"
62+
cursor="pointer">
63+
M64.564,38.583H54l0.008-5.834c0-3.035,0.293-4.666,4.657-4.666
64+
h5.833V16.429h-9.33c-11.213,0-15.159,5.654-15.159,15.16v6.994
65+
h-6.99v11.652h6.99v33.815H54V50.235h9.331L64.564,38.583z
66+
</Shape>
67+
</Surface>
68+
);
69+
}
70+
ReactDOM.render(<TestComponent />, window.root);
71+
ReactTestUtils.act(() => {
72+
setState(2);
73+
});
74+
}
75+
76+
async function testARTTest() {
77+
ARTCurrentMode.setCurrent(ARTSVGMode);
78+
let setState;
79+
function TestComponent(props) {
80+
// eslint-disable-next-line no-unused-vars
81+
const [state, _setState] = React.useState(0);
82+
setState = _setState;
83+
84+
return (
85+
<Surface width={150} height={200}>
86+
<Shape
87+
fill="#3C5A99"
88+
key="b"
89+
scale={0.5}
90+
x={50}
91+
y={50}
92+
title="This is an F"
93+
cursor="pointer">
94+
M64.564,38.583H54l0.008-5.834c0-3.035,0.293-4.666,4.657-4.666
95+
h5.833V16.429h-9.33c-11.213,0-15.159,5.654-15.159,15.16v6.994
96+
h-6.99v11.652h6.99v33.815H54V50.235h9.331L64.564,38.583z
97+
</Shape>
98+
</Surface>
99+
);
100+
}
101+
ReactTestRenderer.create(<TestComponent />);
102+
ReactTestRenderer.act(() => {
103+
setState(2);
104+
});
105+
}
106+
107+
async function run() {
108+
await testDOMAsynAct();
109+
await testMixRenderers();
110+
await testARTDOM();
111+
await testARTTest();
112+
}
113+
114+
run();

fixtures/act/src/index.test.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import TestUtils from 'react-dom/test-utils';
4+
import TestRenderer from 'react-test-renderer';
5+
6+
let spy;
7+
beforeEach(() => {
8+
spy = jest.spyOn(global.console, 'error');
9+
});
10+
11+
function confirmWarning() {
12+
expect(spy).toHaveBeenCalledWith(
13+
expect.stringContaining(
14+
"It looks like you're using the wrong act() around your test interactions."
15+
),
16+
''
17+
);
18+
}
19+
20+
function App(props) {
21+
return 'hello world';
22+
}
23+
24+
it("doesn't warn when you use the right act + renderer: dom", () => {
25+
TestUtils.act(() => {
26+
TestUtils.renderIntoDocument(<App />);
27+
});
28+
expect(spy).not.toHaveBeenCalled();
29+
});
30+
31+
it("doesn't warn when you use the right act + renderer: test", () => {
32+
TestRenderer.act(() => {
33+
TestRenderer.create(<App />);
34+
});
35+
expect(spy).not.toHaveBeenCalled();
36+
});
37+
38+
it('warns when using the wrong act version - test + dom: render', () => {
39+
TestRenderer.act(() => {
40+
TestUtils.renderIntoDocument(<App />);
41+
});
42+
confirmWarning();
43+
});
44+
45+
it('warns when using the wrong act version - test + dom: updates', () => {
46+
let setCtr;
47+
function Counter(props) {
48+
const [ctr, _setCtr] = React.useState(0);
49+
setCtr = _setCtr;
50+
return ctr;
51+
}
52+
TestUtils.renderIntoDocument(<Counter />);
53+
TestRenderer.act(() => {
54+
setCtr(1);
55+
});
56+
confirmWarning();
57+
});
58+
59+
it('warns when using the wrong act version - dom + test: render', () => {
60+
TestUtils.act(() => {
61+
TestRenderer.create(<App />);
62+
});
63+
confirmWarning();
64+
});
65+
66+
it('warns when using the wrong act version - dom + test: updates', () => {
67+
let setCtr;
68+
function Counter(props) {
69+
const [ctr, _setCtr] = React.useState(0);
70+
setCtr = _setCtr;
71+
return ctr;
72+
}
73+
const root = TestRenderer.create(<Counter />);
74+
TestUtils.act(() => {
75+
setCtr(1);
76+
});
77+
confirmWarning();
78+
});

0 commit comments

Comments
 (0)