@@ -140,6 +140,24 @@ void main() {
140
140
]));
141
141
});
142
142
143
+ test ('does not call getChildContext when childContextKeys is empty' , () {
144
+ var mountNode = new DivElement ();
145
+ var instance = react_dom.render (ContextWrapperWithoutKeys ({'foo' : false }, LifecycleTestWithContext ({})), mountNode);
146
+ _ContextWrapperWithoutKeys component = getDartComponent (instance);
147
+
148
+ expect (component.lifecycleCalls, isEmpty);
149
+ });
150
+
151
+ test ('calls getChildContext when childContextKeys exist' , () {
152
+ var mountNode = new DivElement ();
153
+ var instance = react_dom.render (ContextWrapper ({'foo' : false }, LifecycleTestWithContext ({})), mountNode);
154
+ _ContextWrapper component = getDartComponent (instance);
155
+
156
+ expect (component.lifecycleCalls, equals ([
157
+ matchCall ('getChildContext' ),
158
+ ]));
159
+ });
160
+
143
161
test ('receives updated context with correct lifecycle calls' , () {
144
162
_LifecycleTestWithContext component;
145
163
@@ -549,6 +567,38 @@ external List getUpdatingSetStateLifeCycleCalls();
549
567
@JS ()
550
568
external List getNonUpdatingSetStateLifeCycleCalls ();
551
569
570
+ /// A test helper to record lifecycle calls
571
+ abstract class LifecycleTestHelper {
572
+ Map context;
573
+ Map props;
574
+ Map state;
575
+
576
+ List lifecycleCalls = [];
577
+
578
+ dynamic lifecycleCall (String memberName, {List arguments: const [], defaultReturnValue ()}) {
579
+ lifecycleCalls.add ({
580
+ 'memberName' : memberName,
581
+ 'arguments' : arguments,
582
+ 'props' : props == null ? null : new Map .from (props),
583
+ 'state' : state == null ? null : new Map .from (state),
584
+ 'context' : new Map .from (context ?? const {}),
585
+ });
586
+
587
+ var lifecycleCallback = props == null ? null : props[memberName];
588
+ if (lifecycleCallback != null ) {
589
+ return Function .apply (lifecycleCallback, []
590
+ ..add (this )
591
+ ..addAll (arguments));
592
+ }
593
+
594
+ if (defaultReturnValue != null ) {
595
+ return defaultReturnValue ();
596
+ }
597
+
598
+ return null ;
599
+ }
600
+ }
601
+
552
602
ReactDartComponentFactoryProxy SetStateTest = react.registerComponent (() => new _SetStateTest ());
553
603
class _SetStateTest extends react.Component {
554
604
@override
@@ -640,52 +690,48 @@ class _DefaultPropsTest extends react.Component {
640
690
render () => false ;
641
691
}
642
692
693
+ ReactDartComponentFactoryProxy ContextWrapperWithoutKeys = react.registerComponent (() => new _ContextWrapperWithoutKeys ());
694
+ class _ContextWrapperWithoutKeys extends react.Component with LifecycleTestHelper {
695
+ @override
696
+ Iterable <String > get childContextKeys => const [];
697
+
698
+ @override
699
+ Map <String , dynamic > getChildContext () {
700
+ lifecycleCall ('getChildContext' );
701
+ return {
702
+ 'foo' : props['foo' ],
703
+ 'extraContext' : props['extraContext' ],
704
+ };
705
+ }
706
+
707
+ dynamic render () => react.div ({}, props['children' ]);
708
+ }
709
+
643
710
ReactDartComponentFactoryProxy ContextWrapper = react.registerComponent (() => new _ContextWrapper ());
644
- class _ContextWrapper extends react.Component {
711
+ class _ContextWrapper extends react.Component with LifecycleTestHelper {
645
712
@override
646
- Iterable <String > get childContextKeys => const ['foo' ];
713
+ Iterable <String > get childContextKeys => const ['foo' , 'extraContext' ];
647
714
648
715
@override
649
- Map <String , dynamic > getChildContext () => {
650
- 'foo' : props['foo' ]
651
- };
716
+ Map <String , dynamic > getChildContext () {
717
+ lifecycleCall ('getChildContext' );
718
+ return {
719
+ 'foo' : props['foo' ],
720
+ 'extraContext' : props['extraContext' ],
721
+ };
722
+ }
652
723
653
724
dynamic render () => react.div ({}, props['children' ]);
654
725
}
655
726
656
727
ReactDartComponentFactoryProxy LifecycleTestWithContext = react.registerComponent (() => new _LifecycleTestWithContext ());
657
728
class _LifecycleTestWithContext extends _LifecycleTest {
658
729
@override
659
- Iterable <String > get contextKeys => const ['foo' ];
730
+ Iterable <String > get contextKeys => const ['foo' ]; // only listening to one context key
660
731
}
661
732
662
733
ReactDartComponentFactoryProxy LifecycleTest = react.registerComponent (() => new _LifecycleTest ());
663
- class _LifecycleTest extends react.Component {
664
- List lifecycleCalls = [];
665
-
666
- dynamic lifecycleCall (String memberName, {List arguments: const [], defaultReturnValue ()}) {
667
- lifecycleCalls.add ({
668
- 'memberName' : memberName,
669
- 'arguments' : arguments,
670
- 'props' : props == null ? null : new Map .from (props),
671
- 'state' : state == null ? null : new Map .from (state),
672
- 'context' : new Map .from (context ?? const {}),
673
- });
674
-
675
- var lifecycleCallback = props == null ? null : props[memberName];
676
- if (lifecycleCallback != null ) {
677
- return Function .apply (lifecycleCallback, []
678
- ..add (this )
679
- ..addAll (arguments));
680
- }
681
-
682
- if (defaultReturnValue != null ) {
683
- return defaultReturnValue ();
684
- }
685
-
686
- return null ;
687
- }
688
-
734
+ class _LifecycleTest extends react.Component with LifecycleTestHelper {
689
735
void componentWillMount () => lifecycleCall ('componentWillMount' );
690
736
void componentDidMount () => lifecycleCall ('componentDidMount' );
691
737
void componentWillUnmount () => lifecycleCall ('componentWillUnmount' );
0 commit comments