This repository has been archived by the owner on Aug 5, 2021. It is now read-only.
/
app.component.spec.ts
134 lines (113 loc) 路 3.73 KB
/
app.component.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
import { APP_BASE_HREF, Location } from '@angular/common';
import {
Component,
Type,
Compiler,
NgModuleFactoryLoader,
Injector
} from '@angular/core';
import {
Router,
Routes,
UrlSerializer,
NavigationStart,
NavigationEnd,
Event
} from '@angular/router';
import { Angulartics2Module, Angulartics2, Angulartics2GoogleTagManager } from 'angulartics2';
import { RouterModule } from '@angular/router';
import { HttpModule } from '@angular/http';
import { RouterTestingModule } from '@angular/router/testing';
import { Observable } from 'rxjs/Observable';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { AppComponent } from './app.component';
import { TestBed, ComponentFixture, inject } from '@angular/core/testing';
class MockRouter {
routerState = {root: ''};
private rootComponentType: Type<any>;
private events: Observable<Event>;
constructor() {
this.events = Observable.of(new NavigationStart(2, ''));
}
createUrlTree() {
return {};
}
serializeUrl() {
return 'foo';
}
}
describe('AppComponent', () => {
describe('component logic', () => {
let fixture: ComponentFixture<AppComponent>;
let component: AppComponent;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
Angulartics2Module.forRoot(),
HttpModule,
RouterModule.forRoot([]),
],
declarations: [ AppComponent ],
providers: [
Angulartics2,
Angulartics2GoogleTagManager,
{provide: APP_BASE_HREF, useValue: '/'},
{provide: Router, useClass: MockRouter}
]
});
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
});
it('should have defined AppComponent instance', () => {
fixture.detectChanges();
expect(component).toBeDefined();
});
it('should have defined router event subscription', () => {
fixture.detectChanges();
expect(component.eventSub).toBeDefined();
});
it('should unsubscribe from router events when component is destroyed', () => {
fixture.detectChanges();
spyOn(component.eventSub, 'unsubscribe');
fixture.destroy();
expect(component.eventSub.unsubscribe).toHaveBeenCalled();
});
});
describe('component template', () => {
let fixture: ComponentFixture<AppComponent>;
let component: AppComponent;
beforeEach(() => {
TestBed.resetTestingModule();
TestBed.configureTestingModule({
imports: [
Angulartics2Module.forRoot(),
HttpModule,
RouterModule.forRoot([]),
],
declarations: [AppComponent],
providers: [
Angulartics2,
Angulartics2GoogleTagManager,
{provide: APP_BASE_HREF, useValue: '/'},
{provide: Router, useClass: MockRouter}
]
});
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
});
it('should have header, nav and footer elements', () => {
fixture.detectChanges();
let head = fixture.nativeElement.querySelector('header');
let nav = fixture.nativeElement.querySelector('nav');
let foot = fixture.nativeElement.querySelector('footer');
expect(head).toBeDefined();
expect(nav).toBeDefined();
expect(foot).toBeDefined();
});
it('should have a main element with router-outlet child', () => {
fixture.detectChanges();
let outlet = fixture.nativeElement.querySelector('main router-outlet');
expect(outlet).toBeDefined();
});
});
});