1
- import { async , ComponentFixture , fakeAsync , flush , flushMicrotasks , TestBed } from '@angular/core/testing' ;
1
+ import { async , ComponentFixture , fakeAsync , flush , flushMicrotasks , TestBed , tick } from '@angular/core/testing' ;
2
2
import { CoursesModule } from '../courses.module' ;
3
3
import { DebugElement } from '@angular/core' ;
4
4
@@ -29,6 +29,8 @@ describe('HomeComponent', () => {
29
29
const advancedCourses = setupCourses ( )
30
30
. filter ( course => course . category == 'ADVANCED' ) ;
31
31
32
+
33
+
32
34
beforeEach ( async ( ( ) => {
33
35
34
36
const coursesServiceSpy = jasmine . createSpyObj ( 'CoursesService' , [ 'findAllCourses' ] )
@@ -49,11 +51,8 @@ describe('HomeComponent', () => {
49
51
coursesService = TestBed . get ( CoursesService ) ;
50
52
} ) ;
51
53
52
-
53
54
} ) ) ;
54
55
55
-
56
-
57
56
it ( "should create the component" , ( ) => {
58
57
59
58
expect ( component ) . toBeTruthy ( ) ;
@@ -100,7 +99,7 @@ describe('HomeComponent', () => {
100
99
} ) ;
101
100
102
101
103
- it ( "should display advanced courses when tab clicked" , ( done : DoneFn ) => {
102
+ it ( "should display advanced courses when tab clicked - fakeAsync " , fakeAsync ( ( ) => {
104
103
105
104
coursesService . findAllCourses . and . returnValue ( of ( setupCourses ( ) ) ) ;
106
105
@@ -112,19 +111,43 @@ describe('HomeComponent', () => {
112
111
113
112
fixture . detectChanges ( ) ;
114
113
115
- setTimeout ( ( ) => {
114
+ flush ( ) ;
116
115
117
- const cardTitles = el . queryAll ( By . css ( '.mat-card-title' ) ) ;
116
+ const cardTitles = el . queryAll ( By . css ( '.mat-card-title' ) ) ;
118
117
119
- expect ( cardTitles . length ) . toBeGreaterThan ( 0 , "Could not find card titles" ) ;
118
+ expect ( cardTitles . length ) . toBeGreaterThan ( 0 , "Could not find card titles" ) ;
120
119
121
- expect ( cardTitles [ 0 ] . nativeElement . textContent ) . toContain ( "Angular Security Course" ) ;
120
+ expect ( cardTitles [ 0 ] . nativeElement . textContent ) . toContain ( "Angular Security Course" ) ;
122
121
123
- done ( ) ;
122
+ } ) ) ;
124
123
125
- } , 500 ) ;
126
124
127
- } ) ;
125
+ it ( "should display advanced courses when tab clicked - async" , async ( ( ) => {
126
+
127
+ coursesService . findAllCourses . and . returnValue ( of ( setupCourses ( ) ) ) ;
128
+
129
+ fixture . detectChanges ( ) ;
130
+
131
+ const tabs = el . queryAll ( By . css ( ".mat-tab-label" ) ) ;
132
+
133
+ click ( tabs [ 1 ] ) ;
134
+
135
+ fixture . detectChanges ( ) ;
136
+
137
+ fixture . whenStable ( ) . then ( ( ) => {
138
+
139
+ console . log ( "called whenStable() " ) ;
140
+
141
+ const cardTitles = el . queryAll ( By . css ( '.mat-card-title' ) ) ;
142
+
143
+ expect ( cardTitles . length ) . toBeGreaterThan ( 0 , "Could not find card titles" ) ;
144
+
145
+ expect ( cardTitles [ 0 ] . nativeElement . textContent ) . toContain ( "Angular Security Course" ) ;
146
+
147
+ } ) ;
148
+
149
+ } ) ) ;
150
+
128
151
129
152
} ) ;
130
153
0 commit comments