Skip to content

Commit dba9d31

Browse files
committed
component testing complete
1 parent 8ebc8e9 commit dba9d31

File tree

3 files changed

+107
-66
lines changed

3 files changed

+107
-66
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
describe
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import {async, ComponentFixture, fakeAsync, flush, flushMicrotasks, TestBed, waitForAsync} from '@angular/core/testing';
2+
import {CoursesModule} from '../courses.module';
3+
import {DebugElement} from '@angular/core';
4+
5+
import {HomeComponent} from './home.component';
6+
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
7+
import {CoursesService} from '../services/courses.service';
8+
import {HttpClient} from '@angular/common/http';
9+
import {COURSES} from '../../../../server/db-data';
10+
import {setupCourses} from '../common/setup-test-data';
11+
import {By} from '@angular/platform-browser';
12+
import {of} from 'rxjs';
13+
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
14+
import {click} from '../common/test-utils';
15+
16+
17+
18+
19+
describe('HomeComponent', () => {
20+
21+
let fixture: ComponentFixture<HomeComponent>;
22+
let component: HomeComponent;
23+
let el: DebugElement;
24+
let coursesService: any;
25+
26+
const beginnerCourses = setupCourses().filter(course => course.category === 'BEGINNER');
27+
const advancedCourses = setupCourses().filter(course => course.category === 'ADVANCED');
28+
29+
beforeEach(waitForAsync( ((() => {
30+
const coursesServiceSpy = jasmine.createSpyObj('CoursesService', ['findAllCourses']);
31+
TestBed.configureTestingModule({
32+
imports: [
33+
CoursesModule,
34+
NoopAnimationsModule,
35+
],
36+
providers: [
37+
{provide: CoursesService, useValue: coursesServiceSpy}
38+
]
39+
}).compileComponents().then(() => {
40+
fixture = TestBed.createComponent(HomeComponent);
41+
component = fixture.componentInstance;
42+
el = fixture.debugElement;
43+
coursesService = TestBed.get(CoursesService);
44+
});
45+
}))));
46+
47+
it('should create the component', () => {
48+
expect(component).toBeTruthy();
49+
50+
});
51+
52+
53+
it('should display only beginner courses', () => {
54+
55+
coursesService.findAllCourses.and.returnValue(of(beginnerCourses));
56+
fixture.detectChanges();
57+
58+
const tabs = el.queryAll(By.css('.mat-tab-label'));
59+
60+
expect(tabs.length).toBe(1, 'Unexpected number of tabs found');
61+
62+
});
63+
64+
65+
it('should display only advanced courses', () => {
66+
67+
coursesService.findAllCourses.and.returnValue(of(advancedCourses));
68+
fixture.detectChanges();
69+
70+
const tabs = el.queryAll(By.css('.mat-tab-label'));
71+
72+
expect(tabs.length).toBe(1, 'Unexpected number of tabs found');
73+
});
74+
75+
76+
it('should display both tabs', () => {
77+
78+
coursesService.findAllCourses.and.returnValue(of(setupCourses()));
79+
fixture.detectChanges();
80+
81+
const tabs = el.queryAll(By.css('.mat-tab-label'));
82+
83+
expect(tabs.length).toBe(2, 'Unexpected number of tabs found');
84+
85+
});
86+
87+
88+
it('should display advanced courses when tab clicked', (done: DoneFn) => {
89+
90+
coursesService.findAllCourses.and.returnValue(of(setupCourses()));
91+
fixture.detectChanges();
92+
const tabs = el.queryAll(By.css('.mat-tab-label'));
93+
click(tabs[1]);
94+
fixture.detectChanges();
95+
setTimeout(() => {
96+
97+
const cardTitles = el.queryAll(By.css('.mat-tab-body-active .mat-card-title'));
98+
expect(cardTitles.length).toBeGreaterThan(0, 'could not find card titles');
99+
expect(cardTitles[0].nativeElement.textContent).toContain('Angular Security Course');
100+
done();
101+
}, 500);
102+
});
103+
104+
});
105+
106+

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

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)