Skip to content

Commit 584e6f9

Browse files
committed
Angular Testing Course
1 parent 65d4be8 commit 584e6f9

File tree

5 files changed

+65
-30
lines changed

5 files changed

+65
-30
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ testem.log
4040
# System Files
4141
.DS_Store
4242
Thumbs.db
43+
tmp.html

src/app/app.component.spec.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {By} from '@angular/platform-browser';
88
describe('AppComponent', () => {
99

1010

11-
beforeEach(async(() => {
11+
beforeEach(() => {
1212
TestBed.configureTestingModule({
1313
imports: [
1414
RouterTestingModule,
@@ -22,36 +22,36 @@ describe('AppComponent', () => {
2222
AppComponent
2323
],
2424
}).compileComponents();
25-
}));
25+
});
2626

2727

2828

29-
it('should create the app', async(() => {
29+
it('should create the app', () => {
3030

3131
const fixture = TestBed.createComponent(AppComponent);
3232
const app = fixture.debugElement.componentInstance;
3333
expect(app).toBeTruthy();
3434

35-
}));
35+
});
3636

3737

3838

39-
it(`should have a title property`, async(() => {
39+
it(`should have a title property`, () => {
4040

4141
const fixture = TestBed.createComponent(AppComponent);
4242
const app = fixture.debugElement.componentInstance;
4343
expect(app.title).toEqual('Angular Testing Course');
4444

45-
}));
45+
});
4646

4747

4848

49-
it('should have a navigation menu', async(() => {
49+
it('should have a navigation menu', () => {
5050

5151
const fixture = TestBed.createComponent(AppComponent);
5252
fixture.detectChanges();
5353
expect(fixture.debugElement.queryAll(By.css('mat-toolbar'))).toBeTruthy();
5454

55-
}));
55+
});
5656

5757
});

src/app/courses/courses-card-list/courses-card-list.component.spec.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ describe('CoursesCardListComponent', () => {
1717
let component:CoursesCardListComponent;
1818
let el: DebugElement;
1919

20-
beforeEach(async(() => {
21-
20+
beforeEach(() => {
2221
TestBed.configureTestingModule({
2322
imports: [
2423
CoursesModule
@@ -29,17 +28,25 @@ describe('CoursesCardListComponent', () => {
2928
component = fixture.debugElement.componentInstance;
3029
el = fixture.debugElement;
3130

32-
}));
31+
});
32+
33+
beforeEach(() => {
34+
fixture = TestBed.createComponent(CoursesCardListComponent);
35+
component = fixture.componentInstance;
36+
el = fixture.debugElement;
37+
fixture.detectChanges();
38+
39+
});
3340

3441

35-
it("should create the component", async(() => {
42+
it("should create the component", () => {
3643

3744
expect(component).toBeTruthy();
3845

39-
}));
46+
});
4047

4148

42-
it("should display the course list", async(() => {
49+
it("should display the course list", () => {
4350

4451
component.courses = setupCourses();
4552

@@ -50,10 +57,10 @@ describe('CoursesCardListComponent', () => {
5057
expect(cards).toBeTruthy("Could not finds cards");
5158
expect(cards.length).toBe(12, "Unexpected number of courses");
5259

53-
}));
60+
});
5461

5562

56-
it("should display the first course", async(() => {
63+
it("should display the first course", () => {
5764

5865
component.courses = setupCourses();
5966

@@ -70,7 +77,7 @@ describe('CoursesCardListComponent', () => {
7077
expect(image.nativeElement.src).toBe(course.iconUrl, "Wrong image found");
7178

7279

73-
}));
80+
});
7481

7582

7683
});

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

+38-13
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@ import {NoopAnimationsModule} from '@angular/platform-browser/animations';
1515

1616

1717

18-
fdescribe('HomeComponent', () => {
18+
describe('HomeComponent', () => {
1919

2020
let fixture: ComponentFixture<HomeComponent>;
2121
let component:HomeComponent;
2222
let el: DebugElement;
23-
let courseServicesSpy: any;
2423

25-
courseServicesSpy = jasmine.createSpyObj('CoursesService', ['findAllCourses']);
24+
beforeEach((() => {
2625

27-
beforeEach(async(() => {
26+
const courseServicesSpy = jasmine.createSpyObj('CoursesService', ['findAllCourses']);
2827

2928
TestBed.configureTestingModule({
3029
imports: [
@@ -35,36 +34,62 @@ fdescribe('HomeComponent', () => {
3534
{provide: CoursesService, useValue: courseServicesSpy}
3635
]
3736
}).compileComponents();
37+
}));
38+
3839

40+
beforeEach(() => {
3941
fixture = TestBed.createComponent(HomeComponent);
40-
component = fixture.debugElement.componentInstance;
42+
component = fixture.componentInstance;
4143
el = fixture.debugElement;
44+
});
4245

4346

44-
}));
45-
46-
47-
it("should create the component", async(() => {
47+
it("should create the component", () => {
4848

4949
expect(component).toBeTruthy();
5050

51-
}));
51+
});
5252

5353

54-
it("should display only beginner courses", async(() => {
54+
it("should display only beginner courses", () => {
5555

56-
courseServicesSpy.findAllCourses.and.returnValue(of(setupCourses().filter(course => course.category == 'BEGINNER')));
56+
TestBed.get(CoursesService).findAllCourses.and.returnValue(
57+
of(setupCourses().filter(course => course.category == 'BEGINNER')));
5758

5859
fixture.detectChanges();
5960

6061
const tabs = el.queryAll(By.css('.mat-tab-label'));
6162

6263
expect(tabs.length).toBe(1, "Unexpected number of tabs found");
6364

64-
}));
65+
});
66+
67+
68+
it("should display only advanced courses", () => {
69+
70+
TestBed.get(CoursesService).findAllCourses.and.returnValue(
71+
of(setupCourses().filter(course => course.category == 'ADVANCED')));
72+
73+
fixture.detectChanges();
74+
75+
const tabs = el.queryAll(By.css('.mat-tab-label'));
76+
77+
expect(tabs.length).toBe(1, "Unexpected number of tabs found ");
78+
79+
});
80+
6581

82+
it("should display both tabs", () => {
83+
84+
TestBed.get(CoursesService).findAllCourses.and.returnValue(of(setupCourses()));
85+
86+
fixture.detectChanges();
87+
88+
const tabs = el.queryAll(By.css('.mat-tab-label'));
6689

90+
expect(tabs.length).toBe(2, "Expected to find 2 tabs");
6791

92+
});
6893

6994
});
7095

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

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

2323
ngOnInit() {
2424

25+
console.log("called Home ngOnInit()");
26+
2527
this.reloadCourses();
2628

2729
}

0 commit comments

Comments
 (0)