Skip to content

Commit 65d4be8

Browse files
committed
Angular Testing Course
1 parent 40b9f48 commit 65d4be8

File tree

4 files changed

+96
-12
lines changed

4 files changed

+96
-12
lines changed

src/app/app.module.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ import {CoursesModule} from './courses/courses.module';
3737
BrowserModule,
3838
BrowserAnimationsModule,
3939
HttpClientModule,
40-
CoursesModule
40+
MatToolbarModule,
41+
MatButtonModule,
42+
CoursesModule,
43+
AppRoutingModule
4144
],
4245
providers: [
4346
],

src/app/courses/courses.module.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,17 @@ import {CourseResolver} from './services/course.resolver';
4444
MatTabsModule,
4545
MatSidenavModule,
4646
MatListModule,
47-
MatToolbarModule,
4847
MatInputModule,
4948
MatTableModule,
5049
MatPaginatorModule,
5150
MatSortModule,
5251
MatProgressSpinnerModule,
5352
MatDialogModule,
54-
AppRoutingModule,
5553
MatSelectModule,
5654
MatDatepickerModule,
5755
MatMomentDateModule,
58-
ReactiveFormsModule
56+
ReactiveFormsModule,
57+
AppRoutingModule
5958
],
6059
exports: [
6160
HomeComponent,

src/app/courses/home/home.component.html

+19-8
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,35 @@ <h3>All Courses</h3>
55

66
<mat-tab-group>
77

8-
<mat-tab label="Beginners">
98

10-
<courses-card-list (courseEdited)="reloadCourses()"
11-
[courses]="beginnerCourses$ | async">
9+
<ng-container *ngIf="(beginnerCourses$ | async) as beginnerCourses">
1210

13-
</courses-card-list>
11+
<mat-tab label="Beginners" *ngIf="beginnerCourses?.length > 0">
12+
13+
<courses-card-list (courseEdited)="reloadCourses()"
14+
[courses]="beginnerCourses">
15+
16+
</courses-card-list>
1417

1518
</mat-tab>
1619

17-
<mat-tab label="Advanced">
20+
</ng-container>
21+
22+
23+
<ng-container *ngIf="(advancedCourses$ | async) as advancedCourses">
1824

19-
<courses-card-list (courseEdited)="reloadCourses()"
20-
[courses]="advancedCourses$ | async"
25+
<mat-tab label="Advanced" *ngIf="advancedCourses?.length > 0">
2126

22-
></courses-card-list>
27+
<courses-card-list (courseEdited)="reloadCourses()"
28+
[courses]="advancedCourses">
29+
30+
31+
</courses-card-list>
2332

2433
</mat-tab>
2534

35+
</ng-container>
36+
2637
</mat-tab-group>
2738

2839
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import {async, ComponentFixture, TestBed} 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+
15+
16+
17+
18+
fdescribe('HomeComponent', () => {
19+
20+
let fixture: ComponentFixture<HomeComponent>;
21+
let component:HomeComponent;
22+
let el: DebugElement;
23+
let courseServicesSpy: any;
24+
25+
courseServicesSpy = jasmine.createSpyObj('CoursesService', ['findAllCourses']);
26+
27+
beforeEach(async(() => {
28+
29+
TestBed.configureTestingModule({
30+
imports: [
31+
CoursesModule,
32+
NoopAnimationsModule
33+
],
34+
providers: [
35+
{provide: CoursesService, useValue: courseServicesSpy}
36+
]
37+
}).compileComponents();
38+
39+
fixture = TestBed.createComponent(HomeComponent);
40+
component = fixture.debugElement.componentInstance;
41+
el = fixture.debugElement;
42+
43+
44+
}));
45+
46+
47+
it("should create the component", async(() => {
48+
49+
expect(component).toBeTruthy();
50+
51+
}));
52+
53+
54+
it("should display only beginner courses", async(() => {
55+
56+
courseServicesSpy.findAllCourses.and.returnValue(of(setupCourses().filter(course => course.category == 'BEGINNER')));
57+
58+
fixture.detectChanges();
59+
60+
const tabs = el.queryAll(By.css('.mat-tab-label'));
61+
62+
expect(tabs.length).toBe(1, "Unexpected number of tabs found");
63+
64+
}));
65+
66+
67+
68+
69+
});
70+
71+

0 commit comments

Comments
 (0)