Skip to content

Commit

Permalink
Fix bug when path is "/" and regex is "/.*"
Browse files Browse the repository at this point in the history
  • Loading branch information
jdlehman committed Oct 30, 2015
1 parent f63b9b8 commit 0b2448e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 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.

8 changes: 4 additions & 4 deletions src/components/Switcher.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {Component} from 'react';
import {ensureTrailingSlash} from 'helpers';
import {removeTrailingSlash} from 'helpers';

export default class Switcher extends Component {
static displayName = 'Switcher';
Expand Down Expand Up @@ -80,13 +80,13 @@ export default class Switcher extends Component {

getSwitch = (path) => {
var children = [].concat(this.props.children);
var consistentPath = ensureTrailingSlash(path);
var consistentPath = removeTrailingSlash(path);
return children.filter(child => {
var childPaths = [].concat(child.props.path).map(childPath => {
return ensureTrailingSlash(this.props.basePath + childPath);
return `${removeTrailingSlash(this.props.basePath + childPath)}/?`;
});
var regex = new RegExp(`^${childPaths.join('|')}$`);
return consistentPath.match(regex);
return regex.test(consistentPath);
})[0] || null;
}

Expand Down
8 changes: 6 additions & 2 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export function ensureTrailingSlash(path) {
return path.slice(-1) !== '/' ? `${path}/` : path;
export function removeTrailingSlash(path) {
if (path === '/') {
return path;
} else {
return path.slice(-1) !== '/' ? path : path.slice(0, -1);
}
}
7 changes: 7 additions & 0 deletions test/components/Switcher_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,13 @@ describe('Switcher', function() {
assert.equal(node.innerHTML, 'Default Handler');
});

it('default handle can match /', function() {
window.location.hash = '/';
this.switcher.handleRouteChange();
var node = ReactDOM.findDOMNode(this.switcher);
assert.equal(node.innerHTML, 'Default Handler');
});

it('renders matching component', function() {
window.location.hash = '/home';
this.switcher.handleRouteChange();
Expand Down
22 changes: 14 additions & 8 deletions test/helpers_test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import {assert} from 'chai';
import {ensureTrailingSlash} from 'helpers';
import {removeTrailingSlash} from 'helpers';

describe('helpers', function() {
describe('ensureTrailingSlash', function() {
it('adds trailing slash to path without it', function() {
describe('removeTrailingSlash', function() {
it('removes trailing slash to path with it', function() {
var path = '/test/something/more';
var newPath = ensureTrailingSlash(path);
assert.equal(newPath, `${path}/`);
var newPath = removeTrailingSlash(`${path}/`);
assert.equal(newPath, path);
});

it('returns unmodified path if it does not have a trailing slash', function() {
var path = '/test/something/more';
var newPath = removeTrailingSlash(path);
assert.equal(newPath, path);
});

it('returns unmodified path if it already has trailing slash', function() {
var path = '/test/something/more/';
var newPath = ensureTrailingSlash(path);
it('returns unmodified path if path is /', function() {
var path = '/';
var newPath = removeTrailingSlash(path);
assert.equal(newPath, path);
});
});
Expand Down

0 comments on commit 0b2448e

Please sign in to comment.