Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0f6a975

Browse files
author
Alexander Vakrilov
authored
fix(router): routing services should be provided in forRoot only (#1729)
* fix(router): routing services should be provided in forRoot only * refactor: remove logs * refactor: use single return statment in forRoot/Child
1 parent 1da4cfb commit 0f6a975

File tree

3 files changed

+101
-20
lines changed

3 files changed

+101
-20
lines changed

‎nativescript-angular/router/router.module.ts‎

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,39 @@ 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+
return {
48+
ngModule: NativeScriptRouterModule,
49+
providers: [...RouterModule.forRoot(routes, config).providers, ...NS_ROUTER_PROVIDERS]
50+
};
4451
}
4552

46-
static forChild(routes: Routes): ModuleWithProviders {
47-
return RouterModule.forChild(routes);
53+
static forChild(routes: Routes): ModuleWithProviders<NativeScriptRouterModule> {
54+
return {ngModule: NativeScriptRouterModule,providers: RouterModule.forChild(routes).providers};
4855
}
4956
}
5057

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
33+
assert.instanceOf(injector.get(LocationStrategy, null), NSLocationStrategy);
34+
assert.instanceOf(injector.get(RouterExtensions, null), RouterExtensions);
35+
assert.instanceOf(injector.get(RouteReuseStrategy, null), NSRouteReuseStrategy);
36+
});
37+
});
38+
39+
it("should provide nativescript routing directives", () => {
40+
return nsTestBedRender(RouterTestComponent).then((fixture) => {
41+
const linkDirective = fixture.componentRef.instance.nsRouterLink;
42+
assert.instanceOf(linkDirective, NSRouterLink);
43+
});
44+
});
45+
});
46+
47+
describe("NativeScriptRouterModule.forChild", () => {
48+
beforeEach(nsTestBedBeforeEach(
49+
[RouterTestComponent],
50+
[
51+
{ provide: Router, useValue: {} },
52+
{ provide: RouterExtensions, useValue: {} },
53+
{ provide: ActivatedRoute, useValue: {} },
54+
],
55+
[NativeScriptRouterModule.forChild([])],
56+
[]));
57+
afterEach(nsTestBedAfterEach());
58+
59+
it("should not provide nativescript routing services", () => {
60+
return nsTestBedRender(RouterTestComponent).then((fixture) => {
61+
const injector = fixture.componentRef.injector
62+
assert.isNull(injector.get(LocationStrategy, null));
63+
assert.isNull(injector.get(RouteReuseStrategy, null));
64+
});
65+
});
66+
67+
it("should provide nativescript routing directives", () => {
68+
return nsTestBedRender(RouterTestComponent).then((fixture) => {
69+
const linkDirective = fixture.componentRef.instance.nsRouterLink;
70+
assert.instanceOf(linkDirective, NSRouterLink);
71+
});
72+
});
73+
});
74+

‎tests/package.json‎

Lines changed: 1 addition & 1 deletion
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)

AltStyle によって変換されたページ (->オリジナル) /