-
Notifications
You must be signed in to change notification settings - Fork 5
/
hero-detail.component.spec.ts
149 lines (147 loc) · 5.38 KB
/
hero-detail.component.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';
import { HeroService } from '../hero.service';
import { HeroDetailComponent } from './hero-detail.component';
import { By } from '@angular/platform-browser';
import { of } from 'rxjs';
import { FormsModule } from '@angular/forms';
describe('HeroDetailComponent', () => {
let component: HeroDetailComponent;
let fixture: ComponentFixture<HeroDetailComponent>;
beforeEach(() => {
const activatedRouteStub = () => ({
snapshot: { paramMap: { get: () => ({}) } },
});
const locationStub = () => ({ back: () => ({}) });
const heroServiceStub = () => ({
getHero: (id) => ({
subscribe: (f) => f({}),
}),
updateHero: (hero) => ({ subscribe: (f) => f({}) }),
});
TestBed.configureTestingModule({
schemas: [NO_ERRORS_SCHEMA],
imports: [FormsModule],
declarations: [HeroDetailComponent],
providers: [
{ provide: ActivatedRoute, useFactory: activatedRouteStub },
{ provide: Location, useFactory: locationStub },
{ provide: HeroService, useFactory: heroServiceStub },
],
});
fixture = TestBed.createComponent(HeroDetailComponent);
component = fixture.componentInstance;
});
it('can load instance', () => {
expect(component).toBeTruthy();
});
describe('ngOnInit', () => {
it('makes expected calls', () => {
spyOn(component, 'getHero').and.callThrough();
component.ngOnInit();
expect(component.getHero).toHaveBeenCalled();
});
});
describe('getHero', () => {
it('makes expected calls', () => {
const heroServiceStub: HeroService = TestBed.inject(HeroService);
spyOn(heroServiceStub, 'getHero').and.callThrough();
component.getHero();
expect(heroServiceStub.getHero).toHaveBeenCalled();
});
});
describe('goBack', () => {
it('makes expected calls', () => {
const locationStub: Location = TestBed.inject(Location);
spyOn(locationStub, 'back').and.callThrough();
component.goBack();
expect(locationStub.back).toHaveBeenCalled();
});
});
describe('save', () => {
it('makes expected calls', () => {
const heroServiceStub: HeroService = TestBed.inject(HeroService);
spyOn(component, 'goBack').and.callThrough();
spyOn(heroServiceStub, 'updateHero').and.callThrough();
component.save();
expect(component.goBack).toHaveBeenCalled();
expect(heroServiceStub.updateHero).toHaveBeenCalled();
});
});
describe('Without a hero', () => {
it("Doesn't display anything", () => {
const heroServiceStub = TestBed.inject(HeroService);
spyOn(heroServiceStub, 'getHero').and.returnValue(of(undefined));
fixture.detectChanges();
const anyDiv = fixture.debugElement.query(By.css('div'));
expect(anyDiv).toBeFalsy();
});
});
describe('With hero', () => {
beforeEach(() => {
const heroServiceStub = TestBed.inject(HeroService);
spyOn(heroServiceStub, 'getHero').and.returnValue(
of({
id: 123,
name: 'Alan',
})
);
fixture.detectChanges();
});
it('Displays content when initialized with a hero', () => {
const anyDiv = fixture.debugElement.query(By.css('div'));
expect(anyDiv).toBeTruthy();
});
it('Has header with hero name in uppercase', () => {
const header: HTMLHeadingElement = fixture.debugElement.query(
By.css('h2')
).nativeElement;
expect(header.textContent).toContain('ALAN Details');
});
it('Shows hero id', () => {
const div: HTMLDivElement = fixture.debugElement.query(
By.css('div div') // first inner div
).nativeElement;
expect(div.textContent).toContain('id: 123');
});
it('Has input box with the name', async () => {
await fixture.whenStable();
const input: HTMLInputElement = fixture.debugElement.query(
By.css('input')
).nativeElement;
expect(input.value).toBe('Alan');
});
it('Calls location.back() when go back button is clicked', () => {
const locationStub = TestBed.inject(Location);
spyOn(locationStub, 'back');
const button: HTMLButtonElement = fixture.debugElement.query(
By.css('button') // first button
).nativeElement;
button.click();
expect(locationStub.back).toHaveBeenCalled();
});
it('Updates hero property when user types on the input', () => {
const input: HTMLInputElement = fixture.debugElement.query(
By.css('input')
).nativeElement;
input.value = 'ABC';
input.dispatchEvent(new Event('input'));
fixture.detectChanges();
expect(component.hero.name).toBe('ABC');
});
it('Updates hero then goes back when save button is clicked', () => {
const heroServiceStub = TestBed.inject(HeroService);
spyOn(heroServiceStub, 'updateHero').and.returnValue(of(undefined));
const locationStub = TestBed.inject(Location);
spyOn(locationStub, 'back');
const button: HTMLButtonElement = fixture.debugElement.queryAll(
By.css('button')
)[1].nativeElement; // second button
button.click();
expect(heroServiceStub.updateHero).toHaveBeenCalledWith(component.hero);
expect(locationStub.back).toHaveBeenCalled();
});
});
});