Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(router): child recognizer and catchAllHandler #532

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,23 +276,21 @@ export class Router {
let caseSensitive = config.caseSensitive === true;
let state = this._recognizer.add({path: path, handler: config, caseSensitive: caseSensitive});

if (path) {
let settings = config.settings;
delete config.settings;
let withChild = JSON.parse(JSON.stringify(config));
config.settings = settings;
withChild.route = `${path}/*childRoute`;
withChild.hasChildRouter = true;
this._childRecognizer.add({
path: withChild.route,
handler: withChild,
caseSensitive: caseSensitive
});
let settings = config.settings;
delete config.settings;
let withChild = JSON.parse(JSON.stringify(config));
config.settings = settings;
withChild.route = `${path}/*childRoute`;
withChild.hasChildRouter = true;
this._childRecognizer.add({
path: withChild.route,
handler: withChild,
caseSensitive: caseSensitive
});

withChild.navModel = navModel;
withChild.settings = config.settings;
withChild.navigationStrategy = config.navigationStrategy;
}
withChild.navModel = navModel;
withChild.settings = config.settings;
withChild.navigationStrategy = config.navigationStrategy;

config.navModel = navModel;

Expand Down Expand Up @@ -398,6 +396,13 @@ export class Router {
let results = this._recognizer.recognize(url);
if (!results || !results.length) {
results = this._childRecognizer.recognize(url);
if (
results &&
(this.catchAllHandler || (this.parent && this._parentCatchAllHandler(this.parent)))
) {
results = Array.prototype.filter.call(results,
result => result.handler.route !== '/*childRoute');
}
}

let instructionInit = {
Expand Down
21 changes: 21 additions & 0 deletions test/navigation-instruction.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,25 @@ describe('NavigationInstruction', () => {
});
});
});

it('matches child with empty parent route', (done) => {
router.addRoute({
route: '',
moduleId: 'child'
});
const childRouter = router.createChild();
childRouter.addRoute({
name: 'child',
route: 'test',
moduleId: 'test'
});

router
._createNavigationInstruction('test')
.then(parentInstruction =>
childRouter._createNavigationInstruction('test', parentInstruction))
.then(childInstruction => expect(childInstruction.config.name).toBe('child'))
.catch(fail)
.then(done);
});
});
24 changes: 24 additions & 0 deletions test/router.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,30 @@ describe('the router', () => {
.then(x => expect(true).toBeFalsy('should have rejected', x))
.catch(reason => done());
});

it('should work when route is empty', done => {
router
.configure(config => {
config.unknownRouteConfig = 'test';
config.mapRoute({route: '', moduleId: './empty'});
})
.then(() => router._createNavigationInstruction('foo/123?bar=456'))
.then(instruction => expect(instruction.config.moduleId).toEqual('test'))
.catch(fail)
.then(done);
});

it(`should work when route is '/'`, done => {
router
.configure(config => {
config.unknownRouteConfig = 'test';
config.mapRoute({route: '/', moduleId: './empty'});
})
.then(() => router._createNavigationInstruction('foo/123?bar=456'))
.then(instruction => expect(instruction.config.moduleId).toEqual('test'))
.catch(fail)
.then(done);
});
});
});

Expand Down