Skip to content

Commit 4607eda

Browse files
committed
End home testing
1 parent 325059d commit 4607eda

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
import {
2-
async,
32
ComponentFixture,
43
fakeAsync,
54
flush,
6-
flushMicrotasks,
75
TestBed,
86
waitForAsync,
97
} from "@angular/core/testing";
108
import { CoursesModule } from "../courses.module";
119
import { DebugElement } from "@angular/core";
1210

1311
import { HomeComponent } from "./home.component";
14-
import {
15-
HttpClientTestingModule,
16-
HttpTestingController,
17-
} from "@angular/common/http/testing";
1812
import { CoursesService } from "../services/courses.service";
19-
import { HttpClient } from "@angular/common/http";
20-
import { COURSES } from "../../../../server/db-data";
2113
import { setupCourses } from "../common/setup-test-data";
2214
import { By } from "@angular/platform-browser";
2315
import { of } from "rxjs";
@@ -84,20 +76,37 @@ describe("HomeComponent", () => {
8476
expect(tabs.length).toBe(2, "Unexpected number of tabs found");
8577
});
8678

87-
it("should display advanced courses when tab clicked", (done: DoneFn) => {
79+
it("should display advanced courses when tab clicked - fakeAysnc", fakeAsync(() => {
8880
coursesService.findAllCourses.and.returnValue(of(setupCourses()));
8981
fixture.detectChanges();
9082
const tabs = el.queryAll(By.css(".mat-tab-label"));
9183
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+
}));
9294

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+
);
95106
expect(cardTitles.length).toBeGreaterThan(0);
96107
expect(cardTitles[0].nativeElement.textContent).toContain(
97108
"Angular Security Course"
98109
);
99-
100-
done();
101-
}, 500);
102-
});
110+
});
111+
}));
103112
});

0 commit comments

Comments
 (0)