-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathname.service.spec.ts
58 lines (46 loc) · 1.65 KB
/
name.service.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
// name.service.spec.ts
import { HttpClient } from '@angular/common/http';
import { cold } from 'jasmine-marbles';
import { NameService } from './name.service';
describe('NameService', () => {
let service: NameService;
let httpClientSpy: jasmine.SpyObj<HttpClient>;
beforeEach(() => {
httpClientSpy = jasmine.createSpyObj('HttpClient', ['get']);
service = new NameService(httpClientSpy);
});
describe('loadName', () => {
it('should call get on HttpClient with correct URL', () => {
// Setting up get spy
httpClientSpy.get.and.returnValue(cold('a|', {a: null}));
// Calling loadName
service.loadName();
// Checking get call
expect(httpClientSpy.get).toHaveBeenCalledWith('name URL');
});
it('should emit new name on name$', () => {
// Defining marbles
const getSpyMarbles = '-a|';
const expectedMarbles = 'bc';
// Setting up get spy
httpClientSpy.get.and.returnValue(cold(getSpyMarbles, {a: 'name'}));
// Calling loadName
service.loadName();
// Checking get call
const expected = cold(expectedMarbles, {b: null, c: 'name'});
expect(service.name$()).toBeObservable(expected);
});
it('should emit nothing on name$ if an error occurs', () => {
// Defining marbles
const getSpyMarbles = '-#|';
const expectedMarbles = 'a';
// Setting up get spy
httpClientSpy.get.and.returnValue(cold(getSpyMarbles));
// Calling loadName
service.loadName();
// Checking get call
const expected = cold(expectedMarbles, {a: null});
expect(service.name$()).toBeObservable(expected);
});
});
});