Skip to content

Commit

Permalink
warn when using the wrong act() around create/updates
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Sunil Pai committed Apr 15, 2019
1 parent 46a4237 commit e4d93a7
Show file tree
Hide file tree
Showing 26 changed files with 10,644 additions and 82 deletions.
1 change: 1 addition & 0 deletions fixtures/act/.env
@@ -0,0 +1 @@
SKIP_PREFLIGHT_CHECK=true
23 changes: 23 additions & 0 deletions fixtures/act/.gitignore
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
6 changes: 6 additions & 0 deletions fixtures/act/README.md
@@ -0,0 +1,6 @@
act() fixtures
---

- `yarn`
- `yarn start` - follow instructions on the page
- `yarn test` - all green
27 changes: 27 additions & 0 deletions fixtures/act/package.json
@@ -0,0 +1,27 @@
{
"name": "act",
"version": "0.1.0",
"private": true,
"dependencies": {
"art": "^0.10.3",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-scripts": "2.1.8"
},
"scripts": {
"prestart": "cp -a ../../build/node_modules/. node_modules",
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
Binary file added fixtures/act/public/favicon.ico
Binary file not shown.
42 changes: 42 additions & 0 deletions fixtures/act/public/index.html
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta name="theme-color" content="#000000" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
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
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
15 changes: 15 additions & 0 deletions fixtures/act/public/manifest.json
@@ -0,0 +1,15 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
114 changes: 114 additions & 0 deletions fixtures/act/src/index.js
@@ -0,0 +1,114 @@
import React from 'react';
import ReactDOM from 'react-dom';
import ReactTestUtils from 'react-dom/test-utils';
import ReactTestRenderer from 'react-test-renderer';

import ReactART from 'react-art';
import ARTSVGMode from 'art/modes/svg';
import ARTCurrentMode from 'art/modes/current';

const {Shape, Surface} = ReactART;

window.jest = {}; // to enable warnings
function App() {
let [state, setState] = React.useState(0);
async function ticker() {
await null;
setState(x => x + 1);
}
React.useEffect(
() => {
ticker();
},
[Math.min(state, 4)]
);
return state;
}

async function testDOMAsynAct() {
// from ReactTestUtilsAct-test.js

const el = document.createElement('div');
await ReactTestUtils.act(async () => {
ReactDOM.render(React.createElement(App), el);
});
// all 5 ticks present and accounted for
console.log(el.innerHTML);
}

async function testMixRenderers() {
await ReactTestUtils.act(async () => {
ReactTestRenderer.create(React.createElement(App));
});
}

async function testARTDOM() {
ARTCurrentMode.setCurrent(ARTSVGMode);
let setState;
function TestComponent(props) {
// eslint-disable-next-line no-unused-vars
const [state, _setState] = React.useState(0);
setState = _setState;

return (
<Surface width={150} height={200}>
<Shape
fill="#3C5A99"
key="b"
scale={0.5}
x={50}
y={50}
title="This is an F"
cursor="pointer">
M64.564,38.583H54l0.008-5.834c0-3.035,0.293-4.666,4.657-4.666
h5.833V16.429h-9.33c-11.213,0-15.159,5.654-15.159,15.16v6.994
h-6.99v11.652h6.99v33.815H54V50.235h9.331L64.564,38.583z
</Shape>
</Surface>
);
}
ReactDOM.render(<TestComponent />, window.root);
ReactTestUtils.act(() => {
setState(2);
});
}

async function testARTTest() {
ARTCurrentMode.setCurrent(ARTSVGMode);
let setState;
function TestComponent(props) {
// eslint-disable-next-line no-unused-vars
const [state, _setState] = React.useState(0);
setState = _setState;

return (
<Surface width={150} height={200}>
<Shape
fill="#3C5A99"
key="b"
scale={0.5}
x={50}
y={50}
title="This is an F"
cursor="pointer">
M64.564,38.583H54l0.008-5.834c0-3.035,0.293-4.666,4.657-4.666
h5.833V16.429h-9.33c-11.213,0-15.159,5.654-15.159,15.16v6.994
h-6.99v11.652h6.99v33.815H54V50.235h9.331L64.564,38.583z
</Shape>
</Surface>
);
}
ReactTestRenderer.create(<TestComponent />);
ReactTestRenderer.act(() => {
setState(2);
});
}

async function run() {
await testDOMAsynAct();
await testMixRenderers();
await testARTDOM();
await testARTTest();
}

run();
78 changes: 78 additions & 0 deletions fixtures/act/src/index.test.js
@@ -0,0 +1,78 @@
import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-dom/test-utils';
import TestRenderer from 'react-test-renderer';

let spy;
beforeEach(() => {
spy = jest.spyOn(global.console, 'error');
});

function confirmWarning() {
expect(spy).toHaveBeenCalledWith(
expect.stringContaining(
"It looks like you're using the wrong act() around your test interactions."
),
''
);
}

function App(props) {
return 'hello world';
}

it("doesn't warn when you use the right act + renderer: dom", () => {
TestUtils.act(() => {
TestUtils.renderIntoDocument(<App />);
});
expect(spy).not.toHaveBeenCalled();
});

it("doesn't warn when you use the right act + renderer: test", () => {
TestRenderer.act(() => {
TestRenderer.create(<App />);
});
expect(spy).not.toHaveBeenCalled();
});

it('warns when using the wrong act version - test + dom: render', () => {
TestRenderer.act(() => {
TestUtils.renderIntoDocument(<App />);
});
confirmWarning();
});

it('warns when using the wrong act version - test + dom: updates', () => {
let setCtr;
function Counter(props) {
const [ctr, _setCtr] = React.useState(0);
setCtr = _setCtr;
return ctr;
}
TestUtils.renderIntoDocument(<Counter />);
TestRenderer.act(() => {
setCtr(1);
});
confirmWarning();
});

it('warns when using the wrong act version - dom + test: render', () => {
TestUtils.act(() => {
TestRenderer.create(<App />);
});
confirmWarning();
});

it('warns when using the wrong act version - dom + test: updates', () => {
let setCtr;
function Counter(props) {
const [ctr, _setCtr] = React.useState(0);
setCtr = _setCtr;
return ctr;
}
const root = TestRenderer.create(<Counter />);
TestUtils.act(() => {
setCtr(1);
});
confirmWarning();
});

0 comments on commit e4d93a7

Please sign in to comment.