|
1 | 1 | import {
|
2 |
| - async, |
3 | 2 | ComponentFixture,
|
4 | 3 | fakeAsync,
|
5 | 4 | flush,
|
6 |
| - flushMicrotasks, |
7 | 5 | TestBed,
|
8 | 6 | waitForAsync,
|
9 | 7 | } from "@angular/core/testing";
|
10 | 8 | import { CoursesModule } from "../courses.module";
|
11 | 9 | import { DebugElement } from "@angular/core";
|
12 | 10 |
|
13 | 11 | import { HomeComponent } from "./home.component";
|
14 |
| -import { |
15 |
| - HttpClientTestingModule, |
16 |
| - HttpTestingController, |
17 |
| -} from "@angular/common/http/testing"; |
18 | 12 | import { CoursesService } from "../services/courses.service";
|
19 |
| -import { HttpClient } from "@angular/common/http"; |
20 |
| -import { COURSES } from "../../../../server/db-data"; |
21 | 13 | import { setupCourses } from "../common/setup-test-data";
|
22 | 14 | import { By } from "@angular/platform-browser";
|
23 | 15 | import { of } from "rxjs";
|
@@ -84,20 +76,37 @@ describe("HomeComponent", () => {
|
84 | 76 | expect(tabs.length).toBe(2, "Unexpected number of tabs found");
|
85 | 77 | });
|
86 | 78 |
|
87 |
| - it("should display advanced courses when tab clicked", (done: DoneFn) => { |
| 79 | + it("should display advanced courses when tab clicked - fakeAysnc", fakeAsync(() => { |
88 | 80 | coursesService.findAllCourses.and.returnValue(of(setupCourses()));
|
89 | 81 | fixture.detectChanges();
|
90 | 82 | const tabs = el.queryAll(By.css(".mat-tab-label"));
|
91 | 83 | click(tabs[1]);
|
| 84 | + fixture.detectChanges(); |
| 85 | + flush(); |
| 86 | + const cardTitles = el.queryAll( |
| 87 | + By.css(".mat-tab-body-active .mat-card-title") |
| 88 | + ); |
| 89 | + expect(cardTitles.length).toBeGreaterThan(0); |
| 90 | + expect(cardTitles[0].nativeElement.textContent).toContain( |
| 91 | + "Angular Security Course" |
| 92 | + ); |
| 93 | + })); |
92 | 94 |
|
93 |
| - setTimeout(() => { |
94 |
| - const cardTitles = el.queryAll(By.css(".mat-card-title")); |
| 95 | + it("should display advanced courses when tab clicked - waitForAsync", waitForAsync(() => { |
| 96 | + coursesService.findAllCourses.and.returnValue(of(setupCourses())); |
| 97 | + fixture.detectChanges(); |
| 98 | + const tabs = el.queryAll(By.css(".mat-tab-label")); |
| 99 | + click(tabs[1]); |
| 100 | + fixture.detectChanges(); |
| 101 | + // This is required using wait for async |
| 102 | + fixture.whenStable().then(() => { |
| 103 | + const cardTitles = el.queryAll( |
| 104 | + By.css(".mat-tab-body-active .mat-card-title") |
| 105 | + ); |
95 | 106 | expect(cardTitles.length).toBeGreaterThan(0);
|
96 | 107 | expect(cardTitles[0].nativeElement.textContent).toContain(
|
97 | 108 | "Angular Security Course"
|
98 | 109 | );
|
99 |
| - |
100 |
| - done(); |
101 |
| - }, 500); |
102 |
| - }); |
| 110 | + }); |
| 111 | + })); |
103 | 112 | });
|
0 commit comments