Skip to content

Commit 5080e66

Browse files
committed
State after section 4
1 parent 25b2dcd commit 5080e66

File tree

2 files changed

+168
-7
lines changed

2 files changed

+168
-7
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { fakeAsync, flush, tick, flushMicrotasks } from "@angular/core/testing";
2+
import { of } from "rxjs";
3+
import { delay } from "rxjs/operators";
4+
5+
describe("Async Testing Examples", () => {
6+
7+
it("Asynchronous test example with Jasmine done()", async function (done: DoneFn) {
8+
9+
let test = false;
10+
11+
setTimeout(() => {
12+
console.log("running assertions");
13+
test = true;
14+
expect(test).toBeTruthy();
15+
16+
done();
17+
}, 1000);
18+
});
19+
20+
it("Asynchronous test example -- setTimeout", fakeAsync(function () {
21+
// fakeAsync creates a async zone that observes many browser integrated async operations
22+
// including setTimeout and setInterval. When the function is left, the zone is
23+
// closed. The zone can see which operations still haven't finished and will throw
24+
// an error, if the zone was left with some timers left.
25+
26+
let test = false
27+
28+
setTimeout(() => {
29+
console.log("running assertions setTimeout()");
30+
test = true;
31+
}, 1000);
32+
33+
flush();
34+
expect(test).toBeTrue()
35+
}));
36+
37+
it("Asynchronous test example -- plain Promise", fakeAsync(function () {
38+
let test = false;
39+
40+
console.log("Createing promise");
41+
42+
Promise.resolve().then(() => {
43+
console.log("Promise evaluated successfullly");
44+
test = true;
45+
});
46+
47+
flushMicrotasks();
48+
49+
console.log("Running assertions.");
50+
expect(test).toBeTrue();
51+
}));
52+
53+
it("Asynchronous test example -- Promises + setTimeout()", fakeAsync(function () {
54+
let counter = 0;
55+
56+
Promise.resolve()
57+
.then(() => new Promise(resolve => {
58+
counter += 10;
59+
60+
setTimeout(() => {
61+
counter += 1;
62+
resolve();
63+
}, 1000);
64+
}))
65+
.then(() => {
66+
counter += 10;
67+
})
68+
69+
flushMicrotasks()
70+
expect(counter).toBe(10);
71+
72+
tick(600);
73+
expect(counter).toBe(10);
74+
75+
flush();
76+
expect(counter).toBe(21);
77+
}));
78+
79+
it("Asynchronous test example -- immediate Observable", function () {
80+
let test = false;
81+
82+
console.log("Creating Observable");
83+
84+
const test$ = of(test);
85+
86+
test$.subscribe(() => {
87+
test = true;
88+
});
89+
90+
console.log("Running test assertions");
91+
expect(test).toBeTrue();
92+
});
93+
94+
95+
it("Asynchronous test example -- Observables", fakeAsync(function () {
96+
let test = false;
97+
98+
console.log("Creating Observable");
99+
100+
const test$ = of(test).pipe(delay(1000));
101+
102+
test$.subscribe(() => {
103+
test = true;
104+
});
105+
106+
tick(1000000);
107+
// flushMicrotasks();
108+
// flush();
109+
110+
console.log("Running test assertions");
111+
expect(test).toBeTrue();
112+
}));
113+
114+
})

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

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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';
22
import { CoursesModule } from '../courses.module';
33
import { DebugElement } from '@angular/core';
44

@@ -12,6 +12,7 @@ import { By } from '@angular/platform-browser';
1212
import { of } from 'rxjs';
1313
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
1414
import { click } from '../common/test-utils';
15+
import { fake } from 'cypress/types/sinon';
1516

1617

1718

@@ -95,25 +96,71 @@ describe('HomeComponent', () => {
9596

9697
fixture.detectChanges();
9798

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+
98113
const [beginnerTab, advancedTab] = el.queryAll(By.css(".mat-tab-label"));
99-
click(advancedTab)
114+
click(advancedTab);
100115

101116
fixture.detectChanges();
117+
flush();
102118

103-
const cardTitles = el.queryAll(By.css(".mat-card-title"));
119+
const cardTitles = el.queryAll(By.css(".mat-tab-body-active .mat-card-title"));
104120
expect(cardTitles).toBeTruthy();
105121
expect(cardTitles[0].nativeElement.textContent).toContain("Angular Security Course");
106-
});
122+
}));
107123

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()));
108129

109-
it("should display advanced courses when tab clicked", () => {
130+
fixture.detectChanges();
110131

111-
coursesService.findAllCourses.and.returnValue(of(advancedCourses));
132+
const [beginnerTab, advancedTab] = el.queryAll(By.css(".mat-tab-label"));
133+
click(advancedTab);
112134

113135
fixture.detectChanges();
136+
fixture.whenStable().then(() => {
137+
console.log("called whenStable()");
114138

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()));
116150

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");
117164
});
118165

119166
});

0 commit comments

Comments
 (0)