Skip to content

Commit 65d6311

Browse files
committedFeb 18, 2019
Cover RequestService with tests.
1 parent db69a4f commit 65d6311

7 files changed

+75
-1
lines changed
 

‎.env.production

+3
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ REACT_APP_GIPHY_API_KEY=CdRKiCMbTnt9CkZTZ0lGukSczk6iT4Z6
66

77
# GIPHY API Host.
88
REACT_APP_GIPHY_API_HOST=https://api.giphy.com
9+
10+
# HTTP request timeout in milliseconds.
11+
HTTP_REQUEST_TIMEOUT=1000

‎.env.test

+3
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ REACT_APP_GIPHY_API_KEY=CdRKiCMbTnt9CkZTZ0lGukSczk6iT4Z6
66

77
# GIPHY API Host.
88
REACT_APP_GIPHY_API_HOST=https://api.giphy.com
9+
10+
# HTTP request timeout in milliseconds.
11+
HTTP_REQUEST_TIMEOUT=1000

‎package-lock.json

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"not op_mini all"
3434
],
3535
"devDependencies": {
36+
"axios-mock-adapter": "^1.16.0",
3637
"eslint-config-airbnb": "^17.1.0",
3738
"gh-pages": "^2.0.1",
3839
"husky": "^1.3.1",

‎src/config.js

+3
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ export const GIPHY_API_KEY = env.REACT_APP_GIPHY_API_KEY;
1010
// GIPHY API Host.
1111
export const GIPHY_API_HOST = env.REACT_APP_GIPHY_API_HOST;
1212

13+
// HTTP request timeout in milliseconds.
14+
export const REQUEST_TIMEOUT = env.HTTP_REQUEST_TIMEOUT;
15+
1316
// How many Gif images we want to request per each HTTP request.
1417
export const SEARCH_BATCH_SIZE = 30;

‎src/services/RequestService.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios from 'axios';
2+
import { REQUEST_TIMEOUT } from '../config';
23

34
// RequestService is created as a wrapper on top of axios. This is done in order to be able to
45
// switch to new http-requests library if needed. Utilizing interfaces might be a good option here.
@@ -14,6 +15,8 @@ export class RequestService {
1415
// Generate default Axios configuration for requests.
1516
// We may put here default timeouts etc.
1617
static getDefaultAxiosConfig() {
17-
return {};
18+
return {
19+
timeout: REQUEST_TIMEOUT,
20+
};
1821
}
1922
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import axios from 'axios';
2+
import AxiosMockAdapter from 'axios-mock-adapter';
3+
import { RequestService } from '../RequestService';
4+
5+
describe('RequestService', () => {
6+
it('should perform successful GET requests', (done) => {
7+
const testUrl = 'http://localhost';
8+
const testResponse = { testData: 'Test response data' };
9+
10+
const testParams = {
11+
param1: 'Param #1 value',
12+
param2: 'Param #2 value',
13+
};
14+
const mock = new AxiosMockAdapter(axios);
15+
mock.onGet(testUrl).reply(200, testResponse);
16+
17+
RequestService
18+
.get(testUrl, testParams)
19+
.then((responseData) => {
20+
// We should receive mocked response.
21+
expect(responseData).toBeDefined();
22+
expect(responseData.data).toEqual(testResponse);
23+
})
24+
.catch(() => {
25+
// We should not get here.
26+
expect(true).toBe(false);
27+
})
28+
.finally(() => {
29+
done();
30+
});
31+
});
32+
33+
it('should perform not successful GET requests', (done) => {
34+
const testUrl = 'http://localhost';
35+
const mock = new AxiosMockAdapter(axios);
36+
mock.onGet(testUrl).reply(500);
37+
38+
RequestService
39+
.get(testUrl)
40+
.then(() => {
41+
// We should not get here.
42+
expect(true).toBe(false);
43+
})
44+
.catch((err) => {
45+
// We should get here.
46+
expect(err).toBeDefined();
47+
})
48+
.finally(() => {
49+
done();
50+
});
51+
});
52+
});

0 commit comments

Comments
 (0)
Please sign in to comment.