Skip to content

Commit 26ca12c

Browse files
committed
complete unit testing
1 parent 32db7d2 commit 26ca12c

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/app/courses/home/async-examples.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {fakeAsync, flush, flushMicrotasks, tick} from '@angular/core/testing';
22
import {of} from 'rxjs';
33
import {delay} from 'rxjs/operators';
44

5-
fdescribe('Async testing examples', () => {
5+
describe('Async testing examples', () => {
66
it('Async test example with Jasmine done()', (done: DoneFn) => {
77
let test = false;
88

src/app/courses/home/home.component.spec.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,37 @@ describe('HomeComponent', () => {
8585
});
8686

8787

88-
it('should display advanced courses when tab clicked', (done: DoneFn) => {
88+
it('should display advanced courses when tab clicked - fakeAsync', fakeAsync(() => {
8989

9090
coursesService.findAllCourses.and.returnValue(of(setupCourses()));
9191
fixture.detectChanges();
9292
const tabs = el.queryAll(By.css('.mat-tab-label'));
9393
click(tabs[1]);
9494
fixture.detectChanges();
95-
setTimeout(() => {
95+
flush();
96+
const cardTitles = el.queryAll(By.css('.mat-tab-body-active .mat-card-title'));
97+
expect(cardTitles.length).toBeGreaterThan(0, 'could not find card titles');
98+
expect(cardTitles[0].nativeElement.textContent).toContain('Angular Security Course');
9699

100+
}));
101+
102+
it('should display advanced courses when tab clicked - waitForAsync', waitForAsync(() => {
103+
104+
coursesService.findAllCourses.and.returnValue(of(setupCourses()));
105+
fixture.detectChanges();
106+
const tabs = el.queryAll(By.css('.mat-tab-label'));
107+
click(tabs[1]);
108+
fixture.detectChanges();
109+
fixture.whenStable().then(() => {
110+
// all async code in this block now completed
111+
console.log('called whenStable();');
97112
const cardTitles = el.queryAll(By.css('.mat-tab-body-active .mat-card-title'));
98113
expect(cardTitles.length).toBeGreaterThan(0, 'could not find card titles');
99114
expect(cardTitles[0].nativeElement.textContent).toContain('Angular Security Course');
100-
done();
101-
}, 500);
102-
});
115+
116+
});
117+
118+
}));
103119

104120
});
105121

0 commit comments

Comments
 (0)