1
- import { async , ComponentFixture , fakeAsync , flush , flushMicrotasks , TestBed , waitForAsync } from '@angular/core/testing' ;
1
+ import { async , ComponentFixture , fakeAsync , flush , flushMicrotasks , TestBed , tick , waitForAsync } from '@angular/core/testing' ;
2
2
import { CoursesModule } from '../courses.module' ;
3
3
import { DebugElement } from '@angular/core' ;
4
4
@@ -12,6 +12,7 @@ import { By } from '@angular/platform-browser';
12
12
import { of } from 'rxjs' ;
13
13
import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
14
14
import { click } from '../common/test-utils' ;
15
+ import { fake } from 'cypress/types/sinon' ;
15
16
16
17
17
18
@@ -95,25 +96,71 @@ describe('HomeComponent', () => {
95
96
96
97
fixture . detectChanges ( ) ;
97
98
99
+ const tabs = el . queryAll ( By . css ( ".mat-tab-label" ) ) ;
100
+
101
+ expect ( tabs . length ) . toBe ( 2 , "Unexpected number of tabs" ) ;
102
+ } ) ;
103
+
104
+
105
+ fit ( "should display advanced courses when tab clicked -- fakeAsync" , fakeAsync ( ( ) => {
106
+ // `of` creates an observable that **immediately** emits its value. All of this is done
107
+ // synchronously, the emission and the completion. This is, this spec can be completely
108
+ // synchronous!!!
109
+ coursesService . findAllCourses . and . returnValue ( of ( setupCourses ( ) ) ) ;
110
+
111
+ fixture . detectChanges ( ) ;
112
+
98
113
const [ beginnerTab , advancedTab ] = el . queryAll ( By . css ( ".mat-tab-label" ) ) ;
99
- click ( advancedTab )
114
+ click ( advancedTab ) ;
100
115
101
116
fixture . detectChanges ( ) ;
117
+ flush ( ) ;
102
118
103
- const cardTitles = el . queryAll ( By . css ( ".mat-card-title" ) ) ;
119
+ const cardTitles = el . queryAll ( By . css ( ".mat-tab-body-active .mat- card-title" ) ) ;
104
120
expect ( cardTitles ) . toBeTruthy ( ) ;
105
121
expect ( cardTitles [ 0 ] . nativeElement . textContent ) . toContain ( "Angular Security Course" ) ;
106
- } ) ;
122
+ } ) ) ;
107
123
124
+ fit ( "should display advanced courses when tab clicked -- waitForAsync" , waitForAsync ( ( ) => {
125
+ // `of` creates an observable that **immediately** emits its value. All of this is done
126
+ // synchronously, the emission and the completion. This is, this spec can be completely
127
+ // synchronous!!!
128
+ coursesService . findAllCourses . and . returnValue ( of ( setupCourses ( ) ) ) ;
108
129
109
- it ( "should display advanced courses when tab clicked" , ( ) => {
130
+ fixture . detectChanges ( ) ;
110
131
111
- coursesService . findAllCourses . and . returnValue ( of ( advancedCourses ) ) ;
132
+ const [ beginnerTab , advancedTab ] = el . queryAll ( By . css ( ".mat-tab-label" ) ) ;
133
+ click ( advancedTab ) ;
112
134
113
135
fixture . detectChanges ( ) ;
136
+ fixture . whenStable ( ) . then ( ( ) => {
137
+ console . log ( "called whenStable()" ) ;
114
138
115
- const tabs = el . queryAll ( By . css ( ".mat-tab-label" ) ) ;
139
+ const cardTitles = el . queryAll ( By . css ( ".mat-tab-body-active .mat-card-title" ) ) ;
140
+ expect ( cardTitles ) . toBeTruthy ( ) ;
141
+ expect ( cardTitles [ 0 ] . nativeElement . textContent ) . toContain ( "Angular Security Course" ) ;
142
+ } ) ;
143
+ } ) ) ;
144
+
145
+ fit ( "should display advanced courses when tab clicked -- Typescript async" , async ( ) => {
146
+ // `of` creates an observable that **immediately** emits its value. All of this is done
147
+ // synchronously, the emission and the completion. This is, this spec can be completely
148
+ // synchronous!!!
149
+ coursesService . findAllCourses . and . returnValue ( of ( setupCourses ( ) ) ) ;
116
150
151
+ fixture . detectChanges ( ) ;
152
+
153
+ const [ beginnerTab , advancedTab ] = el . queryAll ( By . css ( ".mat-tab-label" ) ) ;
154
+ click ( advancedTab ) ;
155
+
156
+ fixture . detectChanges ( ) ;
157
+ await fixture . whenStable ( ) ;
158
+
159
+ console . log ( "called whenStable()" ) ;
160
+
161
+ const cardTitles = el . queryAll ( By . css ( ".mat-tab-body-active .mat-card-title" ) ) ;
162
+ expect ( cardTitles ) . toBeTruthy ( ) ;
163
+ expect ( cardTitles [ 0 ] . nativeElement . textContent ) . toContain ( "Angular Security Course" ) ;
117
164
} ) ;
118
165
119
166
} ) ;
0 commit comments