Skip to content

Commit 07a48e8

Browse files
committed
fix: fix issue with building state and paths for non-empty root nodes
1 parent 1e8bda2 commit 07a48e8

2 files changed

Lines changed: 33 additions & 10 deletions

File tree

modules/RouteNode.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ export default class RouteNode {
128128
return filteredRoutes.length ? filteredRoutes[0] : undefined;
129129
};
130130
let segments = [];
131-
let names = routeName.split('.');
132-
let routes = this.children;
131+
let routes = this.parser ? [ this ] : this.children;
132+
let names = ( this.parser ? [''] : [] ).concat(routeName.split('.'));
133133

134134
let matched = names.every(name => {
135135
let segment = findSegmentByName(name, routes);

test/main.js

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
'use strict';
22

3-
var path = require('path');
4-
var pkg = require('../package.json');
5-
var RouteNode = require(path.join(__dirname, '..', pkg.main));
6-
var should = require('should');
7-
var omit = require('lodash.omit');
3+
import path from 'path';
4+
import pkg from '../package.json';
5+
import RouteNode from '../modules/RouteNode';
6+
import should from 'should';
7+
import omit from 'lodash.omit';
88

99
function withoutMeta(obj) {
1010
return omit(obj, '_meta');
1111
}
1212

13-
require('mocha');
14-
1513
describe('RouteNode', function () {
1614
it('should instanciate an empty RouteNode if no routes are specified in constructor', function () {
1715
var node = new RouteNode();
@@ -329,10 +327,35 @@ describe('RouteNode', function () {
329327
var node = new RouteNode('', '?a', [
330328
new RouteNode('route', '/path?b')
331329
]);
332-
withoutMeta(node.matchPath('/path?a=1&b=2')).should.eql({
330+
node.matchPath('/path?a=1&b=2').should.eql({
331+
_meta: {
332+
'': { a: 'query' },
333+
'route': { b: 'query' }
334+
},
333335
name: 'route',
334336
params: { a: '1', b: '2' }
335337
});
338+
339+
node.buildState('route', { b: '1' }).should.eql({
340+
_meta: {
341+
'': { a: 'query' },
342+
'route': { b: 'query' }
343+
},
344+
name: 'route',
345+
params: { b: '1' }
346+
});
347+
348+
node.buildState('route', { a: '1', b: '1' }).should.eql({
349+
_meta: {
350+
'': { a: 'query' },
351+
'route': { b: 'query' }
352+
},
353+
name: 'route',
354+
params: { a: '1', b: '1' }
355+
});
356+
357+
node.buildPath('route', { b: '2' }).should.equal('/path?b=2');
358+
node.buildPath('route', { a: '1', b: '2' }).should.equal('/path?a=1&b=2');
336359
});
337360
});
338361

0 commit comments

Comments
 (0)