Skip to content

Commit 468aacd

Browse files
Fix / reorganize tests, deprecate RTU
1 parent 02068ca commit 468aacd

File tree

73 files changed

+667
-705
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+667
-705
lines changed

.editorconfig

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# top-most EditorConfig file
2-
root = true
3-
41
[*.dart]
52
max_line_length = 120
63

build.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
targets:
22
$default:
33
builders:
4+
test_html_builder:
5+
options:
6+
templates:
7+
"test/js_builds/templates/js_dev.html":
8+
- "test/js_builds/js_dev_test.dart"
9+
"test/js_builds/templates/js_dev_with_addons.html":
10+
- "test/js_builds/js_dev_with_addons_test.dart"
11+
"test/js_builds/templates/js_prod_combined.html":
12+
- "test/js_builds/js_prod_combined_test.dart"
13+
"test/js_builds/templates/js_prod.html":
14+
- "test/js_builds/js_prod_test.dart"
15+
"test/factory/_factory_test_template.html":
16+
- "test/factory/**_test.dart"
17+
"test/lifecycle/_react_lifecycle_test_template.html":
18+
- "test/lifecycle/**_test.dart"
19+
"test/react_client/_react_client_test_template.html":
20+
- "test/react_client/**_test.dart"
21+
"test/_react_test_template.html":
22+
- "test/*_test.dart"
423
# mockito's builder is expensive and is not needed until this package is
524
# migrated to null-safety. At that point, it should be scoped only to
625
# relevant files.

lib/react_test_utils.dart

+27-28
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// Copyright (c) 2016, the Clean project authors. Please see the AUTHORS file
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
4-
4+
// ignore_for_file: deprecated_member_use_from_same_package
5+
@Deprecated('Use the react_testing_library package instead.')
56
@JS()
67
library react.test_utils;
78

8-
import 'dart:js_util' show getProperty;
9-
109
import 'package:js/js.dart';
1110
import 'package:react/react_client.dart';
1211
import 'package:react/react_client/js_interop_helpers.dart';
1312
import 'package:react/react_client/react_interop.dart';
1413
import 'package:react/src/react_test_utils/simulate_wrappers.dart' as sw;
14+
import 'package:react/src/react_test_utils/internal_test_utils.dart' as itu;
1515

1616
// Notes
1717
// ---------------------------------------------------------------------------
@@ -36,8 +36,10 @@ import 'package:react/src/react_test_utils/simulate_wrappers.dart' as sw;
3636
///
3737
/// * For DOM components, this with return the String corresponding to its tagName ('div', 'a', etc.).
3838
/// * For custom composite components React.createClass()-based components, this will return the [ReactClass].
39-
dynamic getComponentTypeV2(ReactComponentFactoryProxy componentFactory) => componentFactory.type;
39+
@Deprecated('Use the react_testing_library package instead.')
40+
final getComponentTypeV2 = itu.getComponentTypeV2;
4041

42+
@Deprecated('Use the react_testing_library package instead.')
4143
typedef bool ComponentTestFunction(/* [1] */ component);
4244

