-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathhooks_test.dart
1058 lines (884 loc) · 33 KB
/
hooks_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@TestOn('browser')
@JS()
library hooks_test;
import 'dart:html';
import 'package:js/js.dart';
import 'package:react/hooks.dart';
import 'package:react/react.dart' as react;
import 'package:react/react.dart';
import 'package:react/react_client.dart';
import 'package:react/react_dom.dart' as react_dom;
import 'package:react/react_test_utils.dart' as react_test_utils;
import 'package:test/test.dart';
import 'factory/common_factory_tests.dart';
main() {
group('React Hooks: ', () {
group('useState -', () {
DivElement? textRef;
DivElement? countRef;
ButtonElement? setButtonRef;
ButtonElement? setWithUpdaterButtonRef;
setUpAll(() {
final mountNode = DivElement();
final UseStateTest = react.registerFunctionComponent((props) {
final text = useStateLazy(() {
return 'initialValue';
});
final count = useState(0);
return react.div({}, [
react.div({
'ref': (ref) {
textRef = ref as DivElement?;
},
}, [
text.value
]),
react.div({
'ref': (ref) {
countRef = ref as DivElement?;
},
}, [
count.value
]),
react.button({
'onClick': (_) => text.set('newValue'),
'ref': (ref) {
setButtonRef = ref as ButtonElement?;
},
}, [
'Set'
]),
react.button({
'onClick': (_) => count.setWithUpdater((prev) => prev + 1),
'ref': (ref) {
setWithUpdaterButtonRef = ref as ButtonElement?;
},
}, [
'+'
]),
]);
});
react_dom.render(UseStateTest({}), mountNode);
});
test('initializes state correctly', () {
expect(countRef!.text, '0');
});
test('Lazy initializes state correctly', () {
expect(textRef!.text, 'initialValue');
});
test('StateHook.set updates state correctly', () {
react_test_utils.Simulate.click(setButtonRef);
expect(textRef!.text, 'newValue');
});
test('StateHook.setWithUpdater updates state correctly', () {
react_test_utils.Simulate.click(setWithUpdaterButtonRef);
expect(countRef!.text, '1');
});
});
group('useEffect -', () {
testEffectHook(useEffect);
});
group('useReducer -', () {
DivElement? textRef;
DivElement? countRef;
ButtonElement? addButtonRef;
ButtonElement? subtractButtonRef;
ButtonElement? textButtonRef;
Map reducer(Map state, Map action) {
switch (action['type'] as String) {
case 'increment':
return {...state, 'count': state['count'] + 1};
case 'decrement':
return {...state, 'count': state['count'] - 1};
case 'changeText':
return {...state, 'text': action['newText']};
default:
return state;
}
}
setUpAll(() {
final mountNode = DivElement();
final UseReducerTest = react.registerFunctionComponent((props) {
final state = useReducer(reducer, {
'text': 'initialValue',
'count': 0,
});
return react.div({}, [
react.div({
'ref': (ref) {
textRef = ref as DivElement?;
},
}, [
state.state['text']
]),
react.div({
'ref': (ref) {
countRef = ref as DivElement?;
},
}, [
state.state['count']
]),
react.button({
'onClick': (_) => state.dispatch({'type': 'changeText', 'newText': 'newValue'}),
'ref': (ref) {
textButtonRef = ref as ButtonElement?;
},
}, [
'Set'
]),
react.button({
'onClick': (_) => state.dispatch({'type': 'increment'}),
'ref': (ref) {
addButtonRef = ref as ButtonElement?;
},
}, [
'+'
]),
react.button({
'onClick': (_) => state.dispatch({'type': 'decrement'}),
'ref': (ref) {
subtractButtonRef = ref as ButtonElement?;
},
}, [
'-'
]),
]);
});
react_dom.render(UseReducerTest({}), mountNode);
});
test('initializes state correctly', () {
expect(countRef!.text, '0');
expect(textRef!.text, 'initialValue');
});
test('dispatch updates states correctly', () {
react_test_utils.Simulate.click(textButtonRef);
expect(textRef!.text, 'newValue');
react_test_utils.Simulate.click(addButtonRef);
expect(countRef!.text, '1');
react_test_utils.Simulate.click(subtractButtonRef);
expect(countRef!.text, '0');
});
});
group('useReducerLazy', () {
DivElement? countRef;
ButtonElement? addButtonRef;
ButtonElement? subtractButtonRef;
ButtonElement? resetButtonRef;
Map initializeCount(int initialValue) {
return {'count': initialValue};
}
Map reducer2(Map state, Map action) {
switch (action['type'] as String) {
case 'increment':
return {...state, 'count': state['count'] + 1};
case 'decrement':
return {...state, 'count': state['count'] - 1};
case 'reset':
return initializeCount(action['payload'] as int);
default:
return state;
}
}
setUpAll(() {
final mountNode = DivElement();
final UseReducerTest = react.registerFunctionComponent((props) {
final state = useReducerLazy<Map, Map, int>(reducer2, props['initialCount'] as int, initializeCount);
return react.div({}, [
react.div({
'ref': (ref) {
countRef = ref as DivElement?;
},
}, [
state.state['count']
]),
react.button({
'onClick': (_) => state.dispatch({'type': 'reset', 'payload': props['initialCount']}),
'ref': (ref) {
resetButtonRef = ref as ButtonElement?;
},
}, [
'reset'
]),
react.button({
'onClick': (_) => state.dispatch({'type': 'increment'}),
'ref': (ref) {
addButtonRef = ref as ButtonElement?;
},
}, [
'+'
]),
react.button({
'onClick': (_) => state.dispatch({'type': 'decrement'}),
'ref': (ref) {
subtractButtonRef = ref as ButtonElement?;
},
}, [
'-'
]),
]);
});
react_dom.render(UseReducerTest({'initialCount': 10}), mountNode);
});
test('initializes state correctly', () {
expect(countRef!.text, '10');
});
test('dispatch updates states correctly', () {
react_test_utils.Simulate.click(addButtonRef);
expect(countRef!.text, '11');
react_test_utils.Simulate.click(resetButtonRef);
expect(countRef!.text, '10');
react_test_utils.Simulate.click(subtractButtonRef);
expect(countRef!.text, '9');
});
});
group('useCallback -', () {
DivElement? deltaRef;
DivElement? countRef;
ButtonElement? incrementWithDepButtonRef;
ButtonElement? incrementNoDepButtonRef;
ButtonElement? incrementDeltaButtonRef;
setUpAll(() {
final mountNode = DivElement();
final UseCallbackTest = react.registerFunctionComponent((props) {
final count = useState(0);
final delta = useState(1);
final incrementNoDep = useCallback((_) {
count.setWithUpdater((prev) => prev + delta.value);
}, []);
final incrementWithDep = useCallback((_) {
count.setWithUpdater((prev) => prev + delta.value);
}, [delta.value]);
final incrementDelta = useCallback((_) {
delta.setWithUpdater((prev) => prev + 1);
}, []);
return react.div({}, [
react.div({
'ref': (ref) {
deltaRef = ref as DivElement?;
},
}, [
delta.value
]),
react.div({
'ref': (ref) {
countRef = ref as DivElement?;
},
}, [
count.value
]),
react.button({
'onClick': incrementNoDep,
'ref': (ref) {
incrementNoDepButtonRef = ref as ButtonElement?;
},
}, [
'Increment count no dep'
]),
react.button({
'onClick': incrementWithDep,
'ref': (ref) {
incrementWithDepButtonRef = ref as ButtonElement?;
},
}, [
'Increment count'
]),
react.button({
'onClick': incrementDelta,
'ref': (ref) {
incrementDeltaButtonRef = ref as ButtonElement?;
},
}, [
'Increment delta'
]),
]);
});
react_dom.render(UseCallbackTest({}), mountNode);
});
test('callback is called correctly', () {
expect(countRef!.text, '0');
expect(deltaRef!.text, '1');
react_test_utils.Simulate.click(incrementNoDepButtonRef);
expect(countRef!.text, '1');
react_test_utils.Simulate.click(incrementWithDepButtonRef);
expect(countRef!.text, '2');
});
group('after depending state changes,', () {
setUpAll(() {
react_test_utils.Simulate.click(incrementDeltaButtonRef);
});
test('callback stays the same if state not in dependency list', () {
react_test_utils.Simulate.click(incrementNoDepButtonRef);
expect(countRef!.text, '3', reason: 'still increments by 1 because delta not in dependency list');
});
test('callback updates if state is in dependency list', () {
react_test_utils.Simulate.click(incrementWithDepButtonRef);
expect(countRef!.text, '5', reason: 'increments by 2 because delta updated');
});
});
});
group('useContext -', () {
late DivElement mountNode;
_ContextProviderWrapper? providerRef;
late int currentCount;
late Context<int> testContext;
Function useContextTestFunctionComponent;
setUp(() {
mountNode = DivElement();
currentCount = 0;
UseContextTestComponent(Map props) {
final context = useContext(testContext);
currentCount = context;
return react.div({
'key': 'uct1'
}, [
react.div({'key': 'uct2'}, ['useContext counter value is $context']),
]);
}
testContext = react.createContext(1);
useContextTestFunctionComponent =
react.registerFunctionComponent(UseContextTestComponent, displayName: 'useContextTest');
react_dom.render(
ContextProviderWrapper({
'contextToUse': testContext,
'mode': 'increment',
'ref': (ref) {
providerRef = ref as _ContextProviderWrapper?;
}
}, [
useContextTestFunctionComponent({'key': 't1'}, []),
]),
mountNode);
});
group('updates with the correct values', () {
test('on first render', () {
expect(currentCount, 1);
});
test('on value updates', () {
providerRef!.increment();
expect(currentCount, 2);
providerRef!.increment();
expect(currentCount, 3);
providerRef!.increment();
expect(currentCount, 4);
});
});
});
group('useRef -', () {
late DivElement mountNode;
ButtonElement? reRenderButton;
late Ref noInitRef;
late Ref initRef;
late Ref domElementRef;
late StateHook<int> renderIndex;
late Ref refFromUseRef;
late Ref refFromCreateRef;
setUpAll(() {
mountNode = DivElement();
final UseRefTest = react.registerFunctionComponent((props) {
// ignore: deprecated_member_use_from_same_package
noInitRef = useRef();
// ignore: deprecated_member_use_from_same_package
initRef = useRef(mountNode);
// ignore: deprecated_member_use_from_same_package
domElementRef = useRef();
renderIndex = useState(1);
// ignore: deprecated_member_use_from_same_package
refFromUseRef = useRef();
refFromCreateRef = react.createRef();
refFromUseRef.current ??= renderIndex.value;
refFromCreateRef.current ??= renderIndex.value;
return react.Fragment({}, [
react.input({'ref': domElementRef}),
react.p({}, [renderIndex.value]),
react.p({}, [refFromUseRef.current]),
react.p({}, [refFromCreateRef.current]),
react.button({
'ref': (ref) => reRenderButton = ref as ButtonElement?,
'onClick': (_) => renderIndex.setWithUpdater((prev) => prev + 1)
}, [
're-render'
]),
]);
});
react_dom.render(UseRefTest({}), mountNode);
});
group('correctly initializes a Ref object', () {
test('with current property set to null if no initial value given', () {
expect(noInitRef, isA<Ref>());
expect(noInitRef.current, null);
});
test('with current property set to the initial value given', () {
expect(initRef, isA<Ref>());
expect(initRef.current, mountNode);
});
});
group('the returned Ref', () {
test('can be attached to elements via the ref attribute', () {
expect(domElementRef.current, isA<InputElement>());
});
test('will persist even after the component re-renders', () {
expect(renderIndex.value, 1);
expect(refFromUseRef.current, 1, reason: 'Ref object initially created on first render');
expect(refFromCreateRef.current, 1);
react_test_utils.Simulate.click(reRenderButton);
expect(renderIndex.value, 2);
expect(refFromUseRef.current, 1,
reason: 'useRef returns the same Ref object on every render for the full lifetime of the component');
expect(refFromCreateRef.current, 2,
reason: 'compare to createRef which creates a new Ref object on every render');
});
});
});
group('useRefInit -', () {
late DivElement mountNode;
ButtonElement? reRenderButton;
late Ref initRef;
late StateHook<int> renderIndex;
setUpAll(() {
mountNode = DivElement();
final UseRefTest = react.registerFunctionComponent((props) {
initRef = useRefInit(mountNode);
renderIndex = useState(1);
return react.Fragment({}, [
react.p({}, [renderIndex.value]),
react.button({
'ref': (ref) => reRenderButton = ref as ButtonElement?,
'onClick': (_) => renderIndex.setWithUpdater((prev) => prev + 1)
}, [
're-render'
]),
]);
});
react_dom.render(UseRefTest({}), mountNode);
});
group('correctly initializes a Ref object', () {
test('with current property set to the initial value given', () {
expect(initRef, isA<Ref>());
expect(initRef.current, mountNode);
});
});
group('the returned Ref', () {
test('will persist even after the component re-renders', () {
expect(renderIndex.value, 1);
expect(initRef.current, mountNode, reason: 'Ref object initially created on first render');
react_test_utils.Simulate.click(reRenderButton);
expect(renderIndex.value, 2);
expect(initRef.current, mountNode,
reason: 'useRef returns the same Ref object on every render for the full lifetime of the component');
});
});
});
group('useMemo -', () {
late StateHook<int> count;
ButtonElement? reRenderButtonRef;
ButtonElement? incrementButtonRef;
// Count how many times createFunction() is called for each variation of dependencies.
var createFunctionCallCountWithDeps = 0;
var createFunctionCallCountNoDeps = 0;
var createFunctionCallCountEmptyDeps = 0;
// Keeps track of return value of useMemo() for each variation of dependencies.
late int returnValueWithDeps;
late int returnValueNoDeps;
late int returnValueEmptyDeps;
int fibonacci(int n) {
if (n <= 1) {
return 1;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
setUpAll(() {
final mountNode = DivElement();
final UseMemoTest = react.registerFunctionComponent((props) {
final reRender = useState(0);
count = useState(5);
returnValueWithDeps = useMemo(
() {
createFunctionCallCountWithDeps++;
return fibonacci(count.value);
},
[count.value],
);
returnValueNoDeps = useMemo(
() {
createFunctionCallCountNoDeps++;
return fibonacci(count.value);
},
);
returnValueEmptyDeps = useMemo(
() {
createFunctionCallCountEmptyDeps++;
return fibonacci(count.value);
},
[],
);
return react.Fragment({}, [
react.button({
'ref': (ref) => incrementButtonRef = ref as ButtonElement?,
'onClick': (_) => count.setWithUpdater((prev) => prev + 1),
}, [
'+'
]),
react.button({
'ref': (ref) => reRenderButtonRef = ref as ButtonElement?,
'onClick': (_) => reRender.setWithUpdater((prev) => prev + 1)
}, [
're-render'
]),
]);
});
react_dom.render(UseMemoTest({}), mountNode);
});
test('correctly initializes memoized value', () {
expect(count.value, 5);
expect(returnValueWithDeps, 8);
expect(returnValueNoDeps, 8);
expect(returnValueEmptyDeps, 8);
expect(createFunctionCallCountWithDeps, 1);
expect(createFunctionCallCountNoDeps, 1);
expect(createFunctionCallCountEmptyDeps, 1);
});
group('after depending state changes,', () {
setUpAll(() {
react_test_utils.Simulate.click(incrementButtonRef);
});
test('createFunction does not run if state not in dependency list', () {
expect(returnValueEmptyDeps, 8);
expect(createFunctionCallCountEmptyDeps, 1, reason: 'count.value is not in dependency list');
});
test('createFunction re-runs if state is in dependency list or if there is no dependency list', () {
expect(returnValueWithDeps, 13);
expect(returnValueNoDeps, 13);
expect(createFunctionCallCountWithDeps, 2, reason: 'count.value is in dependency list');
expect(createFunctionCallCountNoDeps, 2,
reason: 'createFunction runs on every render because there is no dependency list');
});
});
group('after component re-renders,', () {
setUpAll(() {
react_test_utils.Simulate.click(reRenderButtonRef);
});
test('createFunction re-runs if there is no dependency list', () {
expect(returnValueNoDeps, 13, reason: 'count.value stayed the same so the same value is returned');
expect(createFunctionCallCountNoDeps, 3,
reason: 'createFunction runs on every render because there is no dependency list');
});
test('createFunction does not run if there is a dependency list', () {
expect(returnValueEmptyDeps, 8);
expect(returnValueWithDeps, 13);
expect(createFunctionCallCountEmptyDeps, 1, reason: 'no dependency changed');
expect(createFunctionCallCountWithDeps, 2, reason: 'no dependency changed');
});
});
});
group('useLayoutEffect -', () {
testEffectHook(useLayoutEffect);
});
group('useImperativeHandle -', () {
group('updates `ref.current` to the return value of `createHandle()`', () {
ButtonElement? incrementButton;
ButtonElement? reRenderButtonRef1;
ButtonElement? reRenderButtonRef2;
late Ref noDepsRef;
late Ref emptyDepsRef;
late Ref depsRef;
late StateHook<int> count;
setUpAll(() {
final mountNode = DivElement();
final NoDepsComponent = react.forwardRef2((props, ref) {
count = useState(0);
useImperativeHandle(
ref,
() => {'increment': () => count.setWithUpdater((prev) => prev + 1)},
);
return react.div({'ref': ref}, count.value);
});
final EmptyDepsComponent = react.forwardRef2((props, ref) {
final count = useState(0);
useImperativeHandle(ref, () => count.value, []);
return react.Fragment({}, [
react.div({'ref': ref}, count.value),
react.button({
'ref': (ref) => reRenderButtonRef1 = ref as ButtonElement?,
'onClick': (_) => count.setWithUpdater((prev) => prev + 1),
}, []),
]);
});
final DepsComponent = react.forwardRef2((props, ref) {
final count = useState(0);
useImperativeHandle(ref, () => count.value, [count.value]);
return react.Fragment({}, [
react.div({'ref': ref}, count.value),
react.button({
'ref': (ref) => reRenderButtonRef2 = ref as ButtonElement?,
'onClick': (_) => count.setWithUpdater((prev) => prev + 1),
}, []),
]);
});
final NullRefComponent = react.registerFunctionComponent((props) {
final count = useState(0);
final someRefThatIsNotSet = props['someRefThatIsNotSet'];
useImperativeHandle(someRefThatIsNotSet, () => count.value, [count.value]);
return react.Fragment({}, [
react.div({'ref': someRefThatIsNotSet}, count.value),
react.button({
'ref': (ref) => reRenderButtonRef2 = ref as ButtonElement?,
'onClick': (_) => count.setWithUpdater((prev) => prev + 1),
}, []),
]);
});
expect(() => react_dom.render(NullRefComponent({}, []), mountNode), returnsNormally,
reason: 'Hook should not throw if the ref is null');
react_dom.unmountComponentAtNode(mountNode);
final UseImperativeHandleTest = react.registerFunctionComponent((props) {
noDepsRef = useRef();
emptyDepsRef = useRef();
depsRef = useRef();
return react.Fragment({}, [
NoDepsComponent({'ref': noDepsRef}, []),
react.button({
'ref': (ref) => incrementButton = ref as ButtonElement?,
'onClick': (_) => noDepsRef.current['increment'](),
}, [
'+'
]),
EmptyDepsComponent({'ref': emptyDepsRef}),
DepsComponent({'ref': depsRef}),
]);
});
react_dom.render(UseImperativeHandleTest({}), mountNode);
});
test('(with no dependency list)', () {
expect(noDepsRef.current, isA<Map>(), reason: 'useImperativeHandle overrides the existing ref.current value');
expect(noDepsRef.current['increment'], isA<Function>());
expect(count.value, 0);
react_test_utils.Simulate.click(incrementButton);
expect(count.value, 1);
});
test('(with empty dependency list)', () {
expect(emptyDepsRef.current, isA<int>(),
reason: 'useImperativeHandle overrides the existing ref.current value');
expect(emptyDepsRef.current, 0);
react_test_utils.Simulate.click(reRenderButtonRef1);
expect(emptyDepsRef.current, 0,
reason: 'current value does not update because count.value is not in dependency list');
});
test('(with non-empty dependency list)', () {
expect(depsRef.current, isA<int>(), reason: 'useImperativeHandle overrides the existing ref.current value');
expect(depsRef.current, 0);
react_test_utils.Simulate.click(reRenderButtonRef2);
expect(depsRef.current, 1, reason: 'current value updates because count.value is in dependency list');
});
});
group('works with all types of consumer refs -', () {
const customRefValue = {'Am I a ref?': true};
final factory = react.forwardRef2((props, ref) {
useImperativeHandle(ref, () => customRefValue);
return react.div({});
});
refTests<Map>(factory, verifyRefValue: (refValue) {
expect(refValue, same(customRefValue));
});
});
});
group('useDebugValue -', () {
late StateHook<bool> isOnline1;
late StateHook<bool> isOnline2;
StateHook<bool> useFriendStatus() {
final isOnline = useState(false);
useDebugValue(isOnline.value ? 'Online' : 'Not Online');
return isOnline;
}
StateHook<bool> useFriendStatusWithFormatFunction() {
final isOnline = useState(true);
useDebugValue<bool>(isOnline.value, (value) => value ? 'Online' : 'Not Online');
return isOnline;
}
setUpAll(() {
final mountNode = DivElement();
final UseDebugValueTest1 = react.registerFunctionComponent((props) {
isOnline1 = useFriendStatus();
return react.li({
'style': {'color': isOnline1.value ? 'green' : 'black'}
}, [
props['friend']['name']
]);
});
final UseDebugValueTest2 = react.registerFunctionComponent((props) {
isOnline2 = useFriendStatusWithFormatFunction();
return react.li({
'style': {'color': isOnline2.value ? 'green' : 'black'}
}, [
props['friend']['name']
]);
});
react_dom.render(
react.Fragment({}, [
UseDebugValueTest1({
'key': 'friend1',
'friend': {'id': 1, 'name': 'user 1'},
}),
UseDebugValueTest2({
'key': 'friend2',
'friend': {'id': 2, 'name': 'user 2'},
}),
]),
mountNode);
});
test('does not cause errors when used', () {
expect(isOnline1.value, false);
expect(useFriendStatus, isA<Function>());
});
test('with format function does not cause errors when used', () {
expect(isOnline2.value, true);
expect(useFriendStatusWithFormatFunction, isA<Function>());
});
});
});
}
ReactDartComponentFactoryProxy2 ContextProviderWrapper = react.registerComponent2(() => _ContextProviderWrapper());
class _ContextProviderWrapper extends react.Component2 {
@override
get initialState {
return {'counter': 1};
}
increment() {
setState({'counter': state['counter'] + 1});
}
@override
render() {
return react.div({}, [
(props['contextToUse'] as Context)
.Provider({'value': props['mode'] == 'increment' ? state['counter'] : props['value']}, props['children'])
]);
}
}
void testEffectHook(void Function(dynamic Function() sideEffect, [List<Object?>? dependencies]) effectHook) {
late ReactDartFunctionComponentFactoryProxy UseEffectTest;
ButtonElement? countButtonRef;
DivElement? countRef;
late DivElement mountNode;
late int useEffectCallCount;
late int useEffectCleanupCallCount;
late int useEffectWithDepsCallCount;
late int useEffectCleanupWithDepsCallCount;
late int useEffectWithDepsCallCount2;
late int useEffectCleanupWithDepsCallCount2;
late int useEffectWithEmptyDepsCallCount;
late int useEffectCleanupWithEmptyDepsCallCount;
setUpAll(() {
mountNode = DivElement();
useEffectCallCount = 0;
useEffectCleanupCallCount = 0;
useEffectWithDepsCallCount = 0;
useEffectCleanupWithDepsCallCount = 0;
useEffectWithDepsCallCount2 = 0;
useEffectCleanupWithDepsCallCount2 = 0;
useEffectWithEmptyDepsCallCount = 0;
useEffectCleanupWithEmptyDepsCallCount = 0;
UseEffectTest = react.registerFunctionComponent((props) {
final count = useState(0);
final countDown = useState(0);
effectHook(() {
useEffectCallCount++;
return () {
useEffectCleanupCallCount++;
};
});
effectHook(() {
useEffectWithDepsCallCount++;
return () {
useEffectCleanupWithDepsCallCount++;
};
}, [count.value]);
effectHook(() {
useEffectWithDepsCallCount2++;
return () {
useEffectCleanupWithDepsCallCount2++;
};
}, [countDown.value]);
effectHook(() {
useEffectWithEmptyDepsCallCount++;
return () {
useEffectCleanupWithEmptyDepsCallCount++;
};
}, []);
return react.div({}, [
react.div({
'ref': (ref) {
countRef = ref as DivElement?;
},
}, [
count.value
]),
react.button({
'onClick': (_) {
count.set(count.value + 1);
},
'ref': (ref) {
countButtonRef = ref as ButtonElement?;
},
}, [
'+'
]),
]);
});
react_dom.render(UseEffectTest({}), mountNode);
});
test('side effect (no dependency list) is called after the first render', () {
expect(countRef!.text, '0');
expect(useEffectCallCount, 1);
expect(useEffectCleanupCallCount, 0, reason: 'component has not been unmounted or re-rendered');
});
test('side effect (with dependency list) is called after the first render', () {
expect(useEffectWithDepsCallCount, 1);
expect(useEffectCleanupWithDepsCallCount, 0, reason: 'component has not been unmounted or re-rendered');
expect(useEffectWithDepsCallCount2, 1);
expect(useEffectCleanupWithDepsCallCount2, 0, reason: 'component has not been unmounted or re-rendered');
});