Skip to content

Commit

Permalink
feat(redirect): enable redirect from the app router by option
Browse files Browse the repository at this point in the history
Fixes #51
  • Loading branch information
EisenbergEffect committed Mar 23, 2015
1 parent 18e52d8 commit 4d1a7ea
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/navigation-commands.js
Expand Up @@ -16,8 +16,9 @@ export function isNavigationCommand(obj){
* @param {String} url The url to redirect to.
*/
export class Redirect{
constructor(url) {
constructor(url, options) {
this.url = url;
this.options = Object.assign({ trigger: true, replace: true }, options || {});
this.shouldContinueProcessing = false;
}

Expand All @@ -38,6 +39,7 @@ export class Redirect{
* @param {Router} appRouter - a router which should redirect
*/
navigate(appRouter){
(this.router || appRouter).navigate(this.url, { trigger: true, replace: true });
var navigatingRouter = this.options.useAppRouter ? appRouter : (this.router || appRouter);
navigatingRouter.navigate(this.url, this.options);
}
}
30 changes: 29 additions & 1 deletion test/navigation-commands.spec.js
@@ -1,4 +1,5 @@
import {Redirect, isNavigationCommand} from '../src/navigation-commands';
import core from 'core-js';

describe('isNavigaionCommand', () => {
it('should return true for object which has a navigate method', () => {
Expand Down Expand Up @@ -27,10 +28,37 @@ describe('Redirect', () => {
}
};

redirect.setRouter(mockrouter);

expect(mockrouter.url).toBe('');

redirect.navigate(mockrouter);

expect(mockrouter.url).toBe(testurl);
});
});

it('should accept options in constructor to use the app router', () => {
var testurl = 'http://aurelia.io/',
redirect = new Redirect(testurl, {useAppRouter:true}),
mockrouter = {
url: '',
navigate(url) {
this.url = url;
}
},
mockapprouter = {
url: '',
navigate(url) {
this.url = url;
}
};

redirect.setRouter(mockrouter);

expect(mockapprouter.url).toBe('');

redirect.navigate(mockapprouter);

expect(mockapprouter.url).toBe(testurl);
});
});

0 comments on commit 4d1a7ea

Please sign in to comment.