@@ -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) {
0 commit comments