4345
dynamic _jsifyEventData(Map eventData) => jsifyAndAllowInterop(eventData ?? const {});
@@ -51,6 +53,7 @@ dynamic _jsifyEventData(Map eventData) => jsifyAndAllowInterop(eventData ?? cons
5153
///
5254
/// This should include all events documented at:
5355
/// http://facebook.github.io/react/docs/events.html
56+
@Deprecated('Use the UserEvent or fireEvent utilities from the react_testing_library package instead.')
5457
class Simulate {
5558
static void animationEnd(/* [1] */ node, [Map eventData]) =>
5659
sw.Simulate.animationEnd(node, _jsifyEventData(eventData));
@@ -120,6 +123,7 @@ class Simulate {
120123
///
121124
/// Included in Dart for completeness
122125
@JS('React.addons.TestUtils.findAllInRenderedTree')
126+
@Deprecated('Use the react_testing_library package instead.')
123127
external List<dynamic> findAllInRenderedTree(
124128
/* [1] */ tree,
125129
ComponentTestFunction test);
@@ -128,6 +132,7 @@ external List<dynamic> findAllInRenderedTree(
128132
/// result, and returns that one result, or throws exception if there is
129133
/// any other number of matches besides one.
130134
@JS('React.addons.TestUtils.findRenderedDOMComponentWithClass')
135+
@Deprecated('Use the react_testing_library package instead.')
131136
external dynamic /* [1] */ findRenderedDOMComponentWithClass(
132137
/* [1] */ tree,
133138
String className);
@@ -136,63 +141,51 @@ external dynamic /* [1] */ findRenderedDOMComponentWithClass(
136141
/// and returns that one result, or throws exception if there is any other
137142
/// number of matches besides one.
138143
@JS('React.addons.TestUtils.findRenderedDOMComponentWithTag')
144+
@Deprecated('Use the react_testing_library package instead.')
139145
external dynamic /* [1] */ findRenderedDOMComponentWithTag(
140146
/* [1] */ tree,
141147
String tag);
142148

143149
@JS('React.addons.TestUtils.findRenderedComponentWithType')
150+
@Deprecated('Use the react_testing_library package instead.')
144151
external dynamic /* [1] */ _findRenderedComponentWithType(
145152
/* [1] */ tree,
146153
dynamic type);
147154

148155
/// Same as [scryRenderedComponentsWithTypeV2] but expects there to be one result
149156
/// and returns that one result, or throws exception if there is any other
150157
/// number of matches besides one.
158+
@Deprecated('Use the react_testing_library package instead.')
151159
/* [1] */ findRenderedComponentWithTypeV2(
152160
/* [1] */ tree,
153161
ReactComponentFactoryProxy componentFactory) {
154162
return _findRenderedComponentWithType(tree, getComponentTypeV2(componentFactory));
155163
}
156164

157-
@JS('React.addons.TestUtils.isCompositeComponent')
158-
external bool _isCompositeComponent(/* [1] */ instance);
159-
160165
/// Returns true if element is a composite component.
161166
/// (created with React.createClass()).
162-
bool isCompositeComponent(/* [1] */ instance) {
163-
return _isCompositeComponent(instance)
164-
// Workaround for DOM components being detected as composite: https://github.com/facebook/react/pull/3839
165-
&&
166-
getProperty(instance, 'tagName') == null;
167-
}
168-
169-
@JS('React.addons.TestUtils.isCompositeComponentWithType')
170-
external bool _isCompositeComponentWithType(/* [1] */ instance, dynamic type);
167+
@Deprecated('Use the react_testing_library package instead.')
168+
final isCompositeComponent = itu.isCompositeComponent;
171169

172170
/// Returns `true` if instance is a custom composite component created using `React.createClass()`
173171
/// that is of the [ReactComponentFactoryProxy.type] of the provided [componentFactory].
174-
bool isCompositeComponentWithTypeV2(
175-
/* [1] */ instance,
176-
ReactComponentFactoryProxy componentFactory) {
177-
return _isCompositeComponentWithType(instance, getComponentTypeV2(componentFactory));
178-
}
172+
@Deprecated('Use the react_testing_library package instead.')
173+
final isCompositeComponentWithTypeV2 = itu.isCompositeComponentWithTypeV2;
179174

180175
/// Returns true if instance is a DOM component (such as a <div> or <span>).
181176
@JS('React.addons.TestUtils.isDOMComponent')
182-
external bool isDOMComponent(/* [1] */ instance);
177+
@Deprecated('Use the react_testing_library package instead.')
178+
final isDOMComponent = itu.isDOMComponent;
183179

184180
/// Returns true if [object] is a valid React component.
185181
@JS('React.addons.TestUtils.isElement')
182+
@Deprecated('Use the react_testing_library package instead.')
186183
external bool isElement(dynamic object);
187184

188-
@JS('React.addons.TestUtils.isElementOfType')
189-
external bool _isElementOfType(dynamic element, dynamic componentClass);
190-
191185
/// Returns `true` if [element] is a [ReactElement]
192186
/// that is of the [ReactComponentFactoryProxy.type] of the provided [componentFactory].
193-
bool isElementOfTypeV2(dynamic element, ReactComponentFactoryProxy componentFactory) {
194-
return _isElementOfType(element, getComponentTypeV2(componentFactory));
195-
}
187+
@Deprecated('Use the react_testing_library package instead.')
188+
final isElementOfTypeV2 = itu.isElementOfTypeV2;
196189

197190
@JS('React.addons.TestUtils.scryRenderedComponentsWithType')
198191
external List<dynamic> /* [1] */ _scryRenderedComponentsWithType(
@@ -201,6 +194,7 @@ external List<dynamic> /* [1] */ _scryRenderedComponentsWithType(
201194

202195
/// Finds all instances within the provided [tree]
203196
/// that are of the [ReactComponentFactoryProxy.type] of the provided [componentFactory].
197+
@Deprecated('Use the react_testing_library package instead.')
204198
List<dynamic> /* [1] */ scryRenderedComponentsWithTypeV2(
205199
/* [1] */ tree,
206200
ReactComponentFactoryProxy componentFactory) {
@@ -211,6 +205,7 @@ List<dynamic> /* [1] */ scryRenderedComponentsWithTypeV2(
211205

212206
/// Finds all instances of components in the rendered tree that are DOM
213207
/// components with the class name matching className.
208+
@Deprecated('Use the react_testing_library package instead.')
214209
external List<dynamic> scryRenderedDOMComponentsWithClass(
215210
/* [1] */ tree,
216211
String className);
@@ -219,19 +214,22 @@ external List<dynamic> scryRenderedDOMComponentsWithClass(
219214

220215
/// Finds all instances of components in the rendered tree that are DOM
221216
/// components with the tag name matching tagName.
217+
@Deprecated('Use the react_testing_library package instead.')
222218
external List<dynamic> scryRenderedDOMComponentsWithTag(
223219
/* [1] */ tree,
224220
String tagName);
225221

226222
/// Render a Component into a detached DOM node in the document.
227223
@JS('React.addons.TestUtils.renderIntoDocument')
224+
@Deprecated('Use the render() utility from the react_testing_library package instead.')
228225
external /* [1] */ renderIntoDocument(ReactElement instance);
229226

230227
/// Pass a mocked component module to this method to augment it with useful
231228
/// methods that allow it to be used as a dummy React component. Instead of
232229
/// rendering as usual, the component will become a simple <div> (or other tag
233230
/// if mockTagName is provided) containing any provided children.
234231
@JS('React.addons.TestUtils.mockComponent')
232+
@Deprecated('Use the react_testing_library package instead.')
235233
external ReactClass mockComponent(ReactClass componentClass, String mockTagName);
236234

237235
/// Returns a ReactShallowRenderer instance
@@ -255,6 +253,7 @@ external ReactShallowRenderer createRenderer();
255253
/// See react_with_addons.js#ReactShallowRenderer
256254
@JS()
257255
@anonymous
256+
@Deprecated('Use the react_testing_library package instead.')
258257
class ReactShallowRenderer {
259258
/// Get the rendered output. [render] must be called first
260259
external ReactElement getRenderOutput();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
@JS()
2+
library react.src.react_test_utils.internal_test_utils;
3+
4+
import 'dart:js_util' show getProperty;
5+
6+
import 'package:js/js.dart';
7+
import 'package:react/react_client.dart';
8+
import 'package:react/react_client/react_interop.dart';
9+
10+
/// Returns the [ReactComponentFactoryProxy.type] of a given [componentFactory].
11+
///
12+
/// * For DOM components, this with return the String corresponding to its tagName ('div', 'a', etc.).
13+
/// * For custom composite components React.createClass()-based components, this will return the [ReactClass].
14+
dynamic getComponentTypeV2(ReactComponentFactoryProxy componentFactory) => componentFactory.type;
15+
16+
@JS('React.addons.TestUtils.isCompositeComponent')
17+
external bool _isCompositeComponent(/* [1] */ instance);
18+
19+
/// Returns true if element is a composite component.
20+
/// (created with React.createClass()).
21+
bool isCompositeComponent(/* [1] */ instance) {
22+
return _isCompositeComponent(instance)
23+
// Workaround for DOM components being detected as composite: https://github.com/facebook/react/pull/3839
24+
&&
25+
getProperty(instance, 'tagName') == null;
26+
}
27+
28+
@JS('React.addons.TestUtils.isCompositeComponentWithType')
29+
external bool _isCompositeComponentWithType(/* [1] */ instance, dynamic type);
30+
31+
/// Returns `true` if instance is a custom composite component created using `React.createClass()`
32+
/// that is of the [ReactComponentFactoryProxy.type] of the provided [componentFactory].
33+
bool isCompositeComponentWithTypeV2(
34+
/* [1] */ instance,
35+
ReactComponentFactoryProxy componentFactory) {
36+
return _isCompositeComponentWithType(instance, getComponentTypeV2(componentFactory));
37+
}
38+
39+
/// Returns true if instance is a DOM component (such as a <div> or <span>).
40+
@JS('React.addons.TestUtils.isDOMComponent')
41+
external bool isDOMComponent(/* [1] */ instance);
42+
43+
/// Returns true if [object] is a valid React component.
44+
@JS('React.addons.TestUtils.isElement')
45+
external bool isElement(dynamic object);
46+
47+
@JS('React.addons.TestUtils.isElementOfType')
48+
external bool _isElementOfType(dynamic element, dynamic componentClass);
49+
50+
/// Returns `true` if [element] is a [ReactElement]
51+
/// that is of the [ReactComponentFactoryProxy.type] of the provided [componentFactory].
52+
bool isElementOfTypeV2(dynamic element, ReactComponentFactoryProxy componentFactory) {
53+
return _isElementOfType(element, getComponentTypeV2(componentFactory));
54+
}
55+
56+
/// Render a Component into a detached DOM node in the document.
57+
@JS('React.addons.TestUtils.renderIntoDocument')
58+
external ReactComponent renderIntoDocument(ReactElement instance);

pubspec.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,14 @@ dev_dependencies:
1515
dependency_validator: ">=2.0.0 <4.0.0"
1616
matcher: ^0.12.5
1717
mockito: ">=4.1.1 <6.0.0"
18+
react_testing_library: ^2.0.0
1819
test: ^1.6.5
20+
test_html_builder: ">=2.2.2 <4.0.0"
21+
22+
23+
dependency_overrides:
24+
react_testing_library:
25+
git:
26+
url: https://github.com/Workiva/react_testing_library
27+
ref: r18
28+

test/_react_test_template.html

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<!-- The "testName" placeholder will be replaced with a unique name for
5+
each test. -->
6+
<title>{{testName}} Test</title>
7+
8+
<!-- Load custom assets needed by the test. -->
9+
<script src="packages/react/react_with_addons.js"></script>
10+
<script src="packages/react/react_dom.js"></script>
11+
<script src="packages/react_testing_library/js/react-testing-library.js"></script>
12+
13+
<!-- Every template must include this placeholder. -->
14+
{{testScript}}
15+
<!-- It will be replaced by the builder with the required <link> tag:
16+
<link rel="x-dart-test" href="...">
17+
-->
18+
19+
<!-- Every template must include the test runner script. -->
20+
<script src="packages/test/dart.js"></script>
21+
</head>
22+
</html>
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<!-- The "testName" placeholder will be replaced with a unique name for
5+
each test. -->
6+
<title>{{testName}} Test</title>
7+
8+
<!-- Load custom assets needed by the test. -->
9+
<script src="packages/react/react_with_addons.js"></script>
10+
<script src="packages/react/react_dom.js"></script>
11+
<script src="packages/react_testing_library/js/react-testing-library.js"></script>
12+
13+
<script>
14+
window._JsFoo = class JsFooComponent extends React.Component {
15+
render() {
16+
return React.createElement("div", this.props, this.props.children);
17+
}
18+
};
19+
20+
window._JsFooFunction = React.forwardRef((props, ref) => (
21+
React.createElement("div", {...props, ref: ref})
22+
));
23+
</script>
24+
25+
<!-- Every template must include this placeholder. -->
26+
{{testScript}}
27+
<!-- It will be replaced by the builder with the required <link> tag:
28+
<link rel="x-dart-test" href="...">
29+
-->
30+
31+
<!-- Every template must include the test runner script. -->
32+
<script src="packages/test/dart.js"></script>
33+
</head>
34+
</html>

0 commit comments

Comments
 (0)