-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathreact_client_test.dart
264 lines (225 loc) · 7.74 KB
/
react_client_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
// ignore_for_file: deprecated_member_use_from_same_package
@TestOn('browser')
@JS()
library react_test_utils_test;
import 'dart:html' show DivElement;
import 'package:js/js.dart';
import 'package:react/react.dart';
import 'package:test/test.dart';
import 'package:react/react.dart' as react;
import 'package:react/react_client.dart';
import 'package:react/react_client/component_factory.dart';
import 'package:react/react_client/react_interop.dart' show React;
import 'package:react/react_client/js_interop_helpers.dart';
import 'package:react/react_dom.dart' as react_dom;
import 'package:react/src/react_client/event_prop_key_to_event_factory.dart';
main() {
group('unconvertJsProps', () {
const testChildren = ['child1', 'child2'];
const testStyle = <String, dynamic>{'background': 'white'};
test('returns props for a composite JS component ReactElement', () {
final instance = testJsComponentFactory({
'jsProp': 'js',
'style': testStyle,
}, testChildren);
expect(
unconvertJsProps(instance),
equals({
'jsProp': 'js',
'style': testStyle,
'children': testChildren,
}),
);
expect(unconvertJsProps(instance)['style'], isA<Map<String, dynamic>>());
});
test('throws when a Dart component (Component2) is passed in', () {
final instance = DartComponent2({
'jsProp': 'js',
'style': testStyle,
}, testChildren);
expect(() => unconvertJsProps(instance), throwsArgumentError);
});
test('throws when a Dart component is passed in', () {
final instance = DartComponent({
'jsProp': 'js',
'style': testStyle,
}, testChildren);
expect(() => unconvertJsProps(instance), throwsArgumentError);
});
test('returns props for a composite JS ReactComponent', () {
final mountNode = DivElement();
final renderedInstance = react_dom.render(
testJsComponentFactory({
'jsProp': 'js',
'style': testStyle,
}, testChildren),
mountNode);
expect(
unconvertJsProps(renderedInstance),
equals({
'jsProp': 'js',
'style': testStyle,
'children': testChildren,
}),
);
});
test('returns props for a composite JS ReactComponent, even when the props change', () {
final mountNode = DivElement();
var renderedInstance = react_dom.render(
testJsComponentFactory({
'jsProp': 'js',
'style': testStyle,
}, testChildren),
mountNode);
expect(
unconvertJsProps(renderedInstance),
equals({
'jsProp': 'js',
'style': testStyle,
'children': testChildren,
}),
);
renderedInstance = react_dom.render(
testJsComponentFactory({
'jsProp': 'other js',
'style': testStyle,
}, testChildren),
mountNode);
expect(
unconvertJsProps(renderedInstance),
equals({
'jsProp': 'other js',
'style': testStyle,
'children': testChildren,
}));
});
test('returns props for a DOM component ReactElement', () {
final instance = react.div({
'domProp': 'dom',
'style': testStyle,
}, testChildren);
expect(
unconvertJsProps(instance),
equals({
'domProp': 'dom',
'style': testStyle,
'children': testChildren,
}));
});
group('should unconvert JS event handlers', () {
Function createHandler(eventKey) => (_) {
print(eventKey);
};
late Map<String, Function> originalHandlers;
late Map props;
setUp(() {
originalHandlers = {};
props = {};
for (final key in knownEventKeys) {
props[key] = originalHandlers[key] = createHandler(key);
}
});
test('for a DOM element', () {
final component = react.div(props);
final jsProps = unconvertJsProps(component);
for (final key in knownEventKeys) {
expect(jsProps[key], isNotNull, reason: 'JS event handler prop should not be null');
expect(jsProps[key], anyOf(same(originalHandlers[key]), same(allowInterop(originalHandlers[key]!))),
reason: 'JS event handler should be the original or original wrapped in allowInterop');
}
});
test(', except for a JS composite component (handlers should already be unconverted)', () {
final component = testJsComponentFactory(props);
final jsProps = unconvertJsProps(component);
for (final key in knownEventKeys) {
expect(jsProps[key], isNotNull, reason: 'JS event handler prop should not be null');
expect(jsProps[key], same(allowInterop(originalHandlers[key]!)),
reason: 'JS event handler prop was unexpectedly modified');
}
});
});
});
group('registerComponent', () {
test('throws with printed error', () {
expect(() => react.registerComponent(() => ThrowsInDefaultPropsComponent()), throwsStateError);
expect(() {
try {
react.registerComponent(() => ThrowsInDefaultPropsComponent());
} catch (_) {}
}, prints(contains('Error when registering Component:')));
});
});
group('registerComponent2', () {
test('throws with specific error when defaultProps throws', () {
expect(() => react.registerComponent2(() => ThrowsInDefaultPropsComponent2()), throwsStateError);
expect(() {
try {
react.registerComponent2(() => ThrowsInDefaultPropsComponent2());
} catch (_) {}
}, prints(contains('Error when registering Component2 when getting defaultProps')));
});
test('throws with specific error when propTypes throws', () {
expect(() => react.registerComponent2(() => ThrowsInPropTypesComponent2()), throwsStateError);
expect(() {
try {
react.registerComponent2(() => ThrowsInPropTypesComponent2());
} catch (_) {}
}, prints(contains('Error when registering Component2 when getting propTypes')));
}, tags: 'no-dart2js');
test('throws with generic error when something else throws', () {
expect(() => react.registerComponent2(() => throw StateError('bad component')), throwsStateError);
expect(() {
try {
react.registerComponent2(() => throw StateError('bad component'));
} catch (_) {}
}, prints(contains('Error when registering Component2:')));
});
});
}
@JS()
external Function compositeComponent();
/// A factory for a JS composite component, for use in testing.
final testJsComponentFactory = (() {
final type = compositeComponent();
return ([props = const {}, children]) {
return React.createElement(type, jsifyAndAllowInterop(props), listifyChildren(children));
};
})();
class ThrowsInDefaultPropsComponent extends Component {
@override
getDefaultProps() => throw StateError('bad default props');
@override
render() {
return null;
}
}
class ThrowsInDefaultPropsComponent2 extends Component2 {
@override
get defaultProps => throw StateError('bad default props');
@override
render() {
return null;
}
}
class ThrowsInPropTypesComponent2 extends Component2 {
@override
get propTypes => throw StateError('bad prop types');
@override
render() {
return null;
}
}
class DartComponent2Component extends Component2 {
@override
render() {
return null;
}
}
final DartComponent2 = react.registerComponent2(() => DartComponent2Component());
class DartComponentComponent extends Component {
@override
render() {
return null;
}
}
final DartComponent = react.registerComponent(() => DartComponentComponent()) as ReactDartComponentFactoryProxy;