-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapiClient.spec.js
More file actions
264 lines (234 loc) · 7.03 KB
/
apiClient.spec.js
File metadata and controls
264 lines (234 loc) · 7.03 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import sinon from 'sinon';
import {expect} from 'chai';
import ApiClient from '../../lib/apiClient';
import configureMockStore from 'redux-mock-store';
import {makeStore} from '../spec_helpers';
import fetchMock from 'fetch-mock';
const BASE_URL = 'http://fullstackreact.com';
const baseOpts = {
_debug: false,
baseUrl: BASE_URL,
appendExt: false, // optional
headers: {
'X-Requested-With': 'spec'
}
}
describe('ApiClient', () => {
let client, state;
let helpers;
beforeEach(() => {
fetchMock
.mock(`${BASE_URL}/foo`, {
status: 200,
body: {msg: 'world'}
})
});
afterEach(() => fetchMock.restore());
beforeEach(() => {
state = {};
client = new ApiClient(baseOpts, () => state);
});
describe('option parsing', () => {
const makeOptionParsingTest = (key, val) => {
// common tests for each option
it(`accepts ${key} in defaultOpts as a value`, () => {
let res = client._parseOpt(key, {}, { [key]: val });
expect(res).to.eql(val)
});
it(`accepts ${key} in instance options as a value`, () => {
let res = client._parseOpt(key, { [key]: val });
expect(res).to.eql(val)
});
it(`accepts ${key} as a function`, () => {
let res = client._parseOpt(key, { [key]: () => val });
expect(res).to.eql(val)
});
it(`accepts ${key} as false (nullify)`, () => {
let res = client._parseOpt(key, { [key]: false });
expect(res).to.be.null;
});
}
// Test for appendPath
makeOptionParsingTest('appendPath', `/${Math.random(0, 20)}`)
makeOptionParsingTest('appendExt', 'json')
})
describe('get', () => {
it('defines GET as a function', () => {
expect(typeof client.get).to.eql('function');
})
it('runs a request', (done) => {
client.get({url: `${BASE_URL}/foo`})
.then((resp) => {
expect(resp.msg).to.eql('world')
done();
}).catch(done);
});
it('accepts a string as a path', done => {
client.get('/foo')
.then((resp) => {
expect(resp.msg).to.eql('world');
done();
}).catch(done);
})
it('accepts a path (appended to the baseUrl)', done => {
client.get({path: '/foo'})
.then((resp) => {
expect(resp.msg).to.eql('world');
done();
}).catch(done);
});
it('accepts appendPath', done => {
fetchMock.mock(`${BASE_URL}/foo/yellow`, '{}')
client.get({path: '/foo', appendPath: '/yellow'})
.then(() => done()).catch(done);
});
});
describe('post', () => {
it('defines POST as a function', () => {
expect(typeof client.post).to.eql('function');
});
it('sends `data` along with the request', (done) => {
fetchMock.post(`${BASE_URL}/foo`, {
msg: 'world'
})
client.post({
path: '/foo',
data: {msg: 'hello'}
}).then((data) => {
expect(data).to.eql({msg: 'world'})
done();
}).catch(done);
})
})
describe('error handling', () => {
let client;
const generateError = (status, msg={}, options={}) => {
return fetchMock.mock(`${BASE_URL}/err`, (reqUrl, reqOpts) => {
return {
status,
body: JSON.stringify(msg)
}
})
}
beforeEach(() => {
client = new ApiClient(baseOpts, () => state);
});
it('responds with the status code', (done) => {
let p = generateError(500, {msg: 'blah'});
client.get({path: '/err'})
.catch((err) => {
expect(err.status).to.equal(500);
done();
})
});
it('responds with the error messsage', (done) => {
generateError(400, {msg: 'error error'});
client.get({path: '/err'})
.catch((err) => {
err.body.then((json) => {
expect(json.msg).to.eql('error error')
done();
})
})
})
})
describe('request transforms', () => {
let transform = (state, opts) => req => {
req.headers['X-Name'] = 'Ari';
return req;
}
beforeEach(() => {
fetchMock.mock(`${BASE_URL}/name`, (reqUrl, reqOpts) => {
return {
status: 200,
body: JSON.stringify(reqOpts)
}
});
client = new ApiClient(baseOpts, () => state);
});
it('can accept a single requestTransform', () => {
baseOpts.requestTransforms = [transform];
client = new ApiClient(baseOpts, () => state);
client.get({path: '/name'})
.then((json) => {
expect(res.headers['X-Name']).to.eql('Ari')
});
});
it('accepts multiple requestTransforms', () => {
client = new ApiClient(baseOpts, () => state);
client.get({path: '/name', requestTransforms: [transform]})
});
});
describe('response transforms', () => {
let time, msg;
let transform = (state, opts) => res => {
res.headers.set('X-Response-Time', time);
return res;
}
let jsonTransform = (state, opts) => (res) => {
let time = res.headers.get('X-Response-Time');
return res.json()
.then(json => ({...json, time}))
}
beforeEach(() => {
time = new Date()
msg = 'hello world';
fetchMock.mock(`${BASE_URL}/response/time`, {
status: 200,
body: JSON.stringify({time, msg})
});
client = new ApiClient(baseOpts, () => state);
});
it('returns parsed JSON by default without responseTransforms', (done) => {
client = new ApiClient(baseOpts, () => state)
.get({path: '/response/time'})
.then(json => {
expect(json.msg).to.eql(msg);
done();
}).catch(done)
});
it('can accept a single responseTransforms', (done) => {
baseOpts.responseTransforms = [transform];
client = new ApiClient(baseOpts, () => state);
client.get({path: '/response/time'})
.then((res) => {
expect(res.headers.get('X-Response-Time')).to.eql(time)
done();
}).catch(done)
});
it('accepts multiple responseTransforms', (done) => {
client = new ApiClient(baseOpts, () => state);
client.get({path: '/response/time',
responseTransforms: [transform, jsonTransform]})
.then((json) => {
expect(json.time).to.eql(time);
done();
}).catch(done)
});
});
describe('onError', () => {
const generateError = (status, msg={}, options={}) => {
return fetchMock.mock(`${BASE_URL}/errcatch`, (reqUrl, reqOpts) => {
return {
status,
body: JSON.stringify(msg)
}
})
}
it('accept onError option', (done) => {
let p = generateError(401, {msg: 'blah'});
baseOpts.onError = [(getState, opts) => error => {
error.someProp = 'processed';
return error;
}];
client = new ApiClient(baseOpts, () => state);
client.get({path: '/errcatch'})
.catch((promise) => {
promise.then((error) => {
expect(error.someProp).to.equal('processed');
done();
});
})
})
});
})