-
Notifications
You must be signed in to change notification settings - Fork 1
/
race.service.spec.ts
155 lines (121 loc) · 4.5 KB
/
race.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
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
150
151
152
153
154
155
import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { Subject } from 'rxjs';
import { environment } from '../environments/environment';
import { RaceService } from './race.service';
import { WsService } from './ws.service';
import { RaceModel } from './models/race.model';
import { PonyWithPositionModel } from './models/pony.model';
describe('RaceService', () => {
let raceService: RaceService;
let http: HttpTestingController;
const wsService = jasmine.createSpyObj<WsService>('WsService', ['connect']);
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [{ provide: WsService, useValue: wsService }]
});
raceService = TestBed.inject(RaceService);
http = TestBed.inject(HttpTestingController);
});
afterAll(() => http.verify());
it('should list races', () => {
// fake response
const hardcodedRaces = [{ name: 'Paris' }, { name: 'Tokyo' }, { name: 'Lyon' }] as Array<RaceModel>;
let actualRaces: Array<RaceModel> = [];
raceService.list().subscribe((races: Array<RaceModel>) => (actualRaces = races));
http.expectOne(`${environment.baseUrl}/api/races?status=PENDING`).flush(hardcodedRaces);
expect(actualRaces.length).withContext('The `list` method should return an array of RaceModel wrapped in an Observable').not.toBe(0);
expect(actualRaces).toEqual(hardcodedRaces);
});
it('should get a race', () => {
// fake response
const race = { name: 'Paris' } as RaceModel;
const raceId = 1;
let actualRace: RaceModel | undefined;
raceService.get(raceId).subscribe(fetchedRace => (actualRace = fetchedRace));
http.expectOne(`${environment.baseUrl}/api/races/${raceId}`).flush(race);
expect(actualRace).withContext('The observable must emit the race').toBe(race);
});
it('should bet on a race', () => {
// fake response
const race = { name: 'Paris' } as RaceModel;
const raceId = 1;
const ponyId = 2;
let actualRace: RaceModel | undefined;
raceService.bet(raceId, ponyId).subscribe(fetchedRace => (actualRace = fetchedRace));
const req = http.expectOne({ method: 'POST', url: `${environment.baseUrl}/api/races/${raceId}/bets` });
expect(req.request.body).toEqual({ ponyId });
req.flush(race);
expect(actualRace).withContext('The observable must emit the race').toBe(race);
});
it('should cancel a bet on a race', () => {
const raceId = 1;
let called = false;
raceService.cancelBet(raceId).subscribe(() => (called = true));
http.expectOne({ method: 'DELETE', url: `${environment.baseUrl}/api/races/${raceId}/bets` }).flush(null);
expect(called).toBe(true);
});
it('should return live positions from websockets', () => {
const raceId = 1;
const messages = new Subject<{
status: 'PENDING' | 'RUNNING' | 'FINISHED';
ponies: Array<PonyWithPositionModel>;
}>();
let positions: Array<PonyWithPositionModel> = [];
wsService.connect.and.returnValue(messages);
raceService.live(raceId).subscribe(pos => {
positions = pos;
});
expect(wsService.connect).toHaveBeenCalledWith(`/race/${raceId}`);
messages.next({
status: 'RUNNING',
ponies: [
{
id: 1,
name: 'Superb Runner',
color: 'BLUE',
position: 1
}
]
});
expect(positions.length).toBe(1);
expect(positions[0].position).toBe(1);
messages.next({
status: 'RUNNING',
ponies: [
{
id: 1,
name: 'Superb Runner',
color: 'BLUE',
position: 100
}
]
});
expect(positions.length).toBe(1);
expect(positions[0].position).toBe(100);
messages.next({
status: 'FINISHED',
ponies: [
{
id: 1,
name: 'Superb Runner',
color: 'BLUE',
position: 101
}
]
});
expect(positions.length).toBe(1);
expect(positions[0].position).withContext('The observable should stop emitting if the race status is FINISHED').toBe(100);
});
it('should boost a pony in a race', () => {
const ponyId = 12;
const raceId = 1;
let called = false;
raceService.boost(raceId, ponyId).subscribe(() => (called = true));
const req = http.expectOne({ method: 'POST', url: `${environment.baseUrl}/api/races/${raceId}/boosts` });
expect(req.request.body).toEqual({ ponyId });
req.flush(null);
expect(called).toBe(true);
});
});