diff --git a/aio/content/examples/toh-pt6/src/app/dashboard/dashboard.component.spec.ts b/aio/content/examples/toh-pt6/src/app/dashboard/dashboard.component.spec.ts index fea6bfb4db..3926c21e9f 100644 --- a/aio/content/examples/toh-pt6/src/app/dashboard/dashboard.component.spec.ts +++ b/aio/content/examples/toh-pt6/src/app/dashboard/dashboard.component.spec.ts @@ -1,16 +1,36 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { DashboardComponent } from './dashboard.component'; +import { HeroSearchComponent } from '../hero-search/hero-search.component'; + +import { RouterTestingModule } from '@angular/router/testing'; +import { of } from 'rxjs'; +import { HEROES } from '../mock-heroes'; +import { HeroService } from '../hero.service'; describe('DashboardComponent', () => { let component: DashboardComponent; let fixture: ComponentFixture; + let heroService; + let getHeroesSpy; beforeEach(async(() => { + heroService = jasmine.createSpyObj('HeroService', ['getHeroes']); + getHeroesSpy = heroService.getHeroes.and.returnValue( of(HEROES) ); TestBed.configureTestingModule({ - declarations: [ DashboardComponent ] + declarations: [ + DashboardComponent, + HeroSearchComponent + ], + imports: [ + RouterTestingModule.withRoutes([]) + ], + providers: [ + { provide: HeroService, useValue: heroService } + ] }) .compileComponents(); + })); beforeEach(() => { @@ -22,4 +42,17 @@ describe('DashboardComponent', () => { it('should be created', () => { expect(component).toBeTruthy(); }); + + it('should display "Top Heroes" as headline', () => { + expect(fixture.nativeElement.querySelector('h3').textContent).toEqual('Top Heroes'); + }); + + it('should call heroService', async(() => { + expect(getHeroesSpy.calls.any()).toBe(true); + })); + + it('should display 4 links', async(() => { + expect(fixture.nativeElement.querySelectorAll('a').length).toEqual(4); + })); + }); diff --git a/aio/content/examples/toh-pt6/src/app/hero-search/hero-search.component.spec.ts b/aio/content/examples/toh-pt6/src/app/hero-search/hero-search.component.spec.ts index 901bb7f2ab..8414f1c223 100644 --- a/aio/content/examples/toh-pt6/src/app/hero-search/hero-search.component.spec.ts +++ b/aio/content/examples/toh-pt6/src/app/hero-search/hero-search.component.spec.ts @@ -1,14 +1,18 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing'; +import { RouterTestingModule } from '@angular/router/testing'; +import { HttpClientTestingModule } from '@angular/common/http/testing'; import { HeroSearchComponent } from './hero-search.component'; + describe('HeroSearchComponent', () => { let component: HeroSearchComponent; let fixture: ComponentFixture; beforeEach(async(() => { TestBed.configureTestingModule({ - declarations: [ HeroSearchComponent ] + declarations: [ HeroSearchComponent ], + imports: [RouterTestingModule.withRoutes([]), HttpClientTestingModule] }) .compileComponents(); })); diff --git a/aio/content/examples/toh-pt6/src/app/heroes/heroes.component.spec.ts b/aio/content/examples/toh-pt6/src/app/heroes/heroes.component.spec.ts index 9c3b1c4d9f..6991d69010 100644 --- a/aio/content/examples/toh-pt6/src/app/heroes/heroes.component.spec.ts +++ b/aio/content/examples/toh-pt6/src/app/heroes/heroes.component.spec.ts @@ -1,6 +1,7 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing'; - +import { RouterTestingModule } from '@angular/router/testing'; import { HeroesComponent } from './heroes.component'; +import { HttpClientTestingModule } from '@angular/common/http/testing'; describe('HeroesComponent', () => { let component: HeroesComponent; @@ -8,7 +9,8 @@ describe('HeroesComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ - declarations: [ HeroesComponent ] + declarations: [ HeroesComponent ], + imports: [RouterTestingModule.withRoutes([]), HttpClientTestingModule], }) .compileComponents(); }));