Skip to content

Commit ab80e79

Browse files
Merge pull request #98 from Workiva/release_over_react_test_2.9.2
RM-72281 Release over_react_test 2.9.2
2 parents 757c975 + 7cba8f1 commit ab80e79

File tree

5 files changed

+111
-31
lines changed

5 files changed

+111
-31
lines changed

lib/src/over_react_test/common_component_util.dart

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,10 @@ void commonComponentTests(BuilderOnlyUiFactory factory, {
8686
bool ignoreDomProps = true,
8787
bool shouldTestRequiredProps = true,
8888
@Deprecated('This flag is not needed as the test will auto detect the version')
89-
bool isComponent2 = false,
89+
bool isComponent2,
9090
dynamic childrenFactory()
9191
}) {
9292
childrenFactory ??= _defaultChildrenFactory;
93-
isComponent2 = ReactDartComponentVersion.fromType((factory()()).type) == '2' || isComponent2;
9493

9594
if (shouldTestPropForwarding) {
9695
_testPropForwarding(
@@ -112,7 +111,7 @@ void commonComponentTests(BuilderOnlyUiFactory factory, {
112111
testClassNameOverrides(factory, childrenFactory);
113112
}
114113
if (shouldTestRequiredProps) {
115-
testRequiredProps(factory, childrenFactory, isComponent2);
114+
testRequiredProps(factory, childrenFactory);
116115
}
117116
}
118117

@@ -427,13 +426,23 @@ void testClassNameOverrides(BuilderOnlyUiFactory factory, dynamic childrenFactor
427426
/// > Typically not consumed standalone. Use [commonComponentTests] instead.
428427
///
429428
/// __Note__: All required props must be provided by [factory].
430-
void testRequiredProps(BuilderOnlyUiFactory factory, dynamic childrenFactory(),
431-
bool isComponent2) {
429+
void testRequiredProps(BuilderOnlyUiFactory factory, dynamic childrenFactory()) {
430+
bool isComponent2;
431+
432432
var keyToErrorMessage = {};
433433
var nullableProps = <String>[];
434434
var requiredProps = <String>[];
435435

436436
setUp(() {
437+
// This can't go in a setUpAll since it would be called before consumer setUps.
438+
//
439+
// ignore: invalid_use_of_protected_member
440+
final version = ReactDartComponentVersion.fromType(
441+
(factory()(childrenFactory())).type,
442+
);
443+
// ignore: invalid_use_of_protected_member
444+
isComponent2 = version == ReactDartComponentVersion.component2;
445+
437446
var jacket = mount(factory()(childrenFactory()), autoTearDown: false);
438447
var consumedProps = (jacket.getDartInstance() as component_base.UiComponent).consumedProps;
439448
jacket.unmount();
@@ -451,37 +460,37 @@ void testRequiredProps(BuilderOnlyUiFactory factory, dynamic childrenFactory(),
451460
}
452461
});
453462

454-
if (!isComponent2) {
455-
test('throws when the required prop is not set or is null', () {
456-
for (var propKey in requiredProps) {
457-
final reactComponentFactory = factory()
458-
.componentFactory as ReactDartComponentFactoryProxy; // ignore: avoid_as
459-
460-
// Props that are defined in the default props map will never not be set.
461-
if (!reactComponentFactory.defaultProps.containsKey(propKey)) {
462-
var badRenderer = () =>
463-
render((factory()
464-
..remove(propKey)
465-
)(childrenFactory()));
466-
467-
expect(badRenderer,
468-
throwsPropError_Required(propKey, keyToErrorMessage[propKey]),
469-
reason: '$propKey is not set');
470-
}
463+
test('throws (component1) or logs the correct errors (component2) when the required prop is not set or is null', () {
464+
void component1RequiredPropsTest(){
465+
for (var propKey in requiredProps) {
466+
final reactComponentFactory = factory()
467+
.componentFactory as ReactDartComponentFactoryProxy; // ignore: avoid_as
471468

472-
var propsToAdd = {propKey: null};
469+
// Props that are defined in the default props map will never not be set.
470+
if (!reactComponentFactory.defaultProps.containsKey(propKey)) {
473471
var badRenderer = () =>
474472
render((factory()
475-
..addAll(propsToAdd)
473+
..remove(propKey)
476474
)(childrenFactory()));
477475

478476
expect(badRenderer,
479477
throwsPropError_Required(propKey, keyToErrorMessage[propKey]),
480-
reason: '$propKey is set to null');
478+
reason: '$propKey is not set');
481479
}
482-
});
483-
} else {
484-
test('logs the correct errors when the required prop is not set or is null', () {
480+
481+
var propsToAdd = {propKey: null};
482+
var badRenderer = () =>
483+
render((factory()
484+
..addAll(propsToAdd)
485+
)(childrenFactory()));
486+
487+
expect(badRenderer,
488+
throwsPropError_Required(propKey, keyToErrorMessage[propKey]),
489+
reason: '$propKey is set to null');
490+
}
491+
}
492+
493+
void component2RequiredPropsTest() {
485494
PropTypes.resetWarningCache();
486495

487496
List<String> consoleErrors = [];
@@ -534,8 +543,10 @@ void testRequiredProps(BuilderOnlyUiFactory factory, dynamic childrenFactory(),
534543
consoleErrors = [];
535544
PropTypes.resetWarningCache();
536545
}
537-
});
538-
}
546+
}
547+
548+
isComponent2 ? component2RequiredPropsTest() : component1RequiredPropsTest();
549+
});
539550

540551
test('nullable props', () {
541552
if (!isComponent2) {

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: over_react_test
2-
version: 2.9.1
2+
version: 2.9.2
33
description: A library for testing OverReact components
44
author: Workiva UI Platform Team <[email protected]>
55
homepage: https://github.com/Workiva/over_react_test/

test/over_react_test/common_component_util_test.dart

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import 'package:over_react/over_react.dart';
1516
import 'package:over_react_test/src/over_react_test/props_meta.dart';
1617
import 'package:test/test.dart';
1718
import 'package:over_react_test/over_react_test.dart';
@@ -80,5 +81,71 @@ main() {
8081
);
8182
});
8283
});
84+
85+
// This causes issues when consumers access properties on variables
86+
// initialized in setUp blocks within `factory`.
87+
group('does not call `factory` directly within the consuming group', () {
88+
void sharedTest(
89+
BuilderOnlyUiFactory factory, {
90+
List Function(PropsMetaCollection) getUnconsumedPropKeys,
91+
}) {
92+
var wasFactoryCalled = false;
93+
94+
// These needs to be before declared before commonComponentTests is called,
95+
// or these tests may fail when they shouldn't.
96+
97+
// Test both setUpAll/setUp since they have different timings relative
98+
// to commonComponent test setup blocks.
99+
//
100+
// setUp is the important one, but might as well test both while we're
101+
// here!
102+
103+
setUpAll(() {
104+
expect(wasFactoryCalled, isFalse,
105+
reason: 'factory arg was called within group, '
106+
'before consumer setUpAll blocks are called');
107+
});
108+
109+
int setUpCallCount = 0;
110+
setUp(() {
111+
// Only do this the first time, since it gets called before every
112+
// test inside commonComponentTests.
113+
if (setUpCallCount == 0) {
114+
expect(wasFactoryCalled, isFalse,
115+
reason: 'factory arg was called within group, '
116+
'before consumer setUp blocks are called');
117+
}
118+
setUpCallCount++;
119+
});
120+
121+
commonComponentTests(() {
122+
wasFactoryCalled = true;
123+
return factory();
124+
}, getUnconsumedPropKeys: getUnconsumedPropKeys);
125+
}
126+
127+
group('when passed a UiComponent', () {
128+
// todo create a new component for this
129+
sharedTest(() => TestCommonRequired()
130+
..bar = true
131+
..foobar = true);
132+
});
133+
134+
group('when passed a UiComponent2', () {
135+
group('(old boilerplate)', () {
136+
// todo create a new component for this
137+
sharedTest(() => TestCommonRequired2()
138+
..bar = true
139+
..foobar = true);
140+
});
141+
142+
group('(new boilerplate)', () {
143+
// todo create a new component for this
144+
sharedTest(new_boilerplate.TestCommonForwarding, getUnconsumedPropKeys: (meta) => [
145+
...meta.forMixin(new_boilerplate.ShouldBeForwardedProps).keys,
146+
]);
147+
});
148+
});
149+
});
83150
});
84151
}

test/over_react_test/utils/test_common_component_required_props.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class TestCommonRequiredComponent extends UiComponent<TestCommonRequiredProps> {
5151
render() {
5252
return (Dom.div()
5353
..addProps(copyUnconsumedDomProps())
54+
..className = forwardingClassNameBuilder().toClassName()
5455
)(props.children);
5556
}
5657
}

test/over_react_test/utils/test_common_component_required_props_commponent2.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class TestCommonRequiredComponent2 extends
4949
render() {
5050
return (Dom.div()
5151
..modifyProps(addUnconsumedDomProps)
52+
..className = forwardingClassNameBuilder().toClassName()
5253
)(props.children);
5354
}
5455
}

0 commit comments

Comments
 (0)