Skip to content
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

fix: Allow falsely children in route config #357

Merged
merged 6 commits into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/makeRouteConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import React from 'react';

function buildRouteConfig(node, routeConfig) {
React.Children.forEach(node, child => {
// Falsy children get coerced to null. We check for this instead of
// implicit falsiness because we don't want to allow empty strings or 0.
if (child === null) {
return;
}

invariant(
React.isValidElement(child),
'`%s` is not a valid React element',
Expand Down
2 changes: 2 additions & 0 deletions test/__snapshots__/makeRouteConfig.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`makeRouteConfig should error on non-element children 1`] = `"\`,\` is not a valid React element"`;

exports[`makeRouteConfig should error on other falsy children 1`] = `"\`0\` is not a valid React element"`;
22 changes: 22 additions & 0 deletions test/makeRouteConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,28 @@ describe('makeRouteConfig', () => {
}).toThrowErrorMatchingSnapshot();
});

it('should allow empty children', () => {
expect(() => {
makeRouteConfig(
<Route path="/" Component={AppPage}>
{false}
<Route path="foo" Component={FooPage} />
</Route>,
);
}).not.toThrow();
});

it('should error on other falsy children', () => {
expect(() => {
makeRouteConfig(
<Route path="/" Component={AppPage}>
{0}
<Route path="foo" Component={FooPage} />
</Route>,
);
}).toThrowErrorMatchingSnapshot();
});

['react-proxy', 'react-stand-in'].forEach(packageName => {
it(`should work with proxies from ${packageName}`, () => {
// eslint-disable-next-line global-require, import/no-dynamic-require
Expand Down