Skip to content

Commit d76e04a

Browse files
author
vakrilov
committed
fix(router): routing services should be provided in forRoot only
1 parent 1da4cfb commit d76e04a

File tree

3 files changed

+106
-20
lines changed

3 files changed

+106
-20
lines changed

Diff for: nativescript-angular/router/router.module.ts

+28-19
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,41 @@ export { NSEmptyOutletComponent } from "./ns-empty-outlet.component";
1919

2020
export type LocationState = LocationState;
2121

22+
const ROUTER_DIRECTIVES = [NSRouterLink, NSRouterLinkActive, PageRouterOutlet, NSEmptyOutletComponent];
23+
24+
const NS_ROUTER_PROVIDERS = [
25+
{
26+
provide: NSLocationStrategy,
27+
useFactory: provideLocationStrategy,
28+
deps: [[NSLocationStrategy, new Optional(), new SkipSelf()], FrameService],
29+
},
30+
{ provide: LocationStrategy, useExisting: NSLocationStrategy },
31+
NativescriptPlatformLocation,
32+
{ provide: PlatformLocation, useClass: NativescriptPlatformLocation },
33+
RouterExtensions,
34+
NSRouteReuseStrategy,
35+
{ provide: RouteReuseStrategy, useExisting: NSRouteReuseStrategy },
36+
];
37+
2238
@NgModule({
23-
declarations: [NSRouterLink, NSRouterLinkActive, PageRouterOutlet, NSEmptyOutletComponent],
24-
providers: [
25-
{
26-
provide: NSLocationStrategy,
27-
useFactory: provideLocationStrategy,
28-
deps: [[NSLocationStrategy, new Optional(), new SkipSelf()], FrameService],
29-
},
30-
{ provide: LocationStrategy, useExisting: NSLocationStrategy },
31-
NativescriptPlatformLocation,
32-
{ provide: PlatformLocation, useClass: NativescriptPlatformLocation },
33-
RouterExtensions,
34-
NSRouteReuseStrategy,
35-
{ provide: RouteReuseStrategy, useExisting: NSRouteReuseStrategy },
36-
],
39+
declarations: ROUTER_DIRECTIVES,
40+
entryComponents: [NSEmptyOutletComponent],
3741
imports: [RouterModule, NativeScriptCommonModule],
38-
exports: [RouterModule, NSRouterLink, NSRouterLinkActive, PageRouterOutlet, NSEmptyOutletComponent],
42+
exports: [RouterModule, ...ROUTER_DIRECTIVES],
3943
schemas: [NO_ERRORS_SCHEMA],
4044
})
4145
export class NativeScriptRouterModule {
42-
static forRoot(routes: Routes, config?: ExtraOptions): ModuleWithProviders {
43-
return RouterModule.forRoot(routes, config);
46+
static forRoot(routes: Routes, config?: ExtraOptions): ModuleWithProviders<NativeScriptRouterModule> {
47+
const ngForRoot: ModuleWithProviders = RouterModule.forRoot(routes, config);
48+
return {
49+
ngModule: NativeScriptRouterModule,
50+
providers: [...ngForRoot.providers, ...NS_ROUTER_PROVIDERS]
51+
};
4452
}
4553

46-
static forChild(routes: Routes): ModuleWithProviders {
47-
return RouterModule.forChild(routes);
54+
static forChild(routes: Routes): ModuleWithProviders<NativeScriptRouterModule> {
55+
const ngForChild: ModuleWithProviders = RouterModule.forChild(routes);
56+
return { ngModule: NativeScriptRouterModule, providers: ngForChild.providers };
4857
}
4958
}
5059

Diff for: tests/app/tests/router-module-tests.ts

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// make sure you import mocha-config before @angular/core
2+
import { Component, ViewChild } from "@angular/core";
3+
import { nsTestBedAfterEach, nsTestBedBeforeEach, nsTestBedRender } from "nativescript-angular/testing";
4+
import { NativeScriptRouterModule, RouterExtensions } from "nativescript-angular/router";
5+
import { NSRouterLink } from "nativescript-angular/router/ns-router-link";
6+
import { NSLocationStrategy } from "nativescript-angular/router/ns-location-strategy";
7+
import { assert } from "~/tests/test-config";
8+
import { ActivatedRoute, Router, RouteReuseStrategy } from "@angular/router";
9+
import { LocationStrategy, PlatformLocation } from "@angular/common";
10+
import { NSRouteReuseStrategy } from "nativescript-angular/router/ns-route-reuse-strategy";
11+
12+
@Component({
13+
template: `<StackLayout><Label nsRouterLink text="COMPONENT"></Label></StackLayout>`
14+
})
15+
class RouterTestComponent {
16+
@ViewChild(NSRouterLink)
17+
nsRouterLink: NSRouterLink;
18+
}
19+
20+
describe("NativeScriptRouterModule.forRoot", () => {
21+
beforeEach(nsTestBedBeforeEach(
22+
[RouterTestComponent],
23+
[],
24+
[NativeScriptRouterModule.forRoot([])],
25+
[]));
26+
27+
afterEach(nsTestBedAfterEach());
28+
29+
it("should provide nativescript routing services", () => {
30+
return nsTestBedRender(RouterTestComponent).then((fixture) => {
31+
const injector = fixture.componentRef.injector
32+
let ls = injector.get(LocationStrategy, null);
33+
console.log("----> ls: " + typeof ls);
34+
console.dir(ls);
35+
36+
assert.instanceOf(injector.get(LocationStrategy, null), NSLocationStrategy);
37+
assert.instanceOf(injector.get(RouterExtensions, null), RouterExtensions);
38+
assert.instanceOf(injector.get(RouteReuseStrategy, null), NSRouteReuseStrategy);
39+
});
40+
});
41+
42+
it("should provide nativescript routing directives", () => {
43+
return nsTestBedRender(RouterTestComponent).then((fixture) => {
44+
const linkDirective = fixture.componentRef.instance.nsRouterLink;
45+
assert.instanceOf(linkDirective, NSRouterLink);
46+
});
47+
});
48+
});
49+
50+
describe("NativeScriptRouterModule.forChild", () => {
51+
beforeEach(nsTestBedBeforeEach(
52+
[RouterTestComponent],
53+
[
54+
{ provide: Router, useValue: {} },
55+
{ provide: RouterExtensions, useValue: {} },
56+
{ provide: ActivatedRoute, useValue: {} },
57+
],
58+
[NativeScriptRouterModule.forChild([])],
59+
[]));
60+
afterEach(nsTestBedAfterEach());
61+
62+
it("should not provide nativescript routing services", () => {
63+
return nsTestBedRender(RouterTestComponent).then((fixture) => {
64+
const injector = fixture.componentRef.injector
65+
assert.isNull(injector.get(LocationStrategy, null));
66+
assert.isNull(injector.get(RouteReuseStrategy, null));
67+
});
68+
});
69+
70+
it("should provide nativescript routing directives", () => {
71+
return nsTestBedRender(RouterTestComponent).then((fixture) => {
72+
const linkDirective = fixture.componentRef.instance.nsRouterLink;
73+
assert.instanceOf(linkDirective, NSRouterLink);
74+
});
75+
});
76+
});
77+

Diff for: tests/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"@angular/platform-browser": "~7.2.0",
3636
"@angular/platform-browser-dynamic": "~7.2.0",
3737
"@angular/router": "~7.2.0",
38-
"nativescript-angular": "../nativescript-angular",
38+
"nativescript-angular": "file:../nativescript-angular/nativescript-angular-7.3.0.tgz",
3939
"nativescript-unit-test-runner": "^0.3.4",
4040
"rxjs": "~6.3.3",
4141
"tns-core-modules": "next",

0 commit comments

Comments
 (0)