Skip to content

Commit 07bc04c

Browse files
committed
Angular Testing Course
1 parent 07b1731 commit 07bc04c

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

src/app/courses/common/test-utils.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {DebugElement} from '@angular/core';
2+
3+
export const ButtonClickEvents = {
4+
left: { button: 0 },
5+
right: { button: 2 }
6+
};
7+
8+
9+
export function click(el: DebugElement | HTMLElement, eventObj: any = ButtonClickEvents.left): void {
10+
if (el instanceof HTMLElement) {
11+
el.click();
12+
} else {
13+
el.triggerEventHandler('click', eventObj);
14+
}
15+
}

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

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
1+
import {async, ComponentFixture, fakeAsync, flush, flushMicrotasks, TestBed} from '@angular/core/testing';
22
import {CoursesModule} from '../courses.module';
33
import {DebugElement} from '@angular/core';
44

@@ -11,6 +11,7 @@ import {setupCourses} from '../common/setup-test-data';
1111
import {By} from '@angular/platform-browser';
1212
import {of} from 'rxjs';
1313
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
14+
import {click} from '../common/test-utils';
1415

1516

1617

@@ -93,6 +94,33 @@ describe('HomeComponent', () => {
9394

9495
});
9596

97+
98+
it("should display advanced courses when tab clicked", async(() => {
99+
100+
TestBed.get(CoursesService).findAllCourses.and.returnValue(of(setupCourses()));
101+
102+
fixture.detectChanges();
103+
104+
const tabs = el.queryAll(By.css('.mat-tab-label'));
105+
106+
click(tabs[1]);
107+
108+
fixture.detectChanges();
109+
110+
fixture.whenStable().then(() => {
111+
112+
const cardTitles = el.queryAll(By.css('.mat-card-title'));
113+
114+
expect(cardTitles.length).toBeGreaterThan(0,"Could not find card titles");
115+
116+
expect(cardTitles[0].nativeElement.textContent).toContain("Angular Security Course");
117+
118+
119+
});
120+
121+
122+
}));
123+
96124
});
97125

98126

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

-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ export class HomeComponent implements OnInit {
2222

2323
ngOnInit() {
2424

25-
console.log("called Home ngOnInit()");
26-
2725
this.reloadCourses();
2826

2927
}

0 commit comments

Comments
 (0)