1
1
// #docregion routestuff
2
- import { ApplicationRef } from '@angular/core' ;
3
- import { LocationStrategy } from '@angular/common' ;
2
+ import { Directive } from '@angular/core' ;
4
3
import { HTTP_PROVIDERS } from '@angular/http' ;
5
4
import {
6
- ROUTER_PROVIDERS ,
5
+ Router ,
6
+ RouterLink ,
7
+ RootRouter ,
8
+ RouteRegistry ,
7
9
ROUTER_PRIMARY_COMPONENT
8
10
} from '@angular/router-deprecated' ;
9
11
import { Observable } from 'rxjs/Rx' ;
10
12
import {
11
13
describe ,
12
- beforeEachProviders ,
14
+ addProviders ,
13
15
inject ,
14
16
it ,
15
17
expect ,
16
- MockApplicationRef
18
+ // MockApplicationRef
17
19
} from '@angular/core/testing' ;
18
- import { MockLocationStrategy } from '@angular/common/testing' ;
20
+ import { SpyLocation } from '@angular/common/testing' ;
19
21
import {
20
22
TestComponentBuilder ,
21
23
ComponentFixture
22
- } from '@angular/compiler /testing' ;
24
+ } from '@angular/core /testing' ;
23
25
24
26
import { AppComponent } from '../app.component' ;
25
27
import { PhoneListComponent } from './phone-list.component' ;
26
28
import { Phone , PhoneData } from '../core/phone/phone.service' ;
27
29
28
30
// #enddocregion routestuff
29
31
32
+ @Directive ( {
33
+ selector : '[routerLink]' ,
34
+ inputs : [ 'routeParams: routerLink' , 'target: target' ]
35
+ } )
36
+ class RouterLinkMock { }
30
37
31
38
class MockPhone extends Phone {
32
39
query ( ) : Observable < PhoneData [ ] > {
@@ -41,19 +48,22 @@ describe('PhoneList', () => {
41
48
42
49
// #docregion routestuff
43
50
44
- beforeEachProviders ( ( ) => [
45
- { provide : Phone , useClass : MockPhone } ,
46
- HTTP_PROVIDERS ,
47
- ROUTER_PROVIDERS ,
48
- { provide : ApplicationRef , useClass : MockApplicationRef } ,
51
+ addProviders ( [
52
+ RouteRegistry ,
53
+ { provide : Router , useClass : RootRouter } ,
49
54
{ provide : ROUTER_PRIMARY_COMPONENT , useValue : AppComponent } ,
50
- { provide : LocationStrategy , useClass : MockLocationStrategy } ,
55
+ { provide : Location , useClass : SpyLocation } ,
56
+ { provide : Phone , useClass : MockPhone } ,
57
+ HTTP_PROVIDERS
51
58
] ) ;
52
59
// #enddocregion routestuff
53
60
54
61
it ( 'should create "phones" model with 2 phones fetched from xhr' ,
55
62
inject ( [ TestComponentBuilder ] , ( tcb : TestComponentBuilder ) => {
56
- return tcb . createAsync ( PhoneListComponent )
63
+ return tcb
64
+ . overrideDirective ( AppComponent , RouterLink , RouterLinkMock )
65
+ . overrideDirective ( PhoneListComponent , RouterLink , RouterLinkMock )
66
+ . createAsync ( PhoneListComponent )
57
67
. then ( ( fixture : ComponentFixture < PhoneListComponent > ) => {
58
68
fixture . detectChanges ( ) ;
59
69
let compiled = fixture . debugElement . nativeElement ;
@@ -69,7 +79,10 @@ describe('PhoneList', () => {
69
79
70
80
it ( 'should set the default value of orderProp model' ,
71
81
inject ( [ TestComponentBuilder ] , ( tcb : TestComponentBuilder ) => {
72
- return tcb . createAsync ( PhoneListComponent )
82
+ return tcb
83
+ . overrideDirective ( AppComponent , RouterLink , RouterLinkMock )
84
+ . overrideDirective ( PhoneListComponent , RouterLink , RouterLinkMock )
85
+ . createAsync ( PhoneListComponent )
73
86
. then ( ( fixture : ComponentFixture < PhoneListComponent > ) => {
74
87
fixture . detectChanges ( ) ;
75
88
let compiled = fixture . debugElement . nativeElement ;
0 commit comments