Skip to content

Commit

Permalink
feat: add support for RedirectToRoute
Browse files Browse the repository at this point in the history
There is no RedirectToRoute similar to route-herf attribute
  • Loading branch information
tibor19 committed Apr 2, 2016
1 parent 6f3b998 commit 778fbc8
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/navigation-commands.js
Expand Up @@ -37,3 +37,37 @@ export class Redirect {
navigatingRouter.navigate(this.url, this.options);
}
}

/**
* Used during the activation lifecycle to cause a redirect to a named route.
* @param route The name of the route.
* @param params The parameters to be sent to the activation method.
* @param options The options to use for navigation.
*/
export class RedirectToRoute {
constructor(route: string, params: any = {}, options: any = {}) {
this.route = route;
this.params = params;
this.options = Object.assign({ trigger: true, replace: true }, options);
this.shouldContinueProcessing = false;
}

/**
* Called by the activation system to set the child router.
*
* @param router The router.
*/
setRouter(router: Router): void {
this.router = router;
}

/**
* Called by the navigation pipeline to navigate.
*
* @param appRouter The router to be redirected.
*/
navigate(appRouter: Router): void {
let navigatingRouter = this.options.useAppRouter ? appRouter : (this.router || appRouter);
navigatingRouter.navigateToRoute(this.route, this.params, this.options);
}
}
65 changes: 64 additions & 1 deletion test/navigation-commands.spec.js
@@ -1,4 +1,4 @@
import {Redirect, isNavigationCommand} from '../src/navigation-commands';
import {Redirect, RedirectToRoute, isNavigationCommand} from '../src/navigation-commands';

describe('isNavigationCommand', () => {
it('should return true for object which has a navigate method', () => {
Expand Down Expand Up @@ -58,6 +58,69 @@ describe('Redirect', () => {

redirect.navigate(mockapprouter);

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

describe('RedirectToRoute', () => {
it('should accept url in constructor and pass this url to passed router\'s navigate method as first parameter', () => {
let testroute = 'test';
let testparams = {id: 1};
let redirect = new RedirectToRoute(testroute, testparams);
let mockrouter = {
route: '',
params: {},
navigateToRoute(route, params) {
this.route = route;
this.params = params;
}
};

redirect.setRouter(mockrouter);

expect(mockrouter.route).toBe('');
expect(mockrouter.params).toEqual({});

redirect.navigate(mockrouter);

expect(mockrouter.route).toBe(testroute);
expect(mockrouter.params).toEqual(testparams);
});

it('should accept options in constructor to use the app router', () => {
let testroute = 'test';
let testparams = {id: 1};
let redirect = new RedirectToRoute(testroute, testparams, {useAppRouter: true});
let mockrouter = {
route: '',
params: {},
navigateToRoute(route, params) {
this.route = route;
this.params = params;
}
};

let mockapprouter = {
route: '',
params: {},
navigateToRoute(route, params) {
this.route = route;
this.params = params;
}
};

redirect.setRouter(mockrouter);

expect(mockapprouter.route).toBe('');
expect(mockapprouter.params).toEqual({});

redirect.navigate(mockapprouter);

expect(mockrouter.route).toBe('');
expect(mockrouter.params).toEqual({});

expect(mockapprouter.route).toBe(testroute);
expect(mockapprouter.params).toEqual(testparams);
});
});

0 comments on commit 778fbc8

Please sign in to comment.