Skip to content

Commit b459280

Browse files
authored
fix(coverage): increase coverage and fix small edge bugs (#52)
Increase coverage to 85+%, while fixing small bugs on some edge cases
1 parent c22d41a commit b459280

File tree

18 files changed

+1672
-202
lines changed

18 files changed

+1672
-202
lines changed

__tests__/internals/network/__snapshots__/getDefaultNetworkHelpers.test.js.snap

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`getDefaultNetworkHelpers only path 1`] = `
3+
exports[`getDefaultNetworkHelpers all default 1`] = `
44
Object {
55
"getToken": [Function],
66
"handleError": [Function],
@@ -13,7 +13,20 @@ Object {
1313
}
1414
`;
1515

16-
exports[`requestDELETE only path 1`] = `
16+
exports[`getDefaultNetworkHelpers custom getToken 1`] = `
17+
Object {
18+
"getToken": [Function],
19+
"handleError": [Function],
20+
"handleStatusCode": [Function],
21+
"requestDELETE": [Function],
22+
"requestGET": [Function],
23+
"requestPATCH": [Function],
24+
"requestPOST": [Function],
25+
"requestPUT": [Function],
26+
}
27+
`;
28+
29+
exports[`requestDELETE all default 1`] = `
1730
Object {
1831
"headers": Object {
1932
"Accept": "application/json",
@@ -23,7 +36,16 @@ Object {
2336
}
2437
`;
2538

26-
exports[`requestGET only path 1`] = `
39+
exports[`requestDELETE custom getToken 1`] = `
40+
Object {
41+
"headers": Object {
42+
"Accept": "application/json",
43+
},
44+
"method": "DELETE",
45+
}
46+
`;
47+
48+
exports[`requestGET all default 1`] = `
2749
Object {
2850
"headers": Object {
2951
"Accept": "application/json",
@@ -33,7 +55,16 @@ Object {
3355
}
3456
`;
3557

36-
exports[`requestPATCH only path 1`] = `
58+
exports[`requestGET custom getToken 1`] = `
59+
Object {
60+
"headers": Object {
61+
"Accept": "application/json",
62+
},
63+
"method": "GET",
64+
}
65+
`;
66+
67+
exports[`requestPATCH all default 1`] = `
3768
Object {
3869
"body": undefined,
3970
"headers": Object {
@@ -45,7 +76,18 @@ Object {
4576
}
4677
`;
4778

48-
exports[`requestPOST only path 1`] = `
79+
exports[`requestPATCH custom getToken 1`] = `
80+
Object {
81+
"body": undefined,
82+
"headers": Object {
83+
"Accept": "application/json",
84+
"Content-Type": "application/json",
85+
},
86+
"method": "PATCH",
87+
}
88+
`;
89+
90+
exports[`requestPOST all default 1`] = `
4991
Object {
5092
"body": undefined,
5193
"headers": Object {
@@ -57,7 +99,18 @@ Object {
5799
}
58100
`;
59101

60-
exports[`requestPUT only path 1`] = `
102+
exports[`requestPOST custom getToken 1`] = `
103+
Object {
104+
"body": undefined,
105+
"headers": Object {
106+
"Accept": "application/json",
107+
"Content-Type": "application/json",
108+
},
109+
"method": "POST",
110+
}
111+
`;
112+
113+
exports[`requestPUT all default 1`] = `
61114
Object {
62115
"body": undefined,
63116
"headers": Object {
@@ -68,3 +121,14 @@ Object {
68121
"method": "PUT",
69122
}
70123
`;
124+
125+
exports[`requestPUT custom getToken 1`] = `
126+
Object {
127+
"body": undefined,
128+
"headers": Object {
129+
"Accept": "application/json",
130+
"Content-Type": "application/json",
131+
},
132+
"method": "PUT",
133+
}
134+
`;

__tests__/internals/network/getDefaultNetworkHelpers.test.js

Lines changed: 75 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,93 @@
1+
import fetchMock from 'fetch-mock';
2+
13
import getDefaultNetworkHelpers from '../../../src/internals/network/getDefaultNetworkHelpers';
24

35
const NETWORK_HELPERS = getDefaultNetworkHelpers();
6+
const customGetTokenNetworkHelpers = {
7+
...NETWORK_HELPERS,
8+
getToken: () => '',
9+
};
410

511
describe('getDefaultNetworkHelpers', () => {
6-
test('only path', () => {
12+
test('all default', () => {
713
expect(NETWORK_HELPERS).toMatchSnapshot();
814
});
15+
16+
test('custom getToken', () => {
17+
expect(customGetTokenNetworkHelpers).toMatchSnapshot();
18+
});
919
});
1020

1121
describe('requestGET', () => {
12-
test('only path', async () => {
22+
test('all default', async () => {
1323
expect(await NETWORK_HELPERS.requestGET()).toMatchSnapshot();
1424
});
25+
26+
test('custom getToken', async () => {
27+
expect(await customGetTokenNetworkHelpers.requestGET()).toMatchSnapshot();
28+
});
1529
});
1630

1731
describe('requestPATCH', () => {
18-
test('only path', async () => {
32+
test('all default', async () => {
1933
expect(await NETWORK_HELPERS.requestPATCH()).toMatchSnapshot();
2034
});
35+
36+
test('custom getToken', async () => {
37+
expect(await customGetTokenNetworkHelpers.requestPATCH()).toMatchSnapshot();
38+
});
2139
});
2240

2341
describe('requestPUT', () => {
24-
test('only path', async () => {
42+
test('all default', async () => {
2543
expect(await NETWORK_HELPERS.requestPUT()).toMatchSnapshot();
2644
});
45+
46+
test('custom getToken', async () => {
47+
expect(await customGetTokenNetworkHelpers.requestPUT()).toMatchSnapshot();
48+
});
2749
});
2850

2951
describe('requestPOST', () => {
30-
test('only path', async () => {
52+
test('all default', async () => {
3153
expect(await NETWORK_HELPERS.requestPOST()).toMatchSnapshot();
3254
});
55+
56+
test('custom getToken', async () => {
57+
expect(await customGetTokenNetworkHelpers.requestPOST()).toMatchSnapshot();
58+
});
3359
});
3460

3561
describe('requestDELETE', () => {
36-
test('only path', async () => {
62+
test('all default', async () => {
3763
expect(await NETWORK_HELPERS.requestDELETE()).toMatchSnapshot();
3864
});
65+
66+
test('custom getToken', async () => {
67+
expect(
68+
await customGetTokenNetworkHelpers.requestDELETE(),
69+
).toMatchSnapshot();
70+
});
3971
});
4072

4173
describe('handleStatusCode', () => {
42-
test('valid response', () => {
43-
const response = { status: 200 };
44-
const response2 = { status: 299 };
45-
46-
expect(NETWORK_HELPERS.handleStatusCode(response)).toBe(response);
47-
expect(NETWORK_HELPERS.handleStatusCode(response2)).toBe(response2);
74+
test('no response', () => {
75+
expect(NETWORK_HELPERS.handleStatusCode(null)).toBeNull();
4876
});
4977

5078
test('invalid response', () => {
5179
const response = { status: 300 };
5280

5381
expect(() => NETWORK_HELPERS.handleStatusCode(response)).toThrow();
5482
});
83+
84+
test('valid response', () => {
85+
const response = { status: 200 };
86+
const response2 = { status: 299 };
87+
88+
expect(NETWORK_HELPERS.handleStatusCode(response)).toBe(response);
89+
expect(NETWORK_HELPERS.handleStatusCode(response2)).toBe(response2);
90+
});
5591
});
5692

5793
describe('handleError', () => {
@@ -77,4 +113,31 @@ describe('handleError', () => {
77113

78114
expect(consoleErrorMock).toHaveBeenCalledTimes(1);
79115
});
116+
117+
test('wrongly formatted error 2', async () => {
118+
const error = { response: 'oh no' };
119+
120+
await NETWORK_HELPERS.handleError(error);
121+
122+
expect(consoleErrorMock).toHaveBeenCalledTimes(1);
123+
});
124+
125+
test('valid error', async () => {
126+
fetchMock.mock('error', {
127+
status: 400,
128+
body: {
129+
message: 'errorMessage',
130+
},
131+
});
132+
133+
const res1 = await fetch('error');
134+
const error = new Error(res1.statusText);
135+
error.response = res1;
136+
137+
fetchMock.restore();
138+
139+
await NETWORK_HELPERS.handleError(error);
140+
141+
expect(consoleErrorMock).toHaveBeenCalledTimes(1);
142+
});
80143
});

0 commit comments

Comments
 (0)