@vida-global/core 1.2.5 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/npm-test.yml +24 -0
- package/index.js +1 -1
- package/lib/{active_record → activeRecord}/README.md +3 -3
- package/lib/{active_record → activeRecord}/baseRecord.js +11 -2
- package/lib/http/README.md +2 -2
- package/lib/server/README.md +181 -20
- package/lib/server/errors.js +28 -0
- package/lib/server/index.js +3 -3
- package/lib/server/server.js +7 -53
- package/lib/server/serverController.js +147 -20
- package/package.json +1 -1
- package/scripts/{active_record → activeRecord}/migrate.js +1 -1
- package/test/{active_record → activeRecord}/baseRecord.test.js +46 -90
- package/test/activeRecord/db/connection.test.js +149 -0
- package/test/activeRecord/db/connectionConfiguration.test.js +128 -0
- package/test/activeRecord/db/migrator.test.js +144 -0
- package/test/activeRecord/db/queryInterface.test.js +48 -0
- package/test/activeRecord/helpers/baseRecord.js +32 -0
- package/test/activeRecord/helpers/baseRecordMocks.js +59 -0
- package/test/activeRecord/helpers/connection.js +28 -0
- package/test/activeRecord/helpers/connectionConfiguration.js +32 -0
- package/test/activeRecord/helpers/fixtures.js +39 -0
- package/test/activeRecord/helpers/migrator.js +78 -0
- package/test/activeRecord/helpers/queryInterface.js +29 -0
- package/test/http/client.test.js +61 -239
- package/test/http/error.test.js +23 -47
- package/test/http/helpers/client.js +80 -0
- package/test/http/helpers/error.js +31 -0
- package/test/server/helpers/autoload/TmpWithHelpersController.js +17 -0
- package/test/server/helpers/serverController.js +13 -0
- package/test/server/serverController.test.js +319 -6
- package/test/active_record/db/connection.test.js +0 -221
- package/test/active_record/db/connectionConfiguration.test.js +0 -184
- package/test/active_record/db/migrator.test.js +0 -266
- package/test/active_record/db/queryInterface.test.js +0 -66
- /package/lib/{active_record → activeRecord}/db/connection.js +0 -0
- /package/lib/{active_record → activeRecord}/db/connectionConfiguration.js +0 -0
- /package/lib/{active_record → activeRecord}/db/importSchema.js +0 -0
- /package/lib/{active_record → activeRecord}/db/migration.js +0 -0
- /package/lib/{active_record → activeRecord}/db/migrationTemplate.js +0 -0
- /package/lib/{active_record → activeRecord}/db/migrationVersion.js +0 -0
- /package/lib/{active_record → activeRecord}/db/migrator.js +0 -0
- /package/lib/{active_record → activeRecord}/db/queryInterface.js +0 -0
- /package/lib/{active_record → activeRecord}/db/schema.js +0 -0
- /package/lib/{active_record → activeRecord}/index.js +0 -0
- /package/lib/{active_record → activeRecord}/utils.js +0 -0
package/test/http/client.test.js
CHANGED
|
@@ -1,26 +1,6 @@
|
|
|
1
1
|
const { HttpClient } = require('../../lib/http/client');
|
|
2
|
-
const TestHelpers = require('@vida-global/test-helpers');
|
|
3
2
|
const { logger } = require('../../lib/logger');
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const urlRoot = `https://${TestHelpers.Faker.Text.randomString()}.com`.toLowerCase();
|
|
7
|
-
const apiEndpoint = `/${TestHelpers.Faker.Text.randomString()}`
|
|
8
|
-
const apiUrl = `${urlRoot}${apiEndpoint}`;
|
|
9
|
-
const requestReturnValue = {
|
|
10
|
-
[TestHelpers.Faker.Text.randomString()]: Math.random()
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
class ClientWithUrlRoot extends HttpClient {
|
|
15
|
-
get urlRoot() { return urlRoot; }
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
global.fetch = jest.fn().mockReturnValue({
|
|
20
|
-
json: () => requestReturnValue,
|
|
21
|
-
status: 200
|
|
22
|
-
});
|
|
23
|
-
logger.info = jest.fn();
|
|
3
|
+
const Helpers = require('./helpers/client');
|
|
24
4
|
|
|
25
5
|
|
|
26
6
|
afterEach(() => {
|
|
@@ -28,244 +8,86 @@ afterEach(() => {
|
|
|
28
8
|
});
|
|
29
9
|
|
|
30
10
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
await client.post(apiEndpoint);
|
|
35
|
-
expect(fetch).toHaveBeenCalledWith(apiUrl, expect.any(Object));
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
/***************************************************************************************************
|
|
41
|
-
* POST
|
|
42
|
-
***************************************************************************************************/
|
|
43
|
-
describe('HttpClient#post', () => {
|
|
44
|
-
const client = new HttpClient();
|
|
45
|
-
|
|
46
|
-
it ('returns the result of the API request', async () => {
|
|
47
|
-
const res = await client.post(apiUrl);
|
|
48
|
-
expect(res).toEqual({data: requestReturnValue, status: 200});
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it ('calls fetch with the provided url and default params', async () => {
|
|
52
|
-
const expectedRequestParams = {
|
|
53
|
-
method: 'POST',
|
|
54
|
-
body: "{}",
|
|
55
|
-
headers: {
|
|
56
|
-
Accept: "application/json",
|
|
57
|
-
"Content-Type": "application/json",
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
await client.post(apiUrl);
|
|
62
|
-
expect(fetch).toHaveBeenCalledTimes(1);
|
|
63
|
-
expect(fetch).toHaveBeenCalledWith(apiUrl, expectedRequestParams);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it ('includes the provided body in the request', async () => {
|
|
67
|
-
const body = {[TestHelpers.Faker.Text.randomString()]: Math.random()};
|
|
68
|
-
await client.post(apiUrl, { body });
|
|
69
|
-
expect(fetch).toHaveBeenCalledWith(apiUrl, expect.objectContaining({
|
|
70
|
-
body: JSON.stringify(body)
|
|
71
|
-
}));
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it ('includes additional headers', async () => {
|
|
75
|
-
const headers = {[TestHelpers.Faker.Text.randomString()]: Math.random()};
|
|
76
|
-
const expectedRequestParams = {
|
|
77
|
-
method: 'POST',
|
|
78
|
-
body: "{}",
|
|
79
|
-
headers: {
|
|
80
|
-
Accept: "application/json",
|
|
81
|
-
"Content-Type": "application/json",
|
|
82
|
-
...headers
|
|
83
|
-
},
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
await client.post(apiUrl, { headers });
|
|
87
|
-
expect(fetch).toHaveBeenCalledWith(apiUrl, expectedRequestParams);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it ('logs api requests', async () => {
|
|
91
|
-
await client.post(apiUrl);
|
|
92
|
-
expect(logger.info).toHaveBeenCalledTimes(1);
|
|
93
|
-
expect(logger.info).toHaveBeenCalledWith(`API CALL: POST ${apiUrl}`);
|
|
94
|
-
});
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
Helpers.mockFetchSuccess();
|
|
13
|
+
logger.info = jest.fn();
|
|
95
14
|
});
|
|
96
15
|
|
|
97
16
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const client = new HttpClient();
|
|
103
|
-
|
|
104
|
-
it ('returns the result of the API request', async () => {
|
|
105
|
-
const res = await client.put(apiUrl);
|
|
106
|
-
expect(res).toEqual({data: requestReturnValue, status: 200});
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it ('calls fetch with the provided url and default params', async () => {
|
|
110
|
-
const expectedRequestParams = {
|
|
111
|
-
method: 'PUT',
|
|
112
|
-
body: "{}",
|
|
113
|
-
headers: {
|
|
114
|
-
Accept: "application/json",
|
|
115
|
-
"Content-Type": "application/json",
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
await client.put(apiUrl);
|
|
120
|
-
expect(fetch).toHaveBeenCalledTimes(1);
|
|
121
|
-
expect(fetch).toHaveBeenCalledWith(apiUrl, expectedRequestParams);
|
|
122
|
-
});
|
|
17
|
+
describe('HttpClient', () => {
|
|
18
|
+
describe('HttpClient#urlRoot', () => {
|
|
19
|
+
it('prepends the url root when overridden', async () => {
|
|
20
|
+
const client = new Helpers.ClientWithUrlRoot();
|
|
123
21
|
|
|
124
|
-
|
|
125
|
-
const body = {[TestHelpers.Faker.Text.randomString()]: Math.random()};
|
|
126
|
-
await client.put(apiUrl, { body });
|
|
127
|
-
expect(fetch).toHaveBeenCalledWith(apiUrl, expect.objectContaining({
|
|
128
|
-
body: JSON.stringify(body)
|
|
129
|
-
}));
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
it ('includes additional headers', async () => {
|
|
133
|
-
const headers = {[TestHelpers.Faker.Text.randomString()]: Math.random()};
|
|
134
|
-
const expectedRequestParams = {
|
|
135
|
-
method: 'PUT',
|
|
136
|
-
body: "{}",
|
|
137
|
-
headers: {
|
|
138
|
-
Accept: "application/json",
|
|
139
|
-
"Content-Type": "application/json",
|
|
140
|
-
...headers
|
|
141
|
-
},
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
await client.put(apiUrl, { headers });
|
|
145
|
-
expect(fetch).toHaveBeenCalledWith(apiUrl, expectedRequestParams);
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
it ('logs api requests', async () => {
|
|
149
|
-
await client.put(apiUrl);
|
|
150
|
-
expect(logger.info).toHaveBeenCalledTimes(1);
|
|
151
|
-
expect(logger.info).toHaveBeenCalledWith(`API CALL: PUT ${apiUrl}`);
|
|
152
|
-
});
|
|
153
|
-
});
|
|
22
|
+
await client.post(Helpers.endpoint);
|
|
154
23
|
|
|
24
|
+
expect(fetch).toHaveBeenCalledWith(Helpers.apiUrl, expect.any(Object));
|
|
25
|
+
});
|
|
26
|
+
});
|
|
155
27
|
|
|
156
|
-
/***************************************************************************************************
|
|
157
|
-
* GET
|
|
158
|
-
***************************************************************************************************/
|
|
159
|
-
describe('HttpClient#get', () => {
|
|
160
|
-
const client = new HttpClient();
|
|
161
|
-
it ('returns the result of the API request', async () => {
|
|
162
|
-
const res = await client.get(apiUrl);
|
|
163
|
-
expect(res).toEqual({data: requestReturnValue, status: 200});
|
|
164
|
-
});
|
|
165
28
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
29
|
+
const httpMethods = [
|
|
30
|
+
['post', 'POST', { supportsBody: true }],
|
|
31
|
+
['put', 'PUT', { supportsBody: true }],
|
|
32
|
+
['get', 'GET', { supportsParams: true }],
|
|
33
|
+
['delete', 'DELETE', { supportsParams: true }],
|
|
34
|
+
];
|
|
35
|
+
describe.each(httpMethods)('HttpClient#%s', (methodName, httpMethod, behavior) => {
|
|
36
|
+
const client = new HttpClient();
|
|
174
37
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
expect(fetch).toHaveBeenCalledWith(apiUrl, expectedRequestParams);
|
|
178
|
-
});
|
|
38
|
+
it ('returns the API result payload and status', async () => {
|
|
39
|
+
const result = await client[methodName](Helpers.apiUrl);
|
|
179
40
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
const param1Value = TestHelpers.Faker.Text.randomString();
|
|
183
|
-
const param2Name = TestHelpers.Faker.Text.randomString();
|
|
184
|
-
const param2Value = TestHelpers.Faker.Text.randomString();
|
|
185
|
-
const requestParams = {[param1Name]: param1Value, [param2Name]: param2Value};
|
|
186
|
-
const expectedUrl = `${apiUrl}?${param1Name}=${param1Value}&${param2Name}=${param2Value}`;
|
|
187
|
-
await client.get(apiUrl, {requestParams });
|
|
41
|
+
expect(result).toEqual({ data: Helpers.responsePayload, status: 200 });
|
|
42
|
+
});
|
|
188
43
|
|
|
189
|
-
|
|
190
|
-
|
|
44
|
+
it ('calls fetch with default headers', async () => {
|
|
45
|
+
await client[methodName](Helpers.apiUrl);
|
|
191
46
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
method: 'GET',
|
|
196
|
-
headers: {
|
|
197
|
-
Accept: "application/json",
|
|
198
|
-
"Content-Type": "application/json",
|
|
199
|
-
...headers
|
|
200
|
-
},
|
|
201
|
-
}
|
|
47
|
+
const expectedBody = behavior.supportsBody ? {} : undefined;
|
|
48
|
+
Helpers.expectRequest(httpMethod, Helpers.apiUrl, { body: expectedBody });
|
|
49
|
+
});
|
|
202
50
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
});
|
|
51
|
+
it ('merges additional headers', async () => {
|
|
52
|
+
const headers = Helpers.randomPayload();
|
|
206
53
|
|
|
207
|
-
|
|
208
|
-
await client.get(apiUrl);
|
|
209
|
-
expect(logger.info).toHaveBeenCalledTimes(1);
|
|
210
|
-
expect(logger.info).toHaveBeenCalledWith(`API CALL: GET ${apiUrl}`);
|
|
211
|
-
});
|
|
212
|
-
});
|
|
54
|
+
await client[methodName](Helpers.apiUrl, { headers });
|
|
213
55
|
|
|
56
|
+
const expectedBody = behavior.supportsBody ? {} : undefined;
|
|
57
|
+
Helpers.expectRequest(httpMethod, Helpers.apiUrl, { headers, body: expectedBody });
|
|
58
|
+
});
|
|
214
59
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
***************************************************************************************************/
|
|
218
|
-
describe('HttpClient#delete', () => {
|
|
219
|
-
const client = new HttpClient();
|
|
220
|
-
it ('returns the result of the API request', async () => {
|
|
221
|
-
const res = await client.delete(apiUrl);
|
|
222
|
-
expect(res).toEqual({data: requestReturnValue, status: 200});
|
|
223
|
-
});
|
|
60
|
+
it ('logs API requests', async () => {
|
|
61
|
+
await client[methodName](Helpers.apiUrl);
|
|
224
62
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
headers: {
|
|
229
|
-
Accept: "application/json",
|
|
230
|
-
"Content-Type": "application/json",
|
|
231
|
-
}
|
|
232
|
-
}
|
|
63
|
+
expect(logger.info).toHaveBeenCalledTimes(1);
|
|
64
|
+
expect(logger.info).toHaveBeenCalledWith(`API CALL: ${httpMethod} ${Helpers.apiUrl}`);
|
|
65
|
+
});
|
|
233
66
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
});
|
|
67
|
+
if (behavior.supportsBody) {
|
|
68
|
+
it('serializes and sends request body', async () => {
|
|
69
|
+
const body = Helpers.randomPayload();
|
|
238
70
|
|
|
239
|
-
|
|
240
|
-
const param1Name = TestHelpers.Faker.Text.randomString();
|
|
241
|
-
const param1Value = TestHelpers.Faker.Text.randomString();
|
|
242
|
-
const param2Name = TestHelpers.Faker.Text.randomString();
|
|
243
|
-
const param2Value = TestHelpers.Faker.Text.randomString();
|
|
244
|
-
const requestParams = {[param1Name]: param1Value, [param2Name]: param2Value};
|
|
245
|
-
const expectedUrl = `${apiUrl}?${param1Name}=${param1Value}&${param2Name}=${param2Value}`;
|
|
246
|
-
await client.delete(apiUrl, {requestParams });
|
|
71
|
+
await client[methodName](Helpers.apiUrl, { body });
|
|
247
72
|
|
|
248
|
-
|
|
249
|
-
|
|
73
|
+
expect(fetch).toHaveBeenCalledWith(
|
|
74
|
+
Helpers.apiUrl,
|
|
75
|
+
expect.objectContaining({ body: JSON.stringify(body) }),
|
|
76
|
+
);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
250
79
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
"Content-Type": "application/json",
|
|
258
|
-
...headers
|
|
259
|
-
},
|
|
260
|
-
}
|
|
80
|
+
if (behavior.supportsParams) {
|
|
81
|
+
it('adds request params to url', async () => {
|
|
82
|
+
const requestParams = {
|
|
83
|
+
first: 'one',
|
|
84
|
+
second: 'two',
|
|
85
|
+
};
|
|
261
86
|
|
|
262
|
-
|
|
263
|
-
expect(fetch).toHaveBeenCalledWith(apiUrl, expectedRequestParams);
|
|
264
|
-
});
|
|
87
|
+
await client[methodName](Helpers.apiUrl, { requestParams });
|
|
265
88
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
});
|
|
89
|
+
expect(fetch).toHaveBeenCalledWith(`${Helpers.apiUrl}?first=one&second=two`, expect.any(Object));
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
});
|
|
271
93
|
});
|
package/test/http/error.test.js
CHANGED
|
@@ -1,71 +1,47 @@
|
|
|
1
|
-
const { HttpError }
|
|
2
|
-
const TestHelpers
|
|
1
|
+
const { HttpError } = require('../../lib/http/error');
|
|
2
|
+
const TestHelpers = require('@vida-global/test-helpers');
|
|
3
|
+
const { buildRequestPayload, buildResponse } = require('./helpers/error');
|
|
3
4
|
|
|
4
|
-
const key1 = TestHelpers.Faker.Text.randomString()
|
|
5
|
-
const key2 = TestHelpers.Faker.Text.randomString()
|
|
6
|
-
const key3 = TestHelpers.Faker.Text.randomString()
|
|
7
|
-
const key4 = TestHelpers.Faker.Text.randomString()
|
|
8
|
-
const key5 = TestHelpers.Faker.Text.randomString()
|
|
9
|
-
const val1 = TestHelpers.Faker.Text.randomString()
|
|
10
|
-
const val2 = TestHelpers.Faker.Text.randomString()
|
|
11
|
-
const val3 = TestHelpers.Faker.Text.randomString()
|
|
12
|
-
const val4 = TestHelpers.Faker.Text.randomString()
|
|
13
|
-
const val5 = TestHelpers.Faker.Text.randomString()
|
|
14
5
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
};
|
|
24
|
-
const requestPayload = {
|
|
25
|
-
[key3]: val3,
|
|
26
|
-
[key4]: {[key5]: val5}
|
|
27
|
-
};
|
|
28
|
-
const error = new HttpError(response, requestPayload);
|
|
6
|
+
let response;
|
|
7
|
+
let requestPayload;
|
|
8
|
+
let error;
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
response = buildResponse();
|
|
12
|
+
requestPayload = buildRequestPayload();
|
|
13
|
+
error = new HttpError(response, requestPayload);
|
|
14
|
+
});
|
|
29
15
|
|
|
30
16
|
|
|
31
17
|
describe('HttpError', () => {
|
|
32
|
-
it ('
|
|
18
|
+
it ('sets the status code in the message', () => {
|
|
33
19
|
expect(error.message).toEqual(`HTTP Error: ${response.status}`);
|
|
34
20
|
});
|
|
35
21
|
|
|
36
|
-
it ('
|
|
22
|
+
it ('exposes core response metadata', () => {
|
|
37
23
|
expect(error.status).toEqual(response.status);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it ('passes through the status text', () => {
|
|
41
24
|
expect(error.statusText).toEqual(response.statusText);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it ('passes through the url', () => {
|
|
45
25
|
expect(error.url).toEqual(response.url);
|
|
46
26
|
});
|
|
47
27
|
|
|
48
|
-
it ('
|
|
28
|
+
it ('stores copies of headers and request payload', () => {
|
|
49
29
|
expect(error.responseHeaders).toEqual(response.headers);
|
|
50
30
|
expect(error.responseHeaders).not.toBe(response.headers);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it ('passes through a copy of the request payload', () => {
|
|
54
31
|
expect(error.requestPayload).toEqual(requestPayload);
|
|
55
32
|
expect(error.requestPayload).not.toBe(requestPayload);
|
|
56
33
|
});
|
|
57
34
|
|
|
58
|
-
it ('
|
|
59
|
-
response.text
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
expect(data).toEqual(expected);
|
|
35
|
+
it ('parses json text bodies', async () => {
|
|
36
|
+
response.text = () => '{"ok":true,"nested":{"count":1}}';
|
|
37
|
+
|
|
38
|
+
await expect(error.data()).resolves.toEqual({ ok: true, nested: { count: 1 } });
|
|
63
39
|
});
|
|
64
40
|
|
|
65
|
-
it ('
|
|
41
|
+
it ('falls back to raw text when parsing fails', async () => {
|
|
66
42
|
const text = TestHelpers.Faker.Text.randomString();
|
|
67
|
-
response.text
|
|
68
|
-
|
|
69
|
-
expect(data).toEqual(text);
|
|
43
|
+
response.text = () => text;
|
|
44
|
+
|
|
45
|
+
await expect(error.data()).resolves.toEqual(text);
|
|
70
46
|
});
|
|
71
47
|
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
const { HttpClient } = require('../../../lib/http/client');
|
|
2
|
+
const TestHelpers = require('@vida-global/test-helpers');
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
/***************************************************************************************************
|
|
6
|
+
* VARIABLES / VARIABLE GENERATION
|
|
7
|
+
***************************************************************************************************/
|
|
8
|
+
const randomPayload = () => {
|
|
9
|
+
return {
|
|
10
|
+
[TestHelpers.Faker.Text.randomString()]: Math.random(),
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
const baseUrl = `https://${TestHelpers.Faker.Text.randomString()}.com`.toLowerCase();
|
|
16
|
+
const endpoint = `/${TestHelpers.Faker.Text.randomString()}`;
|
|
17
|
+
const apiUrl = `${baseUrl}${endpoint}`;
|
|
18
|
+
const responsePayload = randomPayload();
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class ClientWithUrlRoot extends HttpClient {
|
|
22
|
+
get urlRoot() {
|
|
23
|
+
return baseUrl;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
/***************************************************************************************************
|
|
29
|
+
* MOCKS
|
|
30
|
+
***************************************************************************************************/
|
|
31
|
+
const mockFetchSuccess = () => {
|
|
32
|
+
global.fetch = jest.fn().mockResolvedValue({
|
|
33
|
+
json: jest.fn().mockResolvedValue(responsePayload),
|
|
34
|
+
status: 200,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
/***************************************************************************************************
|
|
40
|
+
* ASSERTIONS
|
|
41
|
+
***************************************************************************************************/
|
|
42
|
+
function expectRequest(method, expectedUrl, options = {}) {
|
|
43
|
+
const expectedRequest = buildExpectedRequest({
|
|
44
|
+
method,
|
|
45
|
+
headers: options.headers,
|
|
46
|
+
body: options.body,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
expect(fetch).toHaveBeenCalledTimes(1);
|
|
50
|
+
expect(fetch).toHaveBeenCalledWith(expectedUrl, expectedRequest);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
function buildExpectedRequest({ method, headers, body }) {
|
|
55
|
+
const request = {
|
|
56
|
+
method,
|
|
57
|
+
headers: {
|
|
58
|
+
Accept: 'application/json',
|
|
59
|
+
'Content-Type': 'application/json',
|
|
60
|
+
...(headers || {}),
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
if (body !== undefined) {
|
|
65
|
+
request.body = JSON.stringify(body);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return request;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
module.exports = {
|
|
73
|
+
apiUrl,
|
|
74
|
+
ClientWithUrlRoot,
|
|
75
|
+
endpoint,
|
|
76
|
+
expectRequest,
|
|
77
|
+
mockFetchSuccess,
|
|
78
|
+
randomPayload,
|
|
79
|
+
responsePayload,
|
|
80
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const TestHelpers = require('@vida-global/test-helpers');
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
function buildResponse(overrides = {}) {
|
|
5
|
+
return {
|
|
6
|
+
headers: {
|
|
7
|
+
requestId: TestHelpers.Faker.Text.randomString(),
|
|
8
|
+
traceId: TestHelpers.Faker.Text.randomString(),
|
|
9
|
+
},
|
|
10
|
+
status: Math.floor(Math.random() * 500) + 100,
|
|
11
|
+
statusText: TestHelpers.Faker.Text.randomString(),
|
|
12
|
+
url: TestHelpers.Faker.Text.randomString(),
|
|
13
|
+
...overrides,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
function buildRequestPayload() {
|
|
19
|
+
return {
|
|
20
|
+
foo: TestHelpers.Faker.Text.randomString(),
|
|
21
|
+
nested: {
|
|
22
|
+
bar: TestHelpers.Faker.Text.randomString(),
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
module.exports = {
|
|
29
|
+
buildRequestPayload,
|
|
30
|
+
buildResponse,
|
|
31
|
+
}
|