Skip to content

Commit 5215e95

Browse files
author
John Conway
committed
Tests for getRequest. Changed to nock for mocking api calls
1 parent ac11abf commit 5215e95

File tree

6 files changed

+104
-119
lines changed

6 files changed

+104
-119
lines changed

src/client/__mocks__/getRequest.js

Lines changed: 0 additions & 89 deletions
This file was deleted.

src/client/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const client = {
5858
ok: response.ok,
5959
data: response.response,
6060
status: response.status,
61-
statusText: response.response.message || response.message || null,
61+
statusText: (response.response || {}).message || response.message || null,
6262
rawResponse: response,
6363
links,
6464
};

src/client/client.spec.js

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import client from './client';
2-
// Require get request import for mocks to work
3-
// eslint-disable-next-line no-unused-vars
4-
import getRequest from './getRequest';
2+
import nock from 'nock';
53

6-
// SEE __mocks__/getRequest for which endpoint paths result in what types of
7-
// responses from the getRequest().toPromise() method.
8-
jest.mock('./getRequest');
4+
// make sure no requests are actually sent
5+
nock.disableNetConnect();
96

107
describe('raw client', () => {
118
it('calls getRequest and returns data', async () => {
12-
const response = await client.get('foo');
9+
nock('http://localhost')
10+
.get('/foo')
11+
.reply(200, { bar: 'baz' });
12+
const response = await client.get('http://localhost/foo');
1313

1414
expect(response.data).toEqual({ bar: 'baz' });
1515
expect(response.links).toEqual({
@@ -21,45 +21,63 @@ describe('raw client', () => {
2121
});
2222

2323
it('responds with an error response when when an error from the server occurs', async () => {
24-
const response = await client.get('/404');
24+
nock('http://localhost')
25+
.get('/404')
26+
.reply(404, { message: 'Not Found' });
27+
const response = await client.get('http://localhost/404');
2528
expect(response.ok).toBe(false);
2629
expect(response.data).toEqual({ message: 'Not Found' });
2730
expect(response.status).toEqual(404);
2831
expect(response.statusText).toEqual('Not Found');
2932
});
3033

31-
it('Has no statusText when no message is in the response', async () => {
32-
const response = await client.get('/500');
34+
it('Has statusText set to ajax error messsage when no message is in the response', async () => {
35+
nock('http://localhost')
36+
.get('/500')
37+
.reply(500);
38+
const response = await client.get('http://localhost/500');
3339
expect(response.ok).toBe(false);
34-
expect(response.data).toEqual([]);
40+
expect(response.data).toEqual(null);
3541
expect(response.status).toEqual(500);
36-
expect(response.statusText).toEqual(null);
37-
});
38-
39-
it('rejects when when an error occurs in the client', async () => {
40-
expect(client.get('/client_error')).rejects.toThrow();
42+
expect(response.statusText).toEqual('ajax error 500');
4143
});
4244

4345
describe('links', () => {
4446
it('returns next link', async () => {
47+
nock('http://localhost')
48+
.get('/link/next')
49+
.reply(200, {}, { Link: '<https://example.com/foo?page=7>; rel="next"' });
50+
4551
const response = await client.get('/link/next');
4652

4753
expect(response.links.next).toEqual('https://example.com/foo?page=7');
4854
});
4955

5056
it('returns previous link', async () => {
57+
nock('http://localhost')
58+
.get('/link/prev')
59+
.reply(200, {}, { Link: '<https://example.com/foo?page=5>; rel="prev"' });
60+
5161
const response = await client.get('/link/prev');
5262

5363
expect(response.links.prev).toEqual('https://example.com/foo?page=5');
5464
});
5565

5666
it('returns first link', async () => {
67+
nock('http://localhost')
68+
.get('/link/first')
69+
.reply(200, {}, { Link: '<https://example.com/foo?page=1>; rel="first"' });
70+
5771
const response = await client.get('/link/first');
5872

5973
expect(response.links.first).toEqual('https://example.com/foo?page=1');
6074
});
6175

6276
it('returns last link', async () => {
77+
nock('http://localhost')
78+
.get('/link/last')
79+
.reply(200, {}, { Link: '<https://example.com/foo?page=10>; rel="last"' });
80+
6381
const response = await client.get('/link/last');
6482

6583
expect(response.links.last).toEqual('https://example.com/foo?page=10');

src/client/createClient.spec.js

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import createClient from './createClient';
2-
// Require get request import for mocks to work
3-
// eslint-disable-next-line no-unused-vars
4-
import getRequest from './getRequest';
2+
import nock from 'nock';
53

6-
// SEE __mocks__/getRequest for which endpoint paths result in what types of
7-
// responses from the getRequest().toPromise() method.
8-
jest.mock('./getRequest');
4+
nock.disableNetConnect();
95

106
describe('createClient', () => {
117
it('intializes', () => {
@@ -30,29 +26,63 @@ describe('createClient', () => {
3026

3127
describe('get()', () => {
3228
it('createdClient getRequest creates full url from relative endpoint', async () => {
29+
nock('https://example.com:443')
30+
.defaultReplyHeaders({ 'access-control-allow-origin': '*' })
31+
.get('/api/v2/foo')
32+
.reply(200);
33+
3334
const client = createClient('https://example.com/api/v2');
3435
const response = await client.get('foo');
3536

36-
expect(response.data).toEqual({ url: 'https://example.com/api/v2/foo' });
37+
expect(response.rawResponse.xhr.responseURL).toEqual('https://example.com/api/v2/foo');
3738
});
3839

3940
it('does not double leading /', async () => {
41+
nock('https://example.com:443')
42+
.defaultReplyHeaders({ 'access-control-allow-origin': '*' })
43+
.get('/api/v2/foo')
44+
.reply(200, { url: 'https://example.com/api/v2/foo' });
45+
4046
const client = createClient('https://example.com/api/v2');
4147
const response = await client.get('/foo');
4248

43-
expect(response.data).toEqual({ url: 'https://example.com/api/v2/foo' });
49+
expect(response.rawResponse.xhr.responseURL).toEqual('https://example.com/api/v2/foo');
4450
});
4551

4652
it('does not modify full urls', async () => {
53+
const gitHubScope = nock('https://github.com')
54+
.defaultReplyHeaders({ 'access-control-allow-origin': '*' })
55+
.get('/users')
56+
.reply(200, { url: 'https://github.com/users' });
57+
58+
const exampleScope = nock('https://example.com')
59+
.defaultReplyHeaders({ 'access-control-allow-origin': '*' })
60+
.get('/api/v2/users')
61+
.reply(404, { url: 'https://exmaple.com/api/v2/users' });
62+
4763
const client = createClient('https://example.com/api/v2');
4864
const response = await client.get('https://github.com/users');
4965

66+
expect(gitHubScope.isDone()).toBe(true);
67+
expect(exampleScope.isDone()).toBe(false);
68+
expect(response.ok).toBe(true);
69+
expect(response.status).toEqual(200);
5070
expect(response.data).toEqual({ url: 'https://github.com/users' });
5171
});
5272

5373
it('rejects when when an error occurs', async () => {
74+
nock('https://example.com')
75+
.defaultReplyHeaders({ 'access-control-allow-origin': '*' })
76+
.get('/api/v2/404')
77+
.reply(404, { message: 'Not Found' });
78+
5479
const client = createClient('https://example.com/api/v2');
55-
expect(client.get('/404')).rejects.toThrow();
80+
const response = await client.get('/404');
81+
82+
expect(response.ok).toBe(false);
83+
expect(response.data).toEqual({ message: 'Not Found' });
84+
expect(response.status).toEqual(404);
85+
expect(response.statusText).toEqual('Not Found');
5686
});
5787
});
5888
});

src/client/getRequest.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import { ajax } from 'rxjs/ajax';
22
import { catchError, map } from 'rxjs/operators';
3-
import { throwError, of } from 'rxjs';
3+
import { of } from 'rxjs';
44

55
const getRequest = path =>
66
ajax(path).pipe(
77
map(response => {
8-
if (response === null) {
9-
return throwError('API Timed out', response);
10-
}
118
response.ok = true;
129
return response;
1310
}),

src/client/getRequest.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import nock from 'nock';
2+
3+
import getRequest from './getRequest';
4+
5+
describe('getRequest', () => {
6+
it('returns response', async () => {
7+
const data = { foo: 'bar' };
8+
nock('http://localhost')
9+
.get('/foo')
10+
.reply(200, data);
11+
12+
const response = await getRequest('http://localhost/foo').toPromise();
13+
expect(response.ok).toBe(true);
14+
expect(response.response).toEqual(data);
15+
expect(response.status).toBe(200);
16+
});
17+
18+
it('returns error', async () => {
19+
const data = { message: 'Error' };
20+
nock('http://localhost')
21+
.get('/foo')
22+
.reply(500, data);
23+
24+
const response = await getRequest('http://localhost/foo').toPromise();
25+
expect(response.ok).toBe(false);
26+
expect(response.response).toEqual(data);
27+
expect(response.status).toBe(500);
28+
});
29+
});

0 commit comments

Comments
 (0)