From aafa070dece5de190b83c4cc56791d53f04a0acd Mon Sep 17 00:00:00 2001 From: Jeremy Gonzalez Date: Tue, 29 Aug 2017 12:09:54 -0700 Subject: [PATCH] feat(router): redirect with token parameters allow redirect to contain token parameters for router config this closes feature request #483 --- src/navigation-plan.js | 16 +++++++++++----- test/navigation-plan.spec.js | 30 +++++++++++++++++++++++++++++- test/router.spec.js | 2 +- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/navigation-plan.js b/src/navigation-plan.js index 31f06ee0..ee5a7106 100644 --- a/src/navigation-plan.js +++ b/src/navigation-plan.js @@ -26,12 +26,18 @@ export function _buildNavigationPlan(instruction: NavigationInstruction, forceLi let plan = {}; if ('redirect' in config) { - let redirectLocation = _resolveUrl(config.redirect, getInstructionBaseUrl(instruction)); - if (instruction.queryString) { - redirectLocation += '?' + instruction.queryString; - } + let router = instruction.router; + return router._createNavigationInstruction(config.redirect) + .then(newInstruction => { + let params = Object.keys(newInstruction.params).length ? instruction.params : {} + let redirectLocation = router.generate(newInstruction.config.name, params, instruction.options); + + if (instruction.queryString) { + redirectLocation += '?' + instruction.queryString; + } - return Promise.reject(new Redirect(redirectLocation)); + return Promise.reject(new Redirect(redirectLocation)); + }) } if (prev) { diff --git a/test/navigation-plan.spec.js b/test/navigation-plan.spec.js index 6f81660d..aa0f8a29 100644 --- a/test/navigation-plan.spec.js +++ b/test/navigation-plan.spec.js @@ -2,11 +2,16 @@ import {BuildNavigationPlanStep} from '../src/navigation-plan'; import {NavigationInstruction} from '../src/navigation-instruction'; import {Redirect} from '../src/navigation-commands'; import {createPipelineState} from './test-util'; +import {AppRouter} from '../src/app-router'; +import {MockHistory} from './router.spec.js'; +import {Container} from 'aurelia-dependency-injection'; +import {PipelineProvider} from '../src/pipeline-provider'; describe('NavigationPlanStep', () => { let step; let state; let redirectInstruction; + let redirectSecondInstruction; let firstInstruction; let sameAsFirstInstruction; let secondInstruction; @@ -18,7 +23,16 @@ describe('NavigationPlanStep', () => { redirectInstruction = new NavigationInstruction({ fragment: 'first', queryString: 'q=1', - config: { redirect: 'second' } + config: { redirect: 'second' }, + router: new AppRouter(new Container(), new MockHistory(), new PipelineProvider(new Container())) + }); + + redirectSecondInstruction = new NavigationInstruction({ + fragment: 'first/10', + queryString: 'q=1', + params: {id: 10}, + config: { name:'first', route: 'first/:id', redirect: 'second/:id' }, + router: new AppRouter(new Container(), new MockHistory(), new PipelineProvider(new Container())) }); firstInstruction = new NavigationInstruction({ @@ -42,6 +56,8 @@ describe('NavigationPlanStep', () => { }); it('cancels on redirect configs', (done) => { + redirectInstruction.router.addRoute({route: 'first', name: 'frist', redirect: 'second' }); + redirectInstruction.router.addRoute({route: 'second', name: 'second', redirect: 'second' }); step.run(redirectInstruction, state.next) .then(e => { expect(state.rejection).toBeTruthy(); @@ -51,6 +67,18 @@ describe('NavigationPlanStep', () => { }); }); + it('redirect to routes with parameters', (done) => { + redirectSecondInstruction.router.addRoute({ name:'second', route: 'second/:id', moduleId: './second' }); + redirectSecondInstruction.router.addRoute({ name:'first', route: 'first/:id', redirect: 'second' }); + step.run(redirectSecondInstruction, state.next) + .then(e => { + expect(state.rejection).toBeTruthy(); + expect(e instanceof Redirect).toBe(true); + expect(e.url).toBe('#/second/10?q=1'); + done(); + }); + }); + describe('generates navigation plans', () => { it('with no prev step', (done) => { step.run(firstInstruction, state.next) diff --git a/test/router.spec.js b/test/router.spec.js index 793de308..90ebb1e3 100644 --- a/test/router.spec.js +++ b/test/router.spec.js @@ -5,7 +5,7 @@ import {PipelineProvider} from '../src/pipeline-provider'; let absoluteRoot = 'http://aurelia.io/docs/'; -class MockHistory extends History { +export class MockHistory extends History { activate() {} deactivate() {} navigate() {}