Skip to content

Commit fce15f1

Browse files
don't fire missing act() warnings for react-art (#15975)
* use toWarnDev for dom fixture tests forks toWarnDev from root into fixture/dom, updates tes tests to use it * disable act() warnings for react-art() - For 'secondary' renderers like react-act, we don't want to fire missing act() warnings; the wrapping renderer will fire warnings anyway, and when it flushes, it flushes effects *across* renderers. - I could have used isPrimaryRenderer as the flag, but this is marked as false for react-test-renderer, and we *do* want the warning to fire for it. Hence a new flag. * add missing dependency `art` to fixtures/dom
1 parent 20f3546 commit fce15f1

File tree

12 files changed

+509
-39
lines changed

12 files changed

+509
-39
lines changed

fixtures/dom/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
},
88
"dependencies": {
99
"@babel/standalone": "^7.0.0",
10+
"art": "^0.10.3",
1011
"classnames": "^2.2.5",
1112
"codemirror": "^5.40.0",
1213
"core-js": "^2.4.1",
14+
"jest-diff": "^24.8.0",
1315
"prop-types": "^15.6.0",
1416
"query-string": "^4.2.3",
1517
"react": "^15.4.1",

fixtures/dom/src/index.test.js

Lines changed: 91 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,17 @@
99

1010
import React from 'react';
1111
import ReactDOM from 'react-dom';
12+
import ReactART from 'react-art';
13+
import ARTSVGMode from 'art/modes/svg';
14+
import ARTCurrentMode from 'art/modes/current';
1215
import TestUtils from 'react-dom/test-utils';
1316
import TestRenderer from 'react-test-renderer';
1417

15-
let spy;
16-
beforeEach(() => {
17-
spy = jest.spyOn(console, 'error').mockImplementation(() => {});
18-
});
18+
ARTCurrentMode.setCurrent(ARTSVGMode);
1919

20-
function confirmWarning() {
21-
expect(spy).toHaveBeenCalledWith(
22-
expect.stringContaining(
23-
"It looks like you're using the wrong act() around your test interactions."
24-
),
25-
''
26-
);
27-
}
20+
global.__DEV__ = process.env.NODE_ENV !== 'production';
21+
22+
expect.extend(require('./toWarnDev'));
2823

2924
function App(props) {
3025
return 'hello world';
@@ -34,29 +29,33 @@ it("doesn't warn when you use the right act + renderer: dom", () => {
3429
TestUtils.act(() => {
3530
TestUtils.renderIntoDocument(<App />);
3631
});
37-
expect(spy).not.toHaveBeenCalled();
3832
});
3933

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

47-
it('works with createRoot().render combo', () => {
40+
it('warns when using createRoot() + .render', () => {
4841
const root = ReactDOM.unstable_createRoot(document.createElement('div'));
49-
TestRenderer.act(() => {
50-
root.render(<App />);
42+
expect(() => {
43+
TestRenderer.act(() => {
44+
root.render(<App />);
45+
});
46+
}).toWarnDev(["It looks like you're using the wrong act()"], {
47+
withoutStack: true,
5148
});
52-
confirmWarning();
5349
});
5450

5551
it('warns when using the wrong act version - test + dom: render', () => {
56-
TestRenderer.act(() => {
57-
TestUtils.renderIntoDocument(<App />);
52+
expect(() => {
53+
TestRenderer.act(() => {
54+
TestUtils.renderIntoDocument(<App />);
55+
});
56+
}).toWarnDev(["It looks like you're using the wrong act()"], {
57+
withoutStack: true,
5858
});
59-
confirmWarning();
6059
});
6160

6261
it('warns when using the wrong act version - test + dom: updates', () => {
@@ -67,29 +66,35 @@ it('warns when using the wrong act version - test + dom: updates', () => {
6766
return ctr;
6867
}
6968
TestUtils.renderIntoDocument(<Counter />);
70-
TestRenderer.act(() => {
71-
setCtr(1);
72-
});
73-
confirmWarning();
69+
expect(() => {
70+
TestRenderer.act(() => {
71+
setCtr(1);
72+
});
73+
}).toWarnDev([
74+
'An update to Counter inside a test was not wrapped in act',
75+
"It looks like you're using the wrong act()",
76+
]);
7477
});
7578

7679
it('warns when using the wrong act version - dom + test: .create()', () => {
77-
TestUtils.act(() => {
78-
TestRenderer.create(<App />);
80+
expect(() => {
81+
TestUtils.act(() => {
82+
TestRenderer.create(<App />);
83+
});
84+
}).toWarnDev(["It looks like you're using the wrong act()"], {
85+
withoutStack: true,
7986
});
80-
confirmWarning();
8187
});
8288

8389
it('warns when using the wrong act version - dom + test: .update()', () => {
84-
let root;
85-
// use the right one here so we don't get the first warning
86-
TestRenderer.act(() => {
87-
root = TestRenderer.create(<App key="one" />);
90+
const root = TestRenderer.create(<App key="one" />);
91+
expect(() => {
92+
TestUtils.act(() => {
93+
root.update(<App key="two" />);
94+
});
95+
}).toWarnDev(["It looks like you're using the wrong act()"], {
96+
withoutStack: true,
8897
});
89-
TestUtils.act(() => {
90-
root.update(<App key="two" />);
91-
});
92-
confirmWarning();
9398
});
9499

95100
it('warns when using the wrong act version - dom + test: updates', () => {
@@ -100,8 +105,56 @@ it('warns when using the wrong act version - dom + test: updates', () => {
100105
return ctr;
101106
}
102107
const root = TestRenderer.create(<Counter />);
108+
expect(() => {
109+
TestUtils.act(() => {
110+
setCtr(1);
111+
});
112+
}).toWarnDev([
113+
'An update to Counter inside a test was not wrapped in act',
114+
"It looks like you're using the wrong act()",
115+
]);
116+
});
117+
118+
const {Surface, Group, Shape} = ReactART;
119+
function ARTTest(props) {
120+
return (
121+
<Surface width={150} height={200}>
122+
<Group>
123+
<Shape
124+
d="M0,0l50,0l0,50l-50,0z"
125+
fill={new ReactART.LinearGradient(['black', 'white'])}
126+
key="a"
127+
width={50}
128+
height={50}
129+
x={50}
130+
y={50}
131+
opacity={0.1}
132+
/>
133+
<Shape
134+
fill="#3C5A99"
135+
key="b"
136+
scale={0.5}
137+
x={50}
138+
y={50}
139+
title="This is an F"
140+
cursor="pointer">
141+
M64.564,38.583H54l0.008-5.834c0-3.035,0.293-4.666,4.657-4.666
142+
h5.833V16.429h-9.33c-11.213,0-15.159,5.654-15.159,15.16v6.994
143+
h-6.99v11.652h6.99v33.815H54V50.235h9.331L64.564,38.583z
144+
</Shape>
145+
</Group>
146+
</Surface>
147+
);
148+
}
149+
150+
it('does not warn when nesting react-act inside react-dom', () => {
103151
TestUtils.act(() => {
104-
setCtr(1);
152+
TestUtils.renderIntoDocument(<ARTTest />);
153+
});
154+
});
155+
156+
it('does not warn when nesting react-act inside react-test-renderer', () => {
157+
TestRenderer.act(() => {
158+
TestRenderer.create(<ARTTest />);
105159
});
106-
confirmWarning();
107160
});

0 commit comments

Comments
 (0)