Skip to content

Commit

Permalink
Allow disabling matching stem routes
Browse files Browse the repository at this point in the history
  • Loading branch information
taion committed Jul 13, 2018
1 parent fa6d6f7 commit 17d5c5d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 23 deletions.
8 changes: 6 additions & 2 deletions src/Matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import pathToRegexp from 'path-to-regexp';
import warning from 'warning';

export default class Matcher {
constructor(routeConfig) {
constructor(routeConfig, { matchStemRoutes = true } = {}) {
this.routeConfig = routeConfig;

this.matchStemRoutes = matchStemRoutes;

// Overly-aggressive deduplication of packages can lead to the wrong
// version of path-to-regexp getting bundled. This is a common enough
// failure mode that it's worthwhile to add a dev-only warning here.
Expand Down Expand Up @@ -87,7 +89,9 @@ export default class Matcher {
}

if (!remaining) {
return [{ index, params }];
if (this.matchStemRoutes || !children) {
return [{ index, params }];
}
}
}

Expand Down
78 changes: 57 additions & 21 deletions test/Matcher.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,82 @@ import Matcher from '../src/Matcher';

describe('Matcher', () => {
describe('route hierarchies', () => {
const matcher = new Matcher([
{
path: 'foo',
children: [
function createMatcher(options) {
return new Matcher(
[
{
children: [{}],
path: 'foo',
children: [
{
children: [{}],
},
{
path: 'bar',
},
],
},
{
path: 'bar',
children: [
{
path: 'baz',
},
],
},
],
},
{
path: 'bar',
children: [
{
path: 'baz',
path: 'foo/baz',
},
],
},
{
path: 'foo/baz',
},
]);
options,
);
}

[
['pathless children', '/foo', [0, 0, 0]],
['nested matching', '/foo/bar', [0, 1]],
['non-leaf routes', '/bar', [1]],
['route fallthrough', '/foo/baz', [2]],
].forEach(([scenario, pathname, expectedRouteIndices]) => {
it(`should support ${scenario}`, () => {
describe(scenario, () => {
it('should be supported', () => {
expect(
createMatcher().match({
pathname,
}),
).toMatchObject({
routeIndices: expectedRouteIndices,
});
});

it('should be supported with matchStemRoutes disabled', () => {
expect(
createMatcher({ matchStemRoutes: false }).match({
pathname,
}),
).toMatchObject({
routeIndices: expectedRouteIndices,
});
});
});
});

describe('stem routes', () => {
it('should be supported by default', () => {
expect(
matcher.match({
pathname,
createMatcher().match({
pathname: '/bar',
}),
).toMatchObject({
routeIndices: expectedRouteIndices,
routeIndices: [1],
});
});

it('should not be supported with matchStemRoutes disabled', () => {
expect(
createMatcher({ matchStemRoutes: false }).match({
pathname: '/bar',
}),
).toBeNull();
});
});
});

Expand Down

0 comments on commit 17d5c5d

Please sign in to comment.