@@ -71,10 +71,6 @@ main() {
71
71
react_dom.render (UseStateTest ({}), mountNode);
72
72
});
73
73
74
- tearDownAll (() {
75
- UseStateTest = null ;
76
- });
77
-
78
74
test ('initializes state correctly' , () {
79
75
expect (countRef.text, '0' );
80
76
});
@@ -94,6 +90,162 @@ main() {
94
90
});
95
91
});
96
92
93
+ group ('useEffect -' , () {
94
+ ReactDartFunctionComponentFactoryProxy UseEffectTest ;
95
+ ButtonElement countButtonRef;
96
+ DivElement countRef;
97
+ DivElement mountNode;
98
+ int useEffectCallCount;
99
+ int useEffectCleanupCallCount;
100
+ int useEffectWithDepsCallCount;
101
+ int useEffectCleanupWithDepsCallCount;
102
+ int useEffectWithDepsCallCount2;
103
+ int useEffectCleanupWithDepsCallCount2;
104
+ int useEffectWithEmptyDepsCallCount;
105
+ int useEffectCleanupWithEmptyDepsCallCount;
106
+
107
+ setUpAll (() {
108
+ mountNode = new DivElement ();
109
+ useEffectCallCount = 0 ;
110
+ useEffectCleanupCallCount = 0 ;
111
+ useEffectWithDepsCallCount = 0 ;
112
+ useEffectCleanupWithDepsCallCount = 0 ;
113
+ useEffectWithDepsCallCount2 = 0 ;
114
+ useEffectCleanupWithDepsCallCount2 = 0 ;
115
+ useEffectWithEmptyDepsCallCount = 0 ;
116
+ useEffectCleanupWithEmptyDepsCallCount = 0 ;
117
+
118
+ UseEffectTest = react.registerFunctionComponent ((Map props) {
119
+ final count = useState (0 );
120
+ final countDown = useState (0 );
121
+
122
+ useEffect (() {
123
+ useEffectCallCount++ ;
124
+ return () {
125
+ useEffectCleanupCallCount++ ;
126
+ };
127
+ });
128
+
129
+ useEffect (() {
130
+ useEffectWithDepsCallCount++ ;
131
+ return () {
132
+ useEffectCleanupWithDepsCallCount++ ;
133
+ };
134
+ }, [count.value]);
135
+
136
+ useEffect (() {
137
+ useEffectWithDepsCallCount2++ ;
138
+ return () {
139
+ useEffectCleanupWithDepsCallCount2++ ;
140
+ };
141
+ }, [countDown.value]);
142
+
143
+ useEffect (() {
144
+ useEffectWithEmptyDepsCallCount++ ;
145
+ return () {
146
+ useEffectCleanupWithEmptyDepsCallCount++ ;
147
+ };
148
+ }, []);
149
+
150
+ return react.div ({}, [
151
+ react.div ({
152
+ 'ref' : (ref) {
153
+ countRef = ref;
154
+ },
155
+ }, [
156
+ count.value
157
+ ]),
158
+ react.button ({
159
+ 'onClick' : (_) {
160
+ count.set (count.value + 1 );
161
+ },
162
+ 'ref' : (ref) {
163
+ countButtonRef = ref;
164
+ },
165
+ }, [
166
+ '+'
167
+ ]),
168
+ ]);
169
+ });
170
+
171
+ react_dom.render (UseEffectTest ({}), mountNode);
172
+ });
173
+
174
+ test ('side effect (no dependency list) is called after the first render' , () {
175
+ expect (countRef.text, '0' );
176
+
177
+ expect (useEffectCallCount, 1 );
178
+ expect (useEffectCleanupCallCount, 0 , reason: 'component has not been unmounted or re-rendered' );
179
+ });
180
+
181
+ test ('side effect (with dependency list) is called after the first render' , () {
182
+ expect (useEffectWithDepsCallCount, 1 );
183
+ expect (useEffectCleanupWithDepsCallCount, 0 , reason: 'component has not been unmounted or re-rendered' );
184
+
185
+ expect (useEffectWithDepsCallCount2, 1 );
186
+ expect (useEffectCleanupWithDepsCallCount2, 0 , reason: 'component has not been unmounted or re-rendered' );
187
+ });
188
+
189
+ test ('side effect (with empty dependency list) is called after the first render' , () {
190
+ expect (useEffectWithEmptyDepsCallCount, 1 );
191
+ expect (useEffectCleanupWithEmptyDepsCallCount, 0 , reason: 'component has not been unmounted or re-rendered' );
192
+ });
193
+
194
+ group ('after state change,' , () {
195
+ setUpAll (() {
196
+ react_test_utils.Simulate .click (countButtonRef);
197
+ });
198
+
199
+ test ('side effect (no dependency list) is called again' , () {
200
+ expect (countRef.text, '1' );
201
+
202
+ expect (useEffectCallCount, 2 );
203
+ expect (useEffectCleanupCallCount, 1 , reason: 'cleanup called before re-render' );
204
+ });
205
+
206
+ test ('side effect (with dependency list) is called again if one of its dependencies changed' , () {
207
+ expect (useEffectWithDepsCallCount, 2 , reason: 'count.value changed' );
208
+ expect (useEffectCleanupWithDepsCallCount, 1 , reason: 'cleanup called before re-render' );
209
+ });
210
+
211
+ test ('side effect (with dependency list) is not called again if none of its dependencies changed' , () {
212
+ expect (useEffectWithDepsCallCount2, 1 , reason: 'countDown.value did not change' );
213
+ expect (useEffectCleanupWithDepsCallCount2, 0 ,
214
+ reason: 'cleanup not called because countDown.value did not change' );
215
+ });
216
+
217
+ test ('side effect (with empty dependency list) is not called again' , () {
218
+ expect (useEffectWithEmptyDepsCallCount, 1 ,
219
+ reason: 'side effect is only called once for empty dependency list' );
220
+ expect (useEffectCleanupWithEmptyDepsCallCount, 0 , reason: 'component has not been unmounted' );
221
+ });
222
+ });
223
+
224
+ group ('after component is unmounted,' , () {
225
+ setUpAll (() {
226
+ react_dom.unmountComponentAtNode (mountNode);
227
+ });
228
+
229
+ test ('cleanup (no dependency list) is called' , () {
230
+ expect (useEffectCallCount, 2 , reason: 'side effect not called on unmount' );
231
+ expect (useEffectCleanupCallCount, 2 );
232
+ });
233
+
234
+ test ('cleanup (with dependency list) is called' , () {
235
+ expect (useEffectWithDepsCallCount, 2 , reason: 'side effect not called on unmount' );
236
+ expect (useEffectCleanupWithDepsCallCount, 2 );
237
+
238
+ expect (useEffectWithDepsCallCount2, 1 , reason: 'side effect not called on unmount' );
239
+ expect (useEffectCleanupWithDepsCallCount2, 1 );
240
+ });
241
+
242
+ test ('cleanup (with empty dependency list) is called' , () {
243
+ expect (useEffectWithEmptyDepsCallCount, 1 , reason: 'side effect not called on unmount' );
244
+ expect (useEffectCleanupWithEmptyDepsCallCount, 1 );
245
+ });
246
+ });
247
+ });
248
+
97
249
group ('useCallback -' , () {
98
250
ReactDartFunctionComponentFactoryProxy UseCallbackTest ;
99
251
DivElement deltaRef;
@@ -166,10 +318,6 @@ main() {
166
318
react_dom.render (UseCallbackTest ({}), mountNode);
167
319
});
168
320
169
- tearDownAll (() {
170
- UseCallbackTest = null ;
171
- });
172
-
173
321
test ('callback is called correctly' , () {
174
322
expect (countRef.text, '0' );
175
323
expect (deltaRef.text, '1' );
0 commit comments