From 778fbc8146afe81f37fd7aff79d1c9c8e519df3f Mon Sep 17 00:00:00 2001 From: Tibi Covaci Date: Fri, 1 Apr 2016 14:31:02 +0100 Subject: [PATCH] feat: add support for RedirectToRoute There is no RedirectToRoute similar to route-herf attribute --- src/navigation-commands.js | 34 +++++++++++++++++ test/navigation-commands.spec.js | 65 +++++++++++++++++++++++++++++++- 2 files changed, 98 insertions(+), 1 deletion(-) diff --git a/src/navigation-commands.js b/src/navigation-commands.js index 59cf4d5c..73594aab 100644 --- a/src/navigation-commands.js +++ b/src/navigation-commands.js @@ -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); + } +} diff --git a/test/navigation-commands.spec.js b/test/navigation-commands.spec.js index c4372a8f..7c1b2866 100644 --- a/test/navigation-commands.spec.js +++ b/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', () => { @@ -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); + }); +});