|
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'; |
2 | 3 | import { HeroComponent } from '../hero.component/hero.component'; |
3 | 4 | import { HeroesComponent } from './heroes.component'; |
4 | 5 | import { HeroService } from '../hero.service/hero.service'; |
5 | 6 | import { Router } from '@angular/router'; |
6 | | -import { By } from '@angular/platform-browser'; |
7 | 7 |
|
8 | | -describe('HeroDetailComponent (shallow tests)', () => { |
| 8 | +describe('HeroDetailComponent (deep tests)', () => { |
9 | 9 | let fixture: ComponentFixture<HeroesComponent>; |
10 | | - let component: HeroesComponent; |
11 | | - let element; |
12 | | - let heroes = [ |
| 10 | + const HEROES = [ |
13 | 11 | {id: 3, name: 'Magneta', strength: 4}, |
14 | 12 | {id: 4, name: 'Dynama', strength: 2} |
15 | 13 | ]; |
16 | 14 |
|
17 | | - beforeEach(() => { |
| 15 | + beforeEach(fakeAsync(() => { |
18 | 16 |
|
19 | 17 | const mockHeroService = { |
20 | | - getHeros: () => Promise.resolve(heroes), |
| 18 | + getHeroes: () => Promise.resolve(HEROES), |
21 | 19 | delete: () => Promise.resolve() |
22 | 20 | }; |
23 | 21 |
|
24 | 22 | const mockRouter = {}; |
25 | 23 |
|
26 | 24 | TestBed.configureTestingModule({ |
27 | | - imports: [ |
28 | | - ], |
29 | 25 | declarations: [ |
30 | 26 | HeroComponent, |
31 | 27 | HeroesComponent |
32 | 28 | ], |
33 | 29 | providers: [ |
34 | 30 | { provide: HeroService, useValue: mockHeroService }, |
35 | 31 | { provide: Router, useValue: mockRouter }, |
36 | | - ], |
37 | | - schemas: [ |
38 | | - // NO_ERRORS_SCHEMA will hide that angular doesn't know about ngModel |
39 | 32 | ] |
40 | 33 | }); |
41 | 34 |
|
42 | 35 | 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 | | - }); |
53 | 36 |
|
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 | + })); |
55 | 44 |
|
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); |
57 | 48 |
|
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 | + }); |
65 | 51 |
|
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 | + }); |
69 | 58 |
|
| 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 | +}); |
0 commit comments