|
| 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 | + |
0 commit comments