Skip to content

Commit

Permalink
feat(router): redirect with token parameters
Browse files Browse the repository at this point in the history
allow redirect to contain token parameters for router config

this closes feature request #483
  • Loading branch information
jagonalez committed Aug 29, 2017
1 parent a398f55 commit aafa070
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/navigation-plan.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
30 changes: 29 additions & 1 deletion test/navigation-plan.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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({
Expand All @@ -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();
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/router.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand Down

0 comments on commit aafa070

Please sign in to comment.