Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Commit

Permalink
Test memory router, ensure Route onRoute is not required (#232)
Browse files Browse the repository at this point in the history
* Test memory router, ensure Route onRoute is not required

* lint
  • Loading branch information
lhorie authored Feb 20, 2019
1 parent a3df907 commit 3e9d434
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
27 changes: 27 additions & 0 deletions src/__tests__/memoryrouter.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/** Copyright (c) 2018 Uber Technologies, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

/* eslint-env browser */
import test from 'tape-cup';
import React from 'react';
import ReactDOM from 'react-dom';

import {MemoryRouter, Route} from '../browser.js';

test('works in browser', t => {
const root = document.createElement('div');

const el = (
<MemoryRouter initialEntries={['/test']}>
<Route path="/test" render={() => <div>Test</div>} />
</MemoryRouter>
);
ReactDOM.render(el, root);
t.ok(/Test/.test(root.innerHTML), 'matches');
t.end();
});
24 changes: 24 additions & 0 deletions src/__tests__/memoryrouter.node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/** Copyright (c) 2018 Uber Technologies, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import test from 'tape-cup';
import React from 'react';

import {renderToString as render} from 'react-dom/server';

import {MemoryRouter, Route} from '../server.js';

test('works in server', t => {
const el = (
<MemoryRouter initialEntries={['/test']}>
<Route path="/test" render={() => <div>Test</div>} />
</MemoryRouter>
);
t.ok(/Test/.test(render(el)), 'matches');
t.end();
});
14 changes: 8 additions & 6 deletions src/modules/Route.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ function Route(props: PropsType, context: ContextType) {
children={(routeProps: ContextRouterType) => {
const {match} = routeProps;
if (match && match.isExact) {
context.onRoute({
page: match.path,
title: trackingId || match.path,
params: match.params,
});
if (typeof context.onRoute === 'function') {
context.onRoute({
page: match.path,
title: trackingId || match.path,
params: match.params,
});
}
}

if (component)
Expand All @@ -60,7 +62,7 @@ function Route(props: PropsType, context: ContextType) {
}

Route.contextTypes = {
onRoute: PropTypes.func.isRequired,
onRoute: PropTypes.func,
};

Route.displayName = 'FusionRoute';
Expand Down

0 comments on commit 3e9d434

Please sign in to comment.