1
+ // #docplaster
1
2
import { async , ComponentFixture , TestBed
2
3
} from '@angular/core/testing' ;
3
4
4
- import { NO_ERRORS_SCHEMA } from '@angular/core' ;
5
+ import { DebugElement } from '@angular/core' ;
5
6
import { By } from '@angular/platform-browser' ;
6
7
7
- import { AppComponent } from './app.component' ;
8
- import { BannerComponent } from './banner.component' ;
9
- import { SharedModule } from './shared/shared.module' ;
10
-
11
- import { Router , RouterStub , RouterLinkDirectiveStub , RouterOutletStubComponent
12
- } from '../testing' ;
13
-
8
+ // #docregion setup-schemas
9
+ import { NO_ERRORS_SCHEMA } from '@angular/core' ;
10
+ // #enddocregion setup-schemas
11
+ // #docregion setup-stubs-w-imports
12
+ import { Component } from '@angular/core' ;
13
+ // #docregion setup-schemas
14
+ import { AppComponent } from './app.component' ;
15
+ // #enddocregion setup-schemas
16
+ import { BannerComponent } from './banner.component' ;
17
+ import { RouterLinkStubDirective } from '../testing' ;
18
+ // #docregion setup-schemas
19
+ import { RouterOutletStubComponent } from '../testing' ;
20
+
21
+ // #enddocregion setup-schemas
22
+ @Component ( { selector : 'app-welcome' , template : '' } )
23
+ class WelcomeStubComponent { }
24
+
25
+ // #enddocregion setup-stubs-w-imports
14
26
15
27
let comp : AppComponent ;
16
28
let fixture : ComponentFixture < AppComponent > ;
17
29
18
30
describe ( 'AppComponent & TestModule' , ( ) => {
31
+ // #docregion setup-stubs, setup-stubs-w-imports
19
32
beforeEach ( async ( ( ) => {
20
33
TestBed . configureTestingModule ( {
21
34
declarations : [
22
- AppComponent , BannerComponent ,
23
- RouterLinkDirectiveStub , RouterOutletStubComponent
24
- ] ,
25
- providers : [ { provide : Router , useClass : RouterStub } ] ,
26
- schemas : [ NO_ERRORS_SCHEMA ]
35
+ AppComponent ,
36
+ BannerComponent , WelcomeStubComponent ,
37
+ RouterLinkStubDirective , RouterOutletStubComponent
38
+ ]
27
39
} )
28
40
29
41
. compileComponents ( )
30
-
31
42
. then ( ( ) => {
32
43
fixture = TestBed . createComponent ( AppComponent ) ;
33
44
comp = fixture . componentInstance ;
34
45
} ) ;
35
-
36
46
} ) ) ;
37
-
47
+ // #enddocregion setup-stubs, setup-stubs-w-imports
38
48
tests ( ) ;
39
49
} ) ;
40
50
41
- function tests ( ) {
42
-
43
- it ( 'can instantiate it' , ( ) => {
44
- expect ( comp ) . not . toBeNull ( ) ;
45
- } ) ;
46
-
47
- it ( 'can get RouterLinks from template' , ( ) => {
48
- fixture . detectChanges ( ) ;
49
-
50
- const links = fixture . debugElement
51
- // find all elements with an attached FakeRouterLink directive
52
- . queryAll ( By . directive ( RouterLinkDirectiveStub ) )
53
- // use injector to get the RouterLink directive instance attached to each element
54
- . map ( de => de . injector . get ( RouterLinkDirectiveStub ) as RouterLinkDirectiveStub ) ;
55
-
56
- expect ( links . length ) . toBe ( 3 , 'should have 3 links' ) ;
57
- expect ( links [ 0 ] . linkParams ) . toBe ( '/dashboard' , '1st link should go to Dashboard' ) ;
58
- expect ( links [ 1 ] . linkParams ) . toBe ( '/heroes' , '1st link should go to Heroes' ) ;
59
- } ) ;
60
-
61
- it ( 'can click Heroes link in template' , ( ) => {
62
- fixture . detectChanges ( ) ;
63
-
64
- // Heroes RouterLink DebugElement
65
- const heroesLinkDe = fixture . debugElement
66
- . queryAll ( By . directive ( RouterLinkDirectiveStub ) ) [ 1 ] ;
67
-
68
- expect ( heroesLinkDe ) . toBeDefined ( 'should have a 2nd RouterLink' ) ;
69
-
70
- const link = heroesLinkDe . injector . get ( RouterLinkDirectiveStub ) as RouterLinkDirectiveStub ;
71
-
72
- expect ( link . navigatedTo ) . toBeNull ( 'link should not have navigate yet' ) ;
73
-
74
- heroesLinkDe . triggerEventHandler ( 'click' , null ) ;
51
+ //////// Testing w/ NO_ERRORS_SCHEMA //////
52
+ describe ( 'AppComponent & NO_ERRORS_SCHEMA' , ( ) => {
53
+ // #docregion setup-schemas
54
+ beforeEach ( async ( ( ) => {
55
+ TestBed . configureTestingModule ( {
56
+ declarations : [ AppComponent , RouterLinkStubDirective ] ,
57
+ schemas : [ NO_ERRORS_SCHEMA ]
58
+ } )
75
59
76
- fixture . detectChanges ( ) ;
77
- expect ( link . navigatedTo ) . toBe ( '/heroes' ) ;
78
- } ) ;
79
- }
60
+ . compileComponents ( )
61
+ . then ( ( ) => {
62
+ fixture = TestBed . createComponent ( AppComponent ) ;
63
+ comp = fixture . componentInstance ;
64
+ } ) ;
65
+ } ) ) ;
66
+ // #enddocregion setup-schemas
67
+ tests ( ) ;
68
+ } ) ;
80
69
81
70
//////// Testing w/ real root module //////
82
- // Best to avoid
83
71
// Tricky because we are disabling the router and its configuration
72
+ // Better to use RouterTestingModule
84
73
import { AppModule } from './app.module' ;
74
+ import { AppRoutingModule } from './app-routing.module' ;
85
75
86
76
describe ( 'AppComponent & AppModule' , ( ) => {
87
77
88
78
beforeEach ( async ( ( ) => {
89
79
90
80
TestBed . configureTestingModule ( {
91
- imports : [ AppModule ] ,
92
- } )
93
-
94
- . overrideModule ( AppModule , {
95
- // Must get rid of `RouterModule.forRoot` to prevent attempt to configure a router
96
- // Can't remove it because it doesn't have a known type (`forRoot` returns an object)
97
- // therefore, must reset the entire `imports` with just the necessary stuff
98
- set : { imports : [ SharedModule ] }
81
+ imports : [ AppModule ]
99
82
} )
100
83
101
- // Separate override because cannot both `set` and `add/remove` in same override
84
+ // Get rid of app's Router configuration otherwise many failures.
85
+ // Doing so removes Router declarations; add the Router stubs
102
86
. overrideModule ( AppModule , {
87
+ remove : {
88
+ imports : [ AppRoutingModule ]
89
+ } ,
103
90
add : {
104
- declarations : [ RouterLinkDirectiveStub , RouterOutletStubComponent ] ,
105
- providers : [ { provide : Router , useClass : RouterStub } ]
91
+ declarations : [ RouterLinkStubDirective , RouterOutletStubComponent ]
106
92
}
107
93
} )
108
94
@@ -117,3 +103,46 @@ describe('AppComponent & AppModule', () => {
117
103
tests ( ) ;
118
104
} ) ;
119
105
106
+ function tests ( ) {
107
+ let links : RouterLinkStubDirective [ ] ;
108
+ let linkDes : DebugElement [ ] ;
109
+
110
+ // #docregion test-setup
111
+ beforeEach ( ( ) => {
112
+ // trigger initial data binding
113
+ fixture . detectChanges ( ) ;
114
+
115
+ // find DebugElements with an attached RouterLinkStubDirective
116
+ linkDes = fixture . debugElement
117
+ . queryAll ( By . directive ( RouterLinkStubDirective ) ) ;
118
+
119
+ // get the attached link directive instances using the DebugElement injectors
120
+ links = linkDes
121
+ . map ( de => de . injector . get ( RouterLinkStubDirective ) as RouterLinkStubDirective ) ;
122
+ } ) ;
123
+ // #enddocregion test-setup
124
+
125
+ it ( 'can instantiate it' , ( ) => {
126
+ expect ( comp ) . not . toBeNull ( ) ;
127
+ } ) ;
128
+
129
+ // #docregion tests
130
+ it ( 'can get RouterLinks from template' , ( ) => {
131
+ expect ( links . length ) . toBe ( 3 , 'should have 3 links' ) ;
132
+ expect ( links [ 0 ] . linkParams ) . toBe ( '/dashboard' , '1st link should go to Dashboard' ) ;
133
+ expect ( links [ 1 ] . linkParams ) . toBe ( '/heroes' , '1st link should go to Heroes' ) ;
134
+ } ) ;
135
+
136
+ it ( 'can click Heroes link in template' , ( ) => {
137
+ const heroesLinkDe = linkDes [ 1 ] ;
138
+ const heroesLink = links [ 1 ] ;
139
+
140
+ expect ( heroesLink . navigatedTo ) . toBeNull ( 'link should not have navigated yet' ) ;
141
+
142
+ heroesLinkDe . triggerEventHandler ( 'click' , null ) ;
143
+ fixture . detectChanges ( ) ;
144
+
145
+ expect ( heroesLink . navigatedTo ) . toBe ( '/heroes' ) ;
146
+ } ) ;
147
+ // #docregion tests
148
+ }
0 commit comments