Skip to content

Commit

Permalink
Work with react-stand-in
Browse files Browse the repository at this point in the history
  • Loading branch information
taion committed Dec 29, 2017
1 parent a577720 commit 3a95ab8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-proxy": "^3.0.0-alpha.1",
"react-stand-in": "^4.0.0-beta.6",
"react-test-renderer": "^16.2.0",
"rimraf": "^2.6.2"
}
Expand Down
15 changes: 15 additions & 0 deletions src/makeRouteConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ export default function makeRouteConfig(node) {
return React.Children.toArray(node)
.filter(React.isValidElement)
.map(({ type: Type, props: { children, ...props } }) => {
if (__DEV__) {
if (Type.prototype.constructor !== Type) {
// Unwrap proxies from react-proxy. This isn't strictly necessary.
// eslint-disable-next-line no-param-reassign
Type = Type.prototype.constructor;
} else if (
// eslint-disable-next-line no-underscore-dangle
Type.__reactstandin__getCurrent
) {
// Unwrap proxies from react-stand-in.
// eslint-disable-next-line no-param-reassign
Type = Object.getPrototypeOf(Type);
}
}

const route = new Type(props);

if (children) {
Expand Down
52 changes: 29 additions & 23 deletions test/makeRouteConfig.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import createProxy from 'react-proxy';

import makeRouteConfig from '../src/makeRouteConfig';
import Redirect from '../src/Redirect';
Expand Down Expand Up @@ -150,33 +149,40 @@ describe('makeRouteConfig', () => {
]);
});

it('should work with proxies', () => {
const ProxiedRedirect = createProxy(Redirect).get();
const redirect = makeRouteConfig(
<ProxiedRedirect from="/foo" to="/bar" />,
)[0];
['react-proxy', 'react-stand-in'].forEach(packageName => {
it(`should work with proxies from ${packageName}`, () => {
// eslint-disable-next-line global-require, import/no-dynamic-require
const createProxy = require(packageName).default;

expect(redirect.path).toBe('/foo');
expect(redirect.to).toBe('/bar');
expect(redirect.render).toBeTruthy();
const ProxiedRedirect = createProxy(Redirect).get();
const redirect = makeRouteConfig(
<ProxiedRedirect from="/foo" to="/bar" />,
)[0];

let redirectException;
expect(Object.getPrototypeOf(redirect)).toBe(Redirect.prototype);

try {
redirect.render({
match: {
router: {
matcher: {
format: to => to,
expect(redirect.path).toBe('/foo');
expect(redirect.to).toBe('/bar');
expect(redirect.render).toBeTruthy();

let redirectException;

try {
redirect.render({
match: {
router: {
matcher: {
format: to => to,
},
},
},
},
});
} catch (e) {
redirectException = e;
}
});
} catch (e) {
redirectException = e;
}

expect(redirectException).toBeInstanceOf(RedirectException);
expect(redirectException.location).toBe('/bar');
expect(redirectException).toBeInstanceOf(RedirectException);
expect(redirectException.location).toBe('/bar');
});
});
});
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3795,6 +3795,10 @@ react-redux@^5.0.6:
loose-envify "^1.1.0"
prop-types "^15.5.10"

react-stand-in@^4.0.0-beta.6:
version "4.0.0-beta.6"
resolved "https://registry.yarnpkg.com/react-stand-in/-/react-stand-in-4.0.0-beta.6.tgz#6dd28cc11665a1a51e4dbbb248bc3e1c77e93add"

react-static-container@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/react-static-container/-/react-static-container-1.0.2.tgz#30a4f7548860be1d55d5eb4c20645b835c2bdf34"
Expand Down

0 comments on commit 3a95ab8

Please sign in to comment.