Skip to content

Commit

Permalink
Fix isActive logic on undefined query values
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense authored and taion committed Jun 19, 2017
1 parent 2cf3da2 commit 816a911
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/Matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export default class Matcher {
return false;
}

// Require that a partial match is followed by a path separator.
// Require that a partial match be followed by a path separator.
const pathnameWithSeparator = pathname.slice(-1) !== '/' ?
`${pathname}/` : pathname;

Expand All @@ -159,8 +159,8 @@ export default class Matcher {
}

return Object.entries(query).every(([key, value]) => (
Object.prototype.hasOwnProperty.call(matchQuery, key) &&
isEqual(matchQuery[key], value)
Object.prototype.hasOwnProperty.call(matchQuery, key) ?
isEqual(matchQuery[key], value) : value === undefined
));
}
}
124 changes: 120 additions & 4 deletions test/Matcher.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('Matcher', () => {
let matcher;

beforeEach(() => {
const routeConfig = [
matcher = new Matcher([
{
path: 'foo',
children: [
Expand All @@ -30,9 +30,7 @@ describe('Matcher', () => {
{
path: 'foo/baz',
},
];

matcher = new Matcher(routeConfig);
]);
});

[
Expand Down Expand Up @@ -166,4 +164,122 @@ describe('Matcher', () => {
});
});
});

describe('#isActive', () => {
let matcher;

beforeEach(() => {
matcher = new Matcher([]);
});

describe('active locations', () => {
[
[
'path match',
{ pathname: '/foo/bar' },
{ pathname: '/foo/bar' },
],
[
'parent path match',
{ pathname: '/foo/bar' },
{ pathname: '/foo' },
],
[
'exact path match',
{ pathname: '/foo/bar' },
{ pathname: '/foo/bar' },
{ exact: true },
],
[
'null query match',
{ pathname: '/foo', query: { foo: 'bar' } },
{ pathname: '/foo' },
],
[
'empty query match',
{ pathname: '/foo', query: { foo: 'bar' } },
{ pathname: '/foo', query: {} },
],
[
'empty query match with explicit undefined',
{ pathname: '/foo', query: { foo: undefined } },
{ pathname: '/foo', query: {} },
],
[
'query match',
{ pathname: '/foo', query: { foo: 'bar' } },
{ pathname: '/foo', query: { foo: 'bar' } },
],
[
'query match with extraneous query item',
{ pathname: '/foo', query: { foo: 'bar', bar: 'foo' } },
{ pathname: '/foo', query: { foo: 'bar' } },
],
[
'absent query match with implicit undefined',
{ pathname: '/foo', query: {} },
{ pathname: '/foo', query: { foo: undefined } },
],
[
'absent query match with explicit undefined',
{ pathname: '/foo', query: { foo: undefined } },
{ pathname: '/foo', query: { foo: undefined } },
],
].forEach(([scenario, matchLocation, location, options]) => {
it(`should be active on ${scenario}`, () => {
expect(matcher.isActive(
{ location: matchLocation }, location, options,
)).toBe(true);
});
});
});

describe('inactive locations', () => {
[
[
'path mismatch',
{ pathname: '/bar' },
{ pathname: '/foo' },
],
[
'exact parent path match',
{ pathname: '/foo/bar' },
{ pathname: '/foo' },
{ exact: true },
],
[
'exact path mismatch',
{ pathname: '/bar' },
{ pathname: '/foo' },
{ exact: true },
],
[
'query mismatch',
{ pathname: '/foo', query: { foo: 'foo' } },
{ pathname: '/foo', query: { foo: 'bar' } },
],
[
'missing query item',
{ pathname: '/foo', query: {} },
{ pathname: '/foo', query: { foo: 'bar' } },
],
[
'expected query item explicitly undefined',
{ pathname: '/foo', query: { foo: undefined } },
{ pathname: '/foo', query: { foo: 'bar' } },
],
[
'expected undefined query item present',
{ pathname: '/foo', query: { foo: 'bar' } },
{ pathname: '/foo', query: { foo: undefined } },
],
].forEach(([scenario, matchLocation, location, options]) => {
it(`should not be active on ${scenario}`, () => {
expect(matcher.isActive(
{ location: matchLocation }, location, options,
)).toBe(false);
});
});
});
});
});

0 comments on commit 816a911

Please sign in to comment.