-
Notifications
You must be signed in to change notification settings - Fork 20
/
bot-http-data-promise.service.spec.ts
154 lines (129 loc) · 5.91 KB
/
bot-http-data-promise.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
import {MockBackend, MockConnection} from "@angular/http/testing";
import {HttpModule, Http, XHRBackend, Response, ResponseOptions} from "@angular/http";
import {async, inject, TestBed} from "@angular/core/testing";
import {Bot} from "./bot.model";
import {BotHttpDataPromiseService as BotDataService} from "./bot-http-data-promise.service";
/**
* Tests the Bot HTTP Data Service (Promise flavour) using a mocked HTTP backend.
*
* @author gazbert
*/
describe('BotHttpDataPromiseService tests using TestBed + Mock HTTP backend', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [HttpModule],
providers: [
BotDataService,
{provide: XHRBackend, useClass: MockBackend}
]
})
.compileComponents().then(() => {/*done*/});
}));
it('should instantiate implementation of BotDataService when injected',
inject([BotDataService], (service: BotDataService) => {
expect(service instanceof BotDataService).toBe(true);
}));
it('should instantiate service with "new"', inject([Http], (http: Http) => {
expect(http).not.toBeNull('http should be provided');
let service = new BotDataService(http);
expect(service instanceof BotDataService).toBe(true,
'new service should be instance of BotDataService');
}));
it('should provide the MockBackend as XHRBackend',
inject([XHRBackend], (backend: MockBackend) => {
expect(backend).not.toBeNull('MockBackend backend should be provided');
}));
describe('when getBots() operation called', () => {
let backend: MockBackend;
let service: BotDataService;
let fakeBots: Bot[];
let response: Response;
beforeEach(inject([Http, XHRBackend], (http: Http, be: MockBackend) => {
backend = be;
service = new BotDataService(http);
fakeBots = makeBotData();
let options = new ResponseOptions({status: 200, body: {data: fakeBots}});
response = new Response(options);
}));
it('should return 3 Bots ', async(inject([], () => {
backend.connections.subscribe((c: MockConnection) => c.mockRespond(response));
service.getBots()
.then(bots => {
expect(bots.length).toBe(fakeBots.length,
'should have returned 3 Bots');
// paranoia!
expect(bots[0].id).toBe(1);
expect(bots[1].id).toBe(2);
expect(bots[2].id).toBe(3);
});
})));
it('should handle returning no Bots', async(inject([], () => {
let resp = new Response(new ResponseOptions({status: 200, body: {data: []}}));
backend.connections.subscribe((c: MockConnection) => c.mockRespond(resp));
service.getBots()
.then(bots => expect(bots.length).toBe(0, 'should have no Bots'));
})));
});
describe('when getBot() operation called with \'gdax\'', () => {
let backend: MockBackend;
let service: BotDataService;
let fakeBots: Bot[];
let response: Response;
const GDAX_BOT = 1;
beforeEach(inject([Http, XHRBackend], (http: Http, be: MockBackend) => {
backend = be;
service = new BotDataService(http);
fakeBots = makeBotData();
let options = new ResponseOptions({status: 200, body: {data: fakeBots[GDAX_BOT]}});
response = new Response(options);
}));
it('should return GDAX Bot ', async(inject([], () => {
backend.connections.subscribe((c: MockConnection) => c.mockRespond(response));
service.getBot(2)
.then(bots => {
expect(bots.id).toBe(2);
expect(bots.name).toBe('GDAX');
expect(bots.status).toBe('Running');
});
})));
it('should handle returning no Bot', async(inject([], () => {
let resp = new Response(new ResponseOptions({status: 200, body: {data: []}}));
backend.connections.subscribe((c: MockConnection) => c.mockRespond(resp));
service.getBot(100) // unknown id
.then(bot => expect(bot.id).not.toBeDefined('should have no Bot'));
})));
});
describe('when update() operation called for Bitstamp', () => {
let backend: MockBackend;
let service: BotDataService;
let response: Response;
let udpatedBot: Bot;
beforeEach(inject([Http, XHRBackend], (http: Http, be: MockBackend) => {
udpatedBot = new Bot(1, 'BitstampV2', 'Stopped');
backend = be;
service = new BotDataService(http);
let options = new ResponseOptions({status: 200, body: {data: udpatedBot}});
response = new Response(options);
}));
it('should return updated Bitstamp Bot on success', async(inject([], () => {
backend.connections.subscribe((c: MockConnection) => c.mockRespond(response));
service.update(udpatedBot)
.then(bot => {
expect(bot.id).toBe(1);
expect(bot.name).toBe('BitstampV2');
expect(bot.status).toBe('Stopped');
});
})));
it('should NOT return Bot for 401 response', async(inject([], () => {
let resp = new Response(new ResponseOptions({status: 401, body: {data: ['Bad request - unknown id']}}));
backend.connections.subscribe((c: MockConnection) => c.mockRespond(resp));
service.update(udpatedBot)
.then(bot => expect(bot.id).not.toBeDefined('should not have Bot'));
})));
});
});
const makeBotData = () => [
new Bot(1, 'Bitstamp', 'Running'),
new Bot(2, 'GDAX', 'Running'),
new Bot(3, 'Gemini', 'Stopped'),
] as Bot[];