Skip to content

Commit 282914b

Browse files
committed
Test updates
1 parent f0d8b3a commit 282914b

File tree

1 file changed

+78
-32
lines changed

1 file changed

+78
-32
lines changed

test/lifecycle_test.dart

Lines changed: 78 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,24 @@ void main() {
140140
]));
141141
});
142142

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+
143161
test('receives updated context with correct lifecycle calls', () {
144162
_LifecycleTestWithContext component;
145163

@@ -549,6 +567,38 @@ external List getUpdatingSetStateLifeCycleCalls();
549567
@JS()
550568
external List getNonUpdatingSetStateLifeCycleCalls();
551569

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+
552602
ReactDartComponentFactoryProxy SetStateTest = react.registerComponent(() => new _SetStateTest());
553603
class _SetStateTest extends react.Component {
554604
@override
@@ -640,52 +690,48 @@ class _DefaultPropsTest extends react.Component {
640690
render() => false;
641691
}
642692

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+
643710
ReactDartComponentFactoryProxy ContextWrapper = react.registerComponent(() => new _ContextWrapper());
644-
class _ContextWrapper extends react.Component {
711+
class _ContextWrapper extends react.Component with LifecycleTestHelper {
645712
@override
646-
Iterable<String> get childContextKeys => const ['foo'];
713+
Iterable<String> get childContextKeys => const ['foo', 'extraContext'];
647714

648715
@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+
}
652723

653724
dynamic render() => react.div({}, props['children']);
654725
}
655726

656727
ReactDartComponentFactoryProxy LifecycleTestWithContext = react.registerComponent(() => new _LifecycleTestWithContext());
657728
class _LifecycleTestWithContext extends _LifecycleTest {
658729
@override
659-
Iterable<String> get contextKeys => const ['foo'];
730+
Iterable<String> get contextKeys => const ['foo']; // only listening to one context key
660731
}
661732

662733
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 {
689735
void componentWillMount() => lifecycleCall('componentWillMount');
690736
void componentDidMount() => lifecycleCall('componentDidMount');
691737
void componentWillUnmount() => lifecycleCall('componentWillUnmount');

0 commit comments

Comments
 (0)