Skip to content

Commit 7516e52

Browse files
author
Your Name
committed
Angular Testing Course
1 parent feb11d5 commit 7516e52

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {of} from 'rxjs';
33
import {delay} from 'rxjs/operators';
44

55

6-
fdescribe('Async Testing Examples', () => {
6+
describe('Async Testing Examples', () => {
77

88
it('Asynchronous test example with Jasmine done()', (done: DoneFn) => {
99

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

+35-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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';
22
import {CoursesModule} from '../courses.module';
33
import {DebugElement} from '@angular/core';
44

@@ -29,6 +29,8 @@ describe('HomeComponent', () => {
2929
const advancedCourses = setupCourses()
3030
.filter(course => course.category == 'ADVANCED');
3131

32+
33+
3234
beforeEach(async(() => {
3335

3436
const coursesServiceSpy = jasmine.createSpyObj('CoursesService', ['findAllCourses'])
@@ -49,11 +51,8 @@ describe('HomeComponent', () => {
4951
coursesService = TestBed.get(CoursesService);
5052
});
5153

52-
5354
}));
5455

55-
56-
5756
it("should create the component", () => {
5857

5958
expect(component).toBeTruthy();
@@ -100,7 +99,7 @@ describe('HomeComponent', () => {
10099
});
101100

102101

103-
it("should display advanced courses when tab clicked", (done: DoneFn) => {
102+
it("should display advanced courses when tab clicked - fakeAsync", fakeAsync(() => {
104103

105104
coursesService.findAllCourses.and.returnValue(of(setupCourses()));
106105

@@ -112,19 +111,43 @@ describe('HomeComponent', () => {
112111

113112
fixture.detectChanges();
114113

115-
setTimeout(() => {
114+
flush();
116115

117-
const cardTitles = el.queryAll(By.css('.mat-card-title'));
116+
const cardTitles = el.queryAll(By.css('.mat-card-title'));
118117

119-
expect(cardTitles.length).toBeGreaterThan(0,"Could not find card titles");
118+
expect(cardTitles.length).toBeGreaterThan(0,"Could not find card titles");
120119

121-
expect(cardTitles[0].nativeElement.textContent).toContain("Angular Security Course");
120+
expect(cardTitles[0].nativeElement.textContent).toContain("Angular Security Course");
122121

123-
done();
122+
}));
124123

125-
}, 500);
126124

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+
128151

129152
});
130153

0 commit comments

Comments
 (0)