9
9
10
10
import React from 'react' ;
11
11
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' ;
12
15
import TestUtils from 'react-dom/test-utils' ;
13
16
import TestRenderer from 'react-test-renderer' ;
14
17
15
- let spy ;
16
- beforeEach ( ( ) => {
17
- spy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
18
- } ) ;
18
+ ARTCurrentMode . setCurrent ( ARTSVGMode ) ;
19
19
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' ) ) ;
28
23
29
24
function App ( props ) {
30
25
return 'hello world' ;
@@ -34,29 +29,33 @@ it("doesn't warn when you use the right act + renderer: dom", () => {
34
29
TestUtils . act ( ( ) => {
35
30
TestUtils . renderIntoDocument ( < App /> ) ;
36
31
} ) ;
37
- expect ( spy ) . not . toHaveBeenCalled ( ) ;
38
32
} ) ;
39
33
40
34
it ( "doesn't warn when you use the right act + renderer: test" , ( ) => {
41
35
TestRenderer . act ( ( ) => {
42
36
TestRenderer . create ( < App /> ) ;
43
37
} ) ;
44
- expect ( spy ) . not . toHaveBeenCalled ( ) ;
45
38
} ) ;
46
39
47
- it ( 'works with createRoot().render combo ' , ( ) => {
40
+ it ( 'warns when using createRoot() + .render' , ( ) => {
48
41
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 ,
51
48
} ) ;
52
- confirmWarning ( ) ;
53
49
} ) ;
54
50
55
51
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 ,
58
58
} ) ;
59
- confirmWarning ( ) ;
60
59
} ) ;
61
60
62
61
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', () => {
67
66
return ctr ;
68
67
}
69
68
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
+ ] ) ;
74
77
} ) ;
75
78
76
79
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 ,
79
86
} ) ;
80
- confirmWarning ( ) ;
81
87
} ) ;
82
88
83
89
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 ,
88
97
} ) ;
89
- TestUtils . act ( ( ) => {
90
- root . update ( < App key = "two" /> ) ;
91
- } ) ;
92
- confirmWarning ( ) ;
93
98
} ) ;
94
99
95
100
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', () => {
100
105
return ctr ;
101
106
}
102
107
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' , ( ) => {
103
151
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 /> ) ;
105
159
} ) ;
106
- confirmWarning ( ) ;
107
160
} ) ;
0 commit comments