forked from angular/angular
/
page_title_strategy_spec.ts
185 lines (165 loc) · 5.4 KB
/
page_title_strategy_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {DOCUMENT} from '@angular/common';
import {Component, Inject, Injectable, NgModule} from '@angular/core';
import {fakeAsync, TestBed, tick} from '@angular/core/testing';
import {NavigationEnd, Router, RouterModule, RouterStateSnapshot, TitleStrategy} from '@angular/router';
import {provideRouterForTesting} from '../testing/src/provide_router_for_testing';
describe('title strategy', () => {
describe('DefaultTitleStrategy', () => {
let router: Router;
let document: Document;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
TestModule,
],
providers: [provideRouterForTesting()]
});
router = TestBed.inject(Router);
document = TestBed.inject(DOCUMENT);
});
it('sets page title from data', fakeAsync(() => {
router.resetConfig([{path: 'home', title: 'My Application', component: BlankCmp}]);
router.navigateByUrl('home');
tick();
expect(document.title).toBe('My Application');
}));
it('sets page title from resolved data', fakeAsync(() => {
router.resetConfig([{path: 'home', title: TitleResolver, component: BlankCmp}]);
router.navigateByUrl('home');
tick();
expect(document.title).toBe('resolved title');
}));
it('sets page title from resolved data function', fakeAsync(() => {
router.resetConfig([{path: 'home', title: () => 'resolved title', component: BlankCmp}]);
router.navigateByUrl('home');
tick();
expect(document.title).toBe('resolved title');
}));
it('sets title with child routes', fakeAsync(() => {
router.resetConfig([{
path: 'home',
title: 'My Application',
children: [
{path: '', title: 'child title', component: BlankCmp},
]
}]);
router.navigateByUrl('home');
tick();
expect(document.title).toBe('child title');
}));
it('sets title with child routes and named outlets', fakeAsync(() => {
router.resetConfig([
{
path: 'home',
title: 'My Application',
children: [
{path: '', title: 'child title', component: BlankCmp},
{path: '', outlet: 'childaux', title: 'child aux title', component: BlankCmp},
],
},
{path: 'compose', component: BlankCmp, outlet: 'aux', title: 'compose'}
]);
router.navigateByUrl('home(aux:compose)');
tick();
expect(document.title).toBe('child title');
}));
it('sets page title with paramsInheritanceStrategy=always', fakeAsync(() => {
router.paramsInheritanceStrategy = 'always';
router.resetConfig([
{
path: 'home',
title: 'My Application',
children: [
{
path: '',
title: TitleResolver,
component: BlankCmp,
},
],
},
]);
router.navigateByUrl('home');
tick();
expect(document.title).toBe('resolved title');
}));
it('can get the title from the ActivatedRouteSnapshot', async () => {
router.resetConfig([
{
path: 'home',
title: 'My Application',
component: BlankCmp,
},
]);
await router.navigateByUrl('home');
expect(router.routerState.snapshot.root.firstChild!.title).toEqual('My Application');
});
});
describe('custom strategies', () => {
it('overriding the setTitle method', fakeAsync(() => {
@Injectable({providedIn: 'root'})
class TemplatePageTitleStrategy extends TitleStrategy {
constructor(@Inject(DOCUMENT) private readonly document: Document) {
super();
}
// Example of how setTitle could implement a template for the title
override updateTitle(state: RouterStateSnapshot) {
const title = this.buildTitle(state);
this.document.title = `My Application | ${title}`;
}
}
TestBed.configureTestingModule({
imports: [
TestModule,
],
providers: [
provideRouterForTesting(),
{provide: TitleStrategy, useClass: TemplatePageTitleStrategy}
]
});
const router = TestBed.inject(Router);
const document = TestBed.inject(DOCUMENT);
router.resetConfig([
{
path: 'home',
title: 'Home',
children: [
{path: '', title: 'Child', component: BlankCmp},
],
},
]);
router.navigateByUrl('home');
tick();
expect(document.title).toEqual('My Application | Child');
}));
});
});
@Component({template: ''})
export class BlankCmp {
}
@Component({
template: `
<router-outlet></router-outlet>
<router-outlet name="aux"></router-outlet>
`
})
export class RootCmp {
}
@NgModule({
declarations: [BlankCmp],
imports: [RouterModule],
})
export class TestModule {
}
@Injectable({providedIn: 'root'})
export class TitleResolver {
resolve() {
return 'resolved title';
}
}