Skip to content

Commit

Permalink
Angular Testing Course
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed May 24, 2019
1 parent feb11d5 commit 7516e52
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/app/courses/home/async-examples.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {of} from 'rxjs';
import {delay} from 'rxjs/operators';


fdescribe('Async Testing Examples', () => {
describe('Async Testing Examples', () => {

it('Asynchronous test example with Jasmine done()', (done: DoneFn) => {

Expand Down
47 changes: 35 additions & 12 deletions src/app/courses/home/home.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {async, ComponentFixture, fakeAsync, flush, flushMicrotasks, TestBed} from '@angular/core/testing';
import {async, ComponentFixture, fakeAsync, flush, flushMicrotasks, TestBed, tick} from '@angular/core/testing';
import {CoursesModule} from '../courses.module';
import {DebugElement} from '@angular/core';

Expand Down Expand Up @@ -29,6 +29,8 @@ describe('HomeComponent', () => {
const advancedCourses = setupCourses()
.filter(course => course.category == 'ADVANCED');



beforeEach(async(() => {

const coursesServiceSpy = jasmine.createSpyObj('CoursesService', ['findAllCourses'])
Expand All @@ -49,11 +51,8 @@ describe('HomeComponent', () => {
coursesService = TestBed.get(CoursesService);
});


}));



it("should create the component", () => {

expect(component).toBeTruthy();
Expand Down Expand Up @@ -100,7 +99,7 @@ describe('HomeComponent', () => {
});


it("should display advanced courses when tab clicked", (done: DoneFn) => {
it("should display advanced courses when tab clicked - fakeAsync", fakeAsync(() => {

coursesService.findAllCourses.and.returnValue(of(setupCourses()));

Expand All @@ -112,19 +111,43 @@ describe('HomeComponent', () => {

fixture.detectChanges();

setTimeout(() => {
flush();

const cardTitles = el.queryAll(By.css('.mat-card-title'));
const cardTitles = el.queryAll(By.css('.mat-card-title'));

expect(cardTitles.length).toBeGreaterThan(0,"Could not find card titles");
expect(cardTitles.length).toBeGreaterThan(0,"Could not find card titles");

expect(cardTitles[0].nativeElement.textContent).toContain("Angular Security Course");
expect(cardTitles[0].nativeElement.textContent).toContain("Angular Security Course");

done();
}));

}, 500);

});
it("should display advanced courses when tab clicked - async", async(() => {

coursesService.findAllCourses.and.returnValue(of(setupCourses()));

fixture.detectChanges();

const tabs = el.queryAll(By.css(".mat-tab-label"));

click(tabs[1]);

fixture.detectChanges();

fixture.whenStable().then(() => {

console.log("called whenStable() ");

const cardTitles = el.queryAll(By.css('.mat-card-title'));

expect(cardTitles.length).toBeGreaterThan(0,"Could not find card titles");

expect(cardTitles[0].nativeElement.textContent).toContain("Angular Security Course");

});

}));


});

Expand Down

0 comments on commit 7516e52

Please sign in to comment.