New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FED-202 Return jsUndefined
instead of null
when children prop is empty
#350
Conversation
Security InsightsNo security relevant content was detected by automated scans. Action Items
Questions or Comments? Reach out on Slack: #support-infosec. |
test/factory/js_factory_test.js
Outdated
if(this.props.children !== undefined) { | ||
throw Error('children prop must be undefined'); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if there's a more straightforward way to test that props.children
is specifically undefined
and not null
- open to suggestions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought maybe we could change this expectation:
-expect(getChildren(instance), shouldAlwaysBeList ? [] : isNull);
+// We want to make sure this is undefined and not null, so we have to use `same`
+// which uses `identical`, since in Dart null and undefined are otherwise interchangeable.
+expect(getChildren(instance), shouldAlwaysBeList ? [] : same(jsUndefined));
But it looks like that doesn't even work, since the identical
check gets compiled to ==
instead of ===
, and identical(jsNull, jsUndefined)
is true
😕.
So, checking it from the JS seems like the way to go to me!
Related to my comment above, we could also potentially check whether it's undefined on the ReactElement without rendering it, if the rendering/throwing causes issues or complicates things.
window.hasUndefinedChildren = (reactElement) => reactElement.props.children === undefined;
expect(hasUndefinedChildren(JsNoChildren({})), isTrue);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh that's a lot nicer! Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
test/factory/js_factory_test.js
Outdated
if(this.props.children !== undefined) { | ||
throw Error('children prop must be undefined'); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought maybe we could change this expectation:
-expect(getChildren(instance), shouldAlwaysBeList ? [] : isNull);
+// We want to make sure this is undefined and not null, so we have to use `same`
+// which uses `identical`, since in Dart null and undefined are otherwise interchangeable.
+expect(getChildren(instance), shouldAlwaysBeList ? [] : same(jsUndefined));
But it looks like that doesn't even work, since the identical
check gets compiled to ==
instead of ===
, and identical(jsNull, jsUndefined)
is true
😕.
So, checking it from the JS seems like the way to go to me!
Related to my comment above, we could also potentially check whether it's undefined on the ReactElement without rendering it, if the rendering/throwing causes issues or complicates things.
window.hasUndefinedChildren = (reactElement) => reactElement.props.children === undefined;
expect(hasUndefinedChildren(JsNoChildren({})), isTrue);
test/factory/js_factory_test.dart
Outdated
@@ -54,6 +55,10 @@ main() { | |||
expect(JsFooFunction.type, equals(_JsFooFunction)); | |||
}); | |||
}); | |||
|
|||
test('with no children returns JS undefined', () { | |||
expect(() => react_dom.render(JsNoChildren({}), Element.div()), returnsNormally); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to make sure this throws when we'd expect it to and can't result in a false positive, could we add an additional check here?
expect(() => react_dom.render(JsNoChildren({}, jsNull), Element.div()), throws,
reason: 'test setup check: throws when children are not undefined');
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+10
@Workiva/release-management-p
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 from RM
Some JS components, like
Tab
expect no children to be passed to them.However, when rendered from Dart, Tab's propTypes warn about children not being supported, even when no children are provided:
This is because, when there are no children,
ReactJsComponentFactoryProxy
passesnull
into createElement, and theunsupportedProp
validation checks warns when the prop is undefined.Changes
jsUndefined
instead ofnull
fromgenerateChildren
when the children prop is emptyQA Instructions