Skip to content

Commit de299b1

Browse files
committed
Test router hook logs
1 parent 1d1a9d4 commit de299b1

File tree

6 files changed

+49
-18
lines changed

6 files changed

+49
-18
lines changed

tests/app/base.component.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import {ROUTER_DIRECTIVES, Router, OnActivate, OnDeactivate, CanReuse, OnReuse,
22
RouteParams, ComponentInstruction, RouteConfig } from '@angular/router-deprecated';
3-
import {Component} from "@angular/core";
4-
5-
6-
export const hooksLog = [];
3+
import {Component, OpaqueToken} from "@angular/core";
4+
export const HOOKS_LOG = new OpaqueToken("Hooks log");
75

86
export class BaseComponent implements OnActivate, OnDeactivate {
97
protected name: string = "";
108

9+
constructor(protected hooksLog: string[]) {
10+
}
11+
1112
routerOnActivate(nextInstruction: ComponentInstruction, prevInstruction: ComponentInstruction): any {
1213
this.log("activate", nextInstruction, prevInstruction);
1314
}
@@ -17,6 +18,6 @@ export class BaseComponent implements OnActivate, OnDeactivate {
1718
}
1819

1920
private log(method: string, nextInstruction: ComponentInstruction, prevInstruction: ComponentInstruction) {
20-
hooksLog.push(this.name + "." + method + " " + nextInstruction.urlPath + " " + (prevInstruction ? prevInstruction.urlPath : null));
21+
this.hooksLog.push(this.name + "." + method + " " + nextInstruction.urlPath + " " + (prevInstruction ? prevInstruction.urlPath : null));
2122
}
2223
}

tests/app/first.component.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import {ROUTER_DIRECTIVES, Router, OnActivate, OnDeactivate, CanReuse, OnReuse,
22
RouteParams, RouteData, ComponentInstruction, RouteConfig } from '@angular/router-deprecated';
3-
import {Component} from "@angular/core";
4-
import {BaseComponent} from "./base.component";
3+
import {Component, Inject} from "@angular/core";
4+
import {HOOKS_LOG, BaseComponent} from "./base.component";
55

