Skip to content

Commit f0db817

Browse files
heroes: fixup HeroesComponent deep test
1 parent 19bf43d commit f0db817

3 files changed

Lines changed: 31 additions & 38 deletions

File tree

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,64 @@
1-
import { TestBed, fakeAsync, tick, async, ComponentFixture, inject } from '@angular/core/testing';
1+
import { TestBed, ComponentFixture, fakeAsync, tick } from '@angular/core/testing';
2+
import { By } from '@angular/platform-browser';
23
import { HeroComponent } from '../hero.component/hero.component';
34
import { HeroesComponent } from './heroes.component';
45
import { HeroService } from '../hero.service/hero.service';
56
import { Router } from '@angular/router';
6-
import { By } from '@angular/platform-browser';
77

8-
describe('HeroDetailComponent (shallow tests)', () => {
8+
describe('HeroDetailComponent (deep tests)', () => {
99
let fixture: ComponentFixture<HeroesComponent>;
10-
let component: HeroesComponent;
11-
let element;
12-
let heroes = [
10+
const HEROES = [
1311
{id: 3, name: 'Magneta', strength: 4},
1412
{id: 4, name: 'Dynama', strength: 2}
1513
];
1614

17-
beforeEach(() => {
15+
beforeEach(fakeAsync(() => {
1816

1917
const mockHeroService = {
20-
getHeros: () => Promise.resolve(heroes),
18+
getHeroes: () => Promise.resolve(HEROES),
2119
delete: () => Promise.resolve()
2220
};
2321

2422
const mockRouter = {};
2523

2624
TestBed.configureTestingModule({
27-
imports: [
28-
],
2925
declarations: [
3026
HeroComponent,
3127
HeroesComponent
3228
],
3329
providers: [
3430
{ provide: HeroService, useValue: mockHeroService },
3531
{ provide: Router, useValue: mockRouter },
36-
],
37-
schemas: [
38-
// NO_ERRORS_SCHEMA will hide that angular doesn't know about ngModel
3932
]
4033
});
4134

4235
fixture = TestBed.createComponent(HeroesComponent);
43-
component = fixture.componentInstance;
44-
element = fixture.nativeElement;
45-
});
46-
47-
describe('initial display', () => {
48-
it('should show the hero id and name for each hero', fakeAsync(() => {
49-
console.log(2.5);
50-
expect(3).toBe(3);
51-
}))
52-
});
5336

54-
it('should select the hero when the hero is clicked');
37+
// Trigger ngOnInit()
38+
fixture.detectChanges();
39+
// Resolve the promise from heroesService.getHeroes()
40+
tick();
41+
// Render the hero components
42+
fixture.detectChanges();
43+
}));
5544

56-
it('should delete the hero when the delete button is clicked');
45+
it('should show the hero id and name for each hero', () => {
46+
const heroComponents = fixture.debugElement.queryAll(By.directive(HeroComponent));
47+
expect(heroComponents.length).toEqual(HEROES.length);
5748

58-
});
59-
60-
function createEvent(eventName: string, bubbles = false, cancelable = false) {
61-
let evt = document.createEvent('CustomEvent'); // MUST be 'CustomEvent'
62-
evt.initCustomEvent(eventName, bubbles, cancelable, null);
63-
return evt;
64-
}
49+
expect(heroComponents[0].nativeElement.textContent).toContain('Magneta');
50+
});
6551

66-
function getHeadingText(fixture) {
67-
return fixture.debugElement.query(By.css('h2')).nativeElement.textContent;
68-
}
52+
it('should delete the hero when the HeroComponent outputs the "delete" event', () => {
53+
spyOn(fixture.componentInstance, 'delete');
54+
const heroComponents = fixture.debugElement.queryAll(By.directive(HeroComponent));
55+
heroComponents[0].triggerEventHandler('delete', null);
56+
expect(fixture.componentInstance.delete).toHaveBeenCalledWith(HEROES[0]);
57+
});
6958

59+
it('should select the hero when the hero is clicked', () => {
60+
const heroContainers = fixture.debugElement.queryAll(By.css('li'));
61+
heroContainers[0].triggerEventHandler('click', null);
62+
expect(fixture.componentInstance.selectedHero).toBe(HEROES[0]);
63+
});
64+
});

tour-of-heroes/src/app/heroes.component/heroes.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ <h2>
1919
</div>
2020

2121

22-
<!--
22+
<!--
2323
Copyright 2016 Google Inc. All Rights Reserved.
2424
Use of this source code is governed by an MIT-style license that
2525
can be found in the LICENSE file at http://angular.io/license

tour-of-heroes/src/app/heroes.component/heroes.component.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ export class HeroesComponent implements OnInit {
1515
constructor(
1616
private heroService: HeroService,
1717
private router: Router) {
18-
console.log(1.5);
1918
}
2019

2120
getHeroes(): void {
22-
console.log(2)
2321
this.heroService
2422
.getHeroes()
2523
.then(heroes => this.heroes = heroes);

0 commit comments

Comments
 (0)