From d76e04a667e5ab0c3577532798e1bac908d41369 Mon Sep 17 00:00:00 2001 From: vakrilov Date: Thu, 14 Feb 2019 12:04:18 +0200 Subject: [PATCH 1/3] fix(router): routing services should be provided in forRoot only --- nativescript-angular/router/router.module.ts | 47 +++++++----- tests/app/tests/router-module-tests.ts | 77 ++++++++++++++++++++ tests/package.json | 2 +- 3 files changed, 106 insertions(+), 20 deletions(-) create mode 100644 tests/app/tests/router-module-tests.ts diff --git a/nativescript-angular/router/router.module.ts b/nativescript-angular/router/router.module.ts index 385b4fe1f..2aa266f28 100644 --- a/nativescript-angular/router/router.module.ts +++ b/nativescript-angular/router/router.module.ts @@ -19,32 +19,41 @@ export { NSEmptyOutletComponent } from "./ns-empty-outlet.component"; export type LocationState = LocationState; +const ROUTER_DIRECTIVES = [NSRouterLink, NSRouterLinkActive, PageRouterOutlet, NSEmptyOutletComponent]; + +const NS_ROUTER_PROVIDERS = [ + { + provide: NSLocationStrategy, + useFactory: provideLocationStrategy, + deps: [[NSLocationStrategy, new Optional(), new SkipSelf()], FrameService], + }, + { provide: LocationStrategy, useExisting: NSLocationStrategy }, + NativescriptPlatformLocation, + { provide: PlatformLocation, useClass: NativescriptPlatformLocation }, + RouterExtensions, + NSRouteReuseStrategy, + { provide: RouteReuseStrategy, useExisting: NSRouteReuseStrategy }, +]; + @NgModule({ - declarations: [NSRouterLink, NSRouterLinkActive, PageRouterOutlet, NSEmptyOutletComponent], - providers: [ - { - provide: NSLocationStrategy, - useFactory: provideLocationStrategy, - deps: [[NSLocationStrategy, new Optional(), new SkipSelf()], FrameService], - }, - { provide: LocationStrategy, useExisting: NSLocationStrategy }, - NativescriptPlatformLocation, - { provide: PlatformLocation, useClass: NativescriptPlatformLocation }, - RouterExtensions, - NSRouteReuseStrategy, - { provide: RouteReuseStrategy, useExisting: NSRouteReuseStrategy }, - ], + declarations: ROUTER_DIRECTIVES, + entryComponents: [NSEmptyOutletComponent], imports: [RouterModule, NativeScriptCommonModule], - exports: [RouterModule, NSRouterLink, NSRouterLinkActive, PageRouterOutlet, NSEmptyOutletComponent], + exports: [RouterModule, ...ROUTER_DIRECTIVES], schemas: [NO_ERRORS_SCHEMA], }) export class NativeScriptRouterModule { - static forRoot(routes: Routes, config?: ExtraOptions): ModuleWithProviders { - return RouterModule.forRoot(routes, config); + static forRoot(routes: Routes, config?: ExtraOptions): ModuleWithProviders { + const ngForRoot: ModuleWithProviders = RouterModule.forRoot(routes, config); + return { + ngModule: NativeScriptRouterModule, + providers: [...ngForRoot.providers, ...NS_ROUTER_PROVIDERS] + }; } - static forChild(routes: Routes): ModuleWithProviders { - return RouterModule.forChild(routes); + static forChild(routes: Routes): ModuleWithProviders { + const ngForChild: ModuleWithProviders = RouterModule.forChild(routes); + return { ngModule: NativeScriptRouterModule, providers: ngForChild.providers }; } } diff --git a/tests/app/tests/router-module-tests.ts b/tests/app/tests/router-module-tests.ts new file mode 100644 index 000000000..c441700fd --- /dev/null +++ b/tests/app/tests/router-module-tests.ts @@ -0,0 +1,77 @@ +// make sure you import mocha-config before @angular/core +import { Component, ViewChild } from "@angular/core"; +import { nsTestBedAfterEach, nsTestBedBeforeEach, nsTestBedRender } from "nativescript-angular/testing"; +import { NativeScriptRouterModule, RouterExtensions } from "nativescript-angular/router"; +import { NSRouterLink } from "nativescript-angular/router/ns-router-link"; +import { NSLocationStrategy } from "nativescript-angular/router/ns-location-strategy"; +import { assert } from "~/tests/test-config"; +import { ActivatedRoute, Router, RouteReuseStrategy } from "@angular/router"; +import { LocationStrategy, PlatformLocation } from "@angular/common"; +import { NSRouteReuseStrategy } from "nativescript-angular/router/ns-route-reuse-strategy"; + +@Component({ + template: `` +}) +class RouterTestComponent { + @ViewChild(NSRouterLink) + nsRouterLink: NSRouterLink; +} + +describe("NativeScriptRouterModule.forRoot", () => { + beforeEach(nsTestBedBeforeEach( + [RouterTestComponent], + [], + [NativeScriptRouterModule.forRoot([])], + [])); + + afterEach(nsTestBedAfterEach()); + + it("should provide nativescript routing services", () => { + return nsTestBedRender(RouterTestComponent).then((fixture) => { + const injector = fixture.componentRef.injector + let ls = injector.get(LocationStrategy, null); + console.log("----> ls: " + typeof ls); + console.dir(ls); + + assert.instanceOf(injector.get(LocationStrategy, null), NSLocationStrategy); + assert.instanceOf(injector.get(RouterExtensions, null), RouterExtensions); + assert.instanceOf(injector.get(RouteReuseStrategy, null), NSRouteReuseStrategy); + }); + }); + + it("should provide nativescript routing directives", () => { + return nsTestBedRender(RouterTestComponent).then((fixture) => { + const linkDirective = fixture.componentRef.instance.nsRouterLink; + assert.instanceOf(linkDirective, NSRouterLink); + }); + }); +}); + +describe("NativeScriptRouterModule.forChild", () => { + beforeEach(nsTestBedBeforeEach( + [RouterTestComponent], + [ + { provide: Router, useValue: {} }, + { provide: RouterExtensions, useValue: {} }, + { provide: ActivatedRoute, useValue: {} }, + ], + [NativeScriptRouterModule.forChild([])], + [])); + afterEach(nsTestBedAfterEach()); + + it("should not provide nativescript routing services", () => { + return nsTestBedRender(RouterTestComponent).then((fixture) => { + const injector = fixture.componentRef.injector + assert.isNull(injector.get(LocationStrategy, null)); + assert.isNull(injector.get(RouteReuseStrategy, null)); + }); + }); + + it("should provide nativescript routing directives", () => { + return nsTestBedRender(RouterTestComponent).then((fixture) => { + const linkDirective = fixture.componentRef.instance.nsRouterLink; + assert.instanceOf(linkDirective, NSRouterLink); + }); + }); +}); + diff --git a/tests/package.json b/tests/package.json index 29bede509..7375973e3 100644 --- a/tests/package.json +++ b/tests/package.json @@ -35,7 +35,7 @@ "@angular/platform-browser": "~7.2.0", "@angular/platform-browser-dynamic": "~7.2.0", "@angular/router": "~7.2.0", - "nativescript-angular": "../nativescript-angular", + "nativescript-angular": "file:../nativescript-angular/nativescript-angular-7.3.0.tgz", "nativescript-unit-test-runner": "^0.3.4", "rxjs": "~6.3.3", "tns-core-modules": "next", From 2809b24213672a4d51804e5db16e8330dab4f4cf Mon Sep 17 00:00:00 2001 From: Alexander Vakrilov Date: Fri, 15 Feb 2019 14:11:31 +0200 Subject: [PATCH 2/3] refactor: remove logs --- tests/app/tests/router-module-tests.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/app/tests/router-module-tests.ts b/tests/app/tests/router-module-tests.ts index c441700fd..7bce3f7ef 100644 --- a/tests/app/tests/router-module-tests.ts +++ b/tests/app/tests/router-module-tests.ts @@ -29,9 +29,6 @@ describe("NativeScriptRouterModule.forRoot", () => { it("should provide nativescript routing services", () => { return nsTestBedRender(RouterTestComponent).then((fixture) => { const injector = fixture.componentRef.injector - let ls = injector.get(LocationStrategy, null); - console.log("----> ls: " + typeof ls); - console.dir(ls); assert.instanceOf(injector.get(LocationStrategy, null), NSLocationStrategy); assert.instanceOf(injector.get(RouterExtensions, null), RouterExtensions); From 8953ee3db03a876f39e2e490c815a5f02ab60d17 Mon Sep 17 00:00:00 2001 From: vakrilov Date: Mon, 18 Feb 2019 17:49:09 +0200 Subject: [PATCH 3/3] refactor: use single return statment in forRoot/Child --- nativescript-angular/router/router.module.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/nativescript-angular/router/router.module.ts b/nativescript-angular/router/router.module.ts index 2aa266f28..95610d6aa 100644 --- a/nativescript-angular/router/router.module.ts +++ b/nativescript-angular/router/router.module.ts @@ -44,16 +44,14 @@ const NS_ROUTER_PROVIDERS = [ }) export class NativeScriptRouterModule { static forRoot(routes: Routes, config?: ExtraOptions): ModuleWithProviders { - const ngForRoot: ModuleWithProviders = RouterModule.forRoot(routes, config); return { ngModule: NativeScriptRouterModule, - providers: [...ngForRoot.providers, ...NS_ROUTER_PROVIDERS] + providers: [...RouterModule.forRoot(routes, config).providers, ...NS_ROUTER_PROVIDERS] }; } static forChild(routes: Routes): ModuleWithProviders { - const ngForChild: ModuleWithProviders = RouterModule.forChild(routes); - return { ngModule: NativeScriptRouterModule, providers: ngForChild.providers }; + return { ngModule: NativeScriptRouterModule, providers: RouterModule.forChild(routes).providers }; } }