66
@Component({
77
selector: "first-comp",
88
template: `
99
<StackLayout>
1010
<Label [automationText]="'first-' + id" [text]="'First: ' + id"></Label>
1111
<Button [automationText]="'first-navigate-' + id" text="Go to second" (tap)="gotoSecond()"></Button>
12+
<TextView [automationText]="'hooks-log-' + id" [text]="hooksLog"></TextView>
13+
<TextView [text]="'hooks-log-' + id"></TextView>
1214
</StackLayout>
1315
`
1416
})
1517
export class FirstComponent extends BaseComponent {
16-
constructor(private router: Router, private routeData: RouteData) {
17-
super();
18+
name = "first";
19+
20+
constructor(private router: Router, private routeData: RouteData, @Inject(HOOKS_LOG) hooksLog: string[]) {
21+
super(hooksLog);
1822
}
1923

2024
ngOnInit() {

tests/app/main.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import {Label} from "ui/label";
1111
import {StackLayout} from "ui/layouts/stack-layout";
1212
import * as application from "application";
1313
//nativeScriptBootstrap(AppComponent, [NS_ROUTER_PROVIDERS]);
14+
import {HOOKS_LOG} from "./base.component";
1415
import {MultiPageMain} from "./multi-page-main.component";
1516
import {SinglePageMain} from "./single-page-main.component";
16-
import {provide} from "@angular/core";
17+
import {provide, OpaqueToken} from "@angular/core";
1718

1819
import { rendererTraceCategory, routerTraceCategory } from "nativescript-angular/trace";
1920

@@ -40,8 +41,12 @@ application.start({
4041
console.log('BOOTSTRAPPING TEST APPS...');
4142
//bootstrap(MultiPageMain, [NS_ROUTER_PROVIDERS]);
4243
const rootViewProvider = provide(APP_ROOT_VIEW, { useValue: root });
43-
bootstrap(SinglePageMain, [rootViewProvider, NS_ROUTER_PROVIDERS]);
44-
bootstrap(MultiPageMain, [rootViewProvider, NS_ROUTER_PROVIDERS]);
44+
let singlePageHooksLog = []
45+
const singlePageHooksLogProvider = provide(HOOKS_LOG, { useValue: singlePageHooksLog });
46+
bootstrap(SinglePageMain, [rootViewProvider, singlePageHooksLogProvider, NS_ROUTER_PROVIDERS]);
47+
let multiPageHooksLog = []
48+
const multiPageHooksLogProvider = provide(HOOKS_LOG, { useValue: multiPageHooksLog });
49+
bootstrap(MultiPageMain, [rootViewProvider, multiPageHooksLogProvider, NS_ROUTER_PROVIDERS]);
4550
}
4651

4752
page.on('loaded', onLoadedHandler);

tests/app/second.component.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
import {ROUTER_DIRECTIVES, Router, OnActivate, OnDeactivate, CanReuse, OnReuse,
22
RouteParams, RouteData, ComponentInstruction, RouteConfig } from '@angular/router-deprecated';
3-
import {Component} from "@angular/core";
4-
import {BaseComponent} from "./base.component";
3+
import {Component, Inject} from "@angular/core";
4+
import {HOOKS_LOG, BaseComponent} from "./base.component";
55

66
@Component({
77
selector: "second-comp",
88
template: `
99
<StackLayout>
1010
<Label [automationText]="'second-' + id" [text]="'Second: ' + id"></Label>
1111
<Button [automationText]="'second-navigate-' + id" text="Go to first" (tap)="gotoFirst()"></Button>
12+
<TextView [automationText]="'hooks-log-' + id" [text]="hooksLog"></TextView>
13+
<TextView [text]="'hooks-log-' + id"></TextView>
1214
</StackLayout>
1315
`
1416
})
1517
export class SecondComponent extends BaseComponent {
16-
constructor(private router: Router, private routeData: RouteData) {
17-
super();
18+
constructor(private router: Router, private routeData: RouteData, @Inject(HOOKS_LOG) hooksLog: string[]) {
19+
super(hooksLog);
1820
}
1921

2022
ngOnInit() {
2123
this.id = this.routeData.get('id');
2224
}
2325

2426
public id: string = ""
27+
name = "second";
2528

2629
gotoFirst() {
2730
this.router.navigateByUrl("/first");

tests/e2e-tests/multi-page-routing.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ describe("multi page routing", function () {
2525
});
2626

2727
it("navigates and returns", function () {
28+
var expectedHookLog = [
29+
"first.activate first null",
30+
"first.deactivate second first",
31+
"second.activate second first",
32+
"second.deactivate first second",
33+
"first.activate first second"].join(",");
2834
return driver
2935
.elementByAccessibilityId("first-navigate-multi-page")
3036
.should.eventually.exist
@@ -37,6 +43,8 @@ describe("multi page routing", function () {
3743
.tap()
3844
.elementByAccessibilityId("first-multi-page")
3945
.should.eventually.exist
40-
.text().should.eventually.equal("First: multi-page")
46+
.text().should.eventually.equal("First: multi-page")
47+
.elementByAccessibilityId("hooks-log-multi-page")
48+
.text().should.eventually.equal(expectedHookLog)
4149
});
4250
});

tests/e2e-tests/single-page-routing.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ describe("single page routing", function () {
2424
.text().should.eventually.equal("First: single-page")
2525
});
2626

27-
it("navigates and returns", function () {
27+
it("navigates, returns and fires hooks", function () {
28+
var expectedHookLog = [
29+
"first.activate first null",
30+
"first.deactivate second first",
31+
"second.activate second first",
32+
"second.deactivate first second",
33+
"first.activate first second"].join(",");
34+
35+
2836
return driver
2937
.elementByAccessibilityId("first-navigate-single-page")
3038
.should.eventually.exist
@@ -38,5 +46,7 @@ describe("single page routing", function () {
3846
.elementByAccessibilityId("first-single-page")
3947
.should.eventually.exist
4048
.text().should.eventually.equal("First: single-page")
49+
.elementByAccessibilityId("hooks-log-single-page")
50+
.text().should.eventually.equal(expectedHookLog)
4151
});
4252
});

0 commit comments

Comments
 (0)