Skip to content

Commit

Permalink
Add test: should not collect parameters from previous routes
Browse files Browse the repository at this point in the history
  • Loading branch information
frenzzy committed Oct 20, 2016
1 parent a26b222 commit 656062b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.

### [v2.0.0] - [unreleased]

- Preserve `context.params` values from the parent route ([#57](/~https://github.com/kriasoft/universal-router/pull/57))
- Throws an error if no route found ([#62](/~https://github.com/kriasoft/universal-router/pull/62))
- Remove obsolete `context.end()` method ([#60](/~https://github.com/kriasoft/universal-router/pull/60))
- Remove obsolete `match` alias for `resolve` function ([#59](/~https://github.com/kriasoft/universal-router/pull/59))
Expand Down
3 changes: 1 addition & 2 deletions src/matchPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ function matchPathBase(end, routePath, urlPath, parentParams) {
return null;
}

const path = m[0];
const params = Object.create(null);
if (parentParams) {
Object.assign(params, parentParams);
}

const path = m[0];

for (let i = 1; i < m.length; i += 1) {
params[regexp.keys[i - 1].name] = decodeParam(m[i]);
}
Expand Down
45 changes: 43 additions & 2 deletions test/resolve.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ describe('resolve(routes, { path, ...context })', () => {
];
const result = await resolve(routes, { path: '/a/b' });
expect(action.calledOnce).to.be.true;
expect(action.args[0][0]).to.have.deep.property('params.one', 'a');
expect(action.args[0][0]).to.have.deep.property('params.two', 'b');
expect(action.args[0][0]).to.have.property('params').that.deep.equals({ one: 'a', two: 'b' });
expect(result).to.be.true;
});

Expand Down Expand Up @@ -156,6 +155,48 @@ describe('resolve(routes, { path, ...context })', () => {
expect(result).to.be.true;
});

it('should not collect parameters from previous routes', async () => {
const action1 = sinon.spy(() => undefined);
const action2 = sinon.spy(() => null);
const action3 = sinon.spy(() => true);
const routes = [
{
path: '/:one',
action: action1,
children: [
{
path: '/:two',
action: action1,
},
],
},
{
path: '/:three',
action: action2,
children: [
{
path: '/:four',
action: action2,
},
{
path: '/:five',
action: action3,
},
],
},
];
const result = await resolve(routes, { path: '/a/b' });
expect(action1.calledTwice).to.be.true;
expect(action1.args[0][0]).to.have.property('params').that.deep.equals({ one: 'a' });
expect(action1.args[1][0]).to.have.property('params').that.deep.equals({ one: 'a', two: 'b' });
expect(action2.calledTwice).to.be.true;
expect(action2.args[0][0]).to.have.property('params').that.deep.equals({ three: 'a' });
expect(action2.args[1][0]).to.have.property('params').that.deep.equals({ three: 'a', four: 'b' });
expect(action3.calledOnce).to.be.true;
expect(action3.args[0][0]).to.have.property('params').that.deep.equals({ three: 'a', five: 'b' });
expect(result).to.be.true;
});

it('should support next() across multiple routes', async () => {
const log = [];
const routes = [
Expand Down

0 comments on commit 656062b

Please sign in to comment.