-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathreact_lazy_test.dart
125 lines (104 loc) · 4.01 KB
/
react_lazy_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
@TestOn('browser')
@JS()
library react.react_lazy_test;
import 'dart:async';
import 'dart:js_util';
import 'package:js/js.dart';
import 'package:react/hooks.dart';
import 'package:react/react.dart' as react;
import 'package:react/react_test_utils.dart' as rtu;
import 'package:react/react_client/component_factory.dart';
import 'package:react/react_client/react_interop.dart';
import 'package:test/test.dart';
import 'factory/common_factory_tests.dart';
main() {
group('lazy', () {
// Event more lazy behavior is tested in `react_suspense_test.dart`
test('correctly throws errors from within load function to the closest error boundary', () async {
const errorString = 'intentional future error';
final errors = [];
final errorCompleter = Completer();
final ThrowingLazyTest = react.lazy(() async {
throw Exception(errorString);
});
onError(error, info) {
errors.add([error, info]);
errorCompleter.complete();
}
expect(
() => rtu.renderIntoDocument(_ErrorBoundary(
{'onComponentDidCatch': onError}, react.Suspense({'fallback': 'Loading...'}, ThrowingLazyTest({})))),
returnsNormally);
await expectLater(errorCompleter.future, completes);
expect(errors, hasLength(1));
expect(errors.first.first, isA<Exception>().having((e) => e.toString(), 'message', contains(errorString)));
expect(errors.first.last, isA<ReactErrorInfo>());
});
group('Dart component', () {
final LazyTest = react.lazy(() async => react.forwardRef2((props, ref) {
useImperativeHandle(ref, () => TestImperativeHandle());
props['onDartRender']?.call(props);
return react.div({...props});
}));
group('- common factory behavior -', () {
commonFactoryTests(
LazyTest,
// ignore: invalid_use_of_protected_member
dartComponentVersion: ReactDartComponentVersion.component2,
renderWrapper: (child) => react.Suspense({'fallback': 'Loading...'}, child),
);
});
group('- dom event handler wrapping -', () {
domEventHandlerWrappingTests(LazyTest);
});
group('- refs -', () {
refTests<TestImperativeHandle>(LazyTest, verifyRefValue: (ref) {
expect(ref, isA<TestImperativeHandle>());
});
});
});
group('JS component', () {
final LazyJsTest = react.lazy(() async => ReactJsComponentFactoryProxy(_JsFoo));
group('- common factory behavior -', () {
commonFactoryTests(
LazyJsTest,
// ignore: invalid_use_of_protected_member
dartComponentVersion: ReactDartComponentVersion.component2,
// This isn't a Dart component, but it's detected as one by tests due to the factory's dartComponentVersion
isNonDartComponentWithDartWrapper: true,
renderWrapper: (child) => react.Suspense({'fallback': 'Loading...'}, child),
);
});
group('- dom event handler wrapping -', () {
domEventHandlerWrappingTests(
LazyJsTest,
// This isn't a Dart component, but it's detected as one by tests due to the factory's dartComponentVersion
isNonDartComponentWithDartWrapper: true,
);
});
group('- refs -', () {
refTests<ReactComponent>(LazyJsTest, verifyRefValue: (ref) {
expect(getProperty(ref as Object, 'constructor'), same(_JsFoo));
});
});
});
});
}
class TestImperativeHandle {}
@JS()
external ReactClass get _JsFoo;
final _ErrorBoundary = react.registerComponent2(() => _ErrorBoundaryComponent(), skipMethods: []);
class _ErrorBoundaryComponent extends react.Component2 {
@override
get initialState => {'hasError': false};
@override
getDerivedStateFromError(dynamic error) => {'hasError': true};
@override
componentDidCatch(dynamic error, ReactErrorInfo info) {
props['onComponentDidCatch'](error, info);
}
@override
render() {
return (state['hasError'] as bool) ? null : props['children'];
}
}