Skip to content

Commit

Permalink
fix: Allow falsely children in route config (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense authored and taion committed Apr 30, 2019
1 parent 890784a commit 98ac8a9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
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

0 comments on commit 98ac8a9

Please sign in to comment.