Skip to content

Commit

Permalink
Fix bug where multiple paths ending in the same pattern would cause w…
Browse files Browse the repository at this point in the history
…rong nodes to render.
  • Loading branch information
eightypop committed Mar 23, 2016
1 parent 76d002e commit 0398f56
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dist/switcheroo.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function formatPathRegex(basePath, path) {
}

export function createRegexFromPaths(paths) {
return new RegExp(`^${paths.join('|')}$`);
return new RegExp(`^(${paths.join('|')})$`);
}

export function getSwitch(path, {children, basePath}) {
Expand Down
33 changes: 33 additions & 0 deletions test/Switcher_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,39 @@ describe('Switcher', function() {
});
});

describe('with multiple paths', function() {
beforeEach(function() {
this.switcher = ReactDOM.render(
<Switcher>
<div path={['/', '/other']}>Home</div>
</Switcher>,
document.getElementById('app')
);
});
afterEach(function() {
ReactDOM.unmountComponentAtNode(document.getElementById('app'));
helpers.currentPath.restore();
});
it('renders correct element', function() {
sinon.stub(helpers, 'currentPath').returns('/other');
this.switcher.handleRouteChange();
var node = ReactDOM.findDOMNode(this.switcher);
assert.equal(node.innerHTML, 'Home');
});
it('renders correct element', function() {
sinon.stub(helpers, 'currentPath').returns('/');
this.switcher.handleRouteChange();
var node = ReactDOM.findDOMNode(this.switcher);
assert.equal(node.innerHTML, 'Home');
});
it('renders correct elements', function() {
sinon.stub(helpers, 'currentPath').returns('/otherThing');
this.switcher.handleRouteChange();
var node = ReactDOM.findDOMNode(this.switcher);
assert.equal(node, null);
});
});

describe('with default handler', function() {
beforeEach(function() {
this.switcher = ReactDOM.render(
Expand Down
2 changes: 1 addition & 1 deletion test/helpers_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ describe('helpers', function() {
it('joins paths and creates a regex', function() {
var paths = ['/one/b', '/two/a/b', '/three'];
var regex = createRegexFromPaths(paths);
assert.equal(`${regex}`, '/^\\/one\\/b|\\/two\\/a\\/b|\\/three$/');
assert.equal(`${regex}`, '/^(\\/one\\/b|\\/two\\/a\\/b|\\/three)$/');
assert(regex instanceof RegExp);
});
});
Expand Down

0 comments on commit 0398f56

Please sign in to comment.