@shopgate/pwa-core 7.31.5 → 7.31.6

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.
@@ -1,333 +0,0 @@
1
- import PipelineRequest, { DEFAULT_VERSION, DEFAULT_RETRIES, DEFAULT_MAX_RETRIES, DEFAULT_INPUT, DEFAULT_TIMEOUT, DEFAULT_MAX_TIMEOUT, DEFAULT_PROCESSED, DEFAULT_HANDLE_ERROR } from '.';
2
- import * as processTypes from "../../constants/ProcessTypes";
3
- import * as errorHandleTypes from "../../constants/ErrorHandleTypes";
4
- let request;
5
- const mockedWarn = jest.fn();
6
- jest.mock("../../helpers", () => ({
7
- logger: {
8
- warn: (...args) => mockedWarn.apply(void 0, args)
9
- }
10
- }));
11
- describe('PipelineRequest', () => {
12
- beforeEach(() => {
13
- request = new PipelineRequest('testPipeline');
14
- jest.clearAllMocks();
15
- });
16
- it('should throw if no pipeline name is set', done => {
17
- try {
18
- // eslint-disable-next-line no-new
19
- new PipelineRequest();
20
- done('Did not throw');
21
- } catch (e) {
22
- done();
23
- }
24
- });
25
- it('should be instanciatable', () => {
26
- expect(request instanceof PipelineRequest).toBe(true);
27
- });
28
- it('has a default version', () => {
29
- expect(request.version).toEqual(DEFAULT_VERSION);
30
- });
31
- it('has a default input', () => {
32
- expect(request.input).toEqual(DEFAULT_INPUT);
33
- });
34
- it('has trusted set to false by default', () => {
35
- expect(request.trusted).toEqual(false);
36
- });
37
- it('has a default retries', () => {
38
- expect(request.retries).toEqual(DEFAULT_RETRIES);
39
- });
40
- it('has a default timeout', () => {
41
- expect(request.timeout).toEqual(DEFAULT_TIMEOUT);
42
- });
43
- it('has a default process handling', () => {
44
- expect(request.process).toEqual(DEFAULT_PROCESSED);
45
- });
46
- it('has a default error handling', () => {
47
- expect(request.handleErrors).toEqual(DEFAULT_HANDLE_ERROR);
48
- });
49
- describe('setVersion()', () => {
50
- it('should throw if input is not a number', done => {
51
- try {
52
- request.setVersion('a string');
53
- done('Did not throw');
54
- } catch (e) {
55
- done();
56
- }
57
- });
58
- it('should throw if it is a negative number', done => {
59
- try {
60
- request.setVersion(-1);
61
- done('Did not throw');
62
- } catch (e) {
63
- done();
64
- }
65
- });
66
- it('should throw if the value is 0', done => {
67
- try {
68
- request.setVersion(0);
69
- done('Did not throw');
70
- } catch (e) {
71
- done();
72
- }
73
- });
74
- it('should set to default if no parameter supplied', () => {
75
- request.setVersion();
76
- expect(request.version).toEqual(DEFAULT_VERSION);
77
- });
78
- it('should set the new timeout', () => {
79
- request.setVersion(2);
80
- expect(request.version).toEqual(2);
81
- });
82
- it('should return a class instance', () => {
83
- const value = request.setVersion(2);
84
- expect(value instanceof PipelineRequest).toEqual(true);
85
- });
86
- });
87
- describe('setInput()', () => {
88
- it('should throw if input is a string', done => {
89
- try {
90
- request.setInput('some input');
91
- done('Did not throw');
92
- } catch (e) {
93
- done();
94
- }
95
- });
96
- it('should throw if input is a number', done => {
97
- try {
98
- request.setInput(123);
99
- done('Did not throw');
100
- } catch (e) {
101
- done();
102
- }
103
- });
104
- it('should throw if input is an array', done => {
105
- try {
106
- request.setInput(['some value']);
107
- done('Did not throw');
108
- } catch (e) {
109
- done();
110
- }
111
- });
112
- it('should set to default if no parameter supplied', () => {
113
- request.setInput();
114
- expect(request.input).toEqual(DEFAULT_INPUT);
115
- });
116
- it('should set the new input', () => {
117
- const input = {
118
- someKey: 'someValue'
119
- };
120
- request.setInput(input);
121
- expect(request.input).toEqual(input);
122
- });
123
- it('should return a class instance', () => {
124
- const value = request.setInput();
125
- expect(value instanceof PipelineRequest).toEqual(true);
126
- });
127
- });
128
- describe('setTrusted()', () => {
129
- it('is false by default', () => {
130
- expect(request.trusted).toEqual(false);
131
- });
132
- it('should set it to true', () => {
133
- request.setTrusted();
134
- expect(request.trusted).toEqual(true);
135
- });
136
- });
137
- describe('setRetries()', () => {
138
- it('should throw if input it not a number', done => {
139
- try {
140
- request.setRetries('a string');
141
- done('Did not throw');
142
- } catch (e) {
143
- done();
144
- }
145
- });
146
- it('should throw if its a negative number', done => {
147
- try {
148
- request.setRetries(-1);
149
- done('Did not throw');
150
- } catch (e) {
151
- done();
152
- }
153
- });
154
- it('should throw if the value is above max', done => {
155
- try {
156
- request.setRetries(DEFAULT_MAX_RETRIES + 1);
157
- done('Did not throw');
158
- } catch (e) {
159
- done();
160
- }
161
- });
162
- it('should set to default if no parameter supplied', () => {
163
- request.setRetries();
164
- expect(request.retries).toEqual(DEFAULT_RETRIES);
165
- });
166
- it('should set the new retries amount', () => {
167
- request.setRetries(3);
168
- expect(request.retries).toEqual(3);
169
- });
170
- it('should return a class instance', () => {
171
- const value = request.setRetries(2);
172
- expect(value instanceof PipelineRequest).toEqual(true);
173
- });
174
- });
175
- describe('setTimeout()', () => {
176
- it('should throw if input it not a number', done => {
177
- try {
178
- request.setTimeout('a string');
179
- done('Did not throw');
180
- } catch (e) {
181
- done();
182
- }
183
- });
184
- it('should throw if its a negative number', done => {
185
- try {
186
- request.setTimeout(-1);
187
- done('Did not throw');
188
- } catch (e) {
189
- done();
190
- }
191
- });
192
- it('should throw if the value is above max', done => {
193
- try {
194
- request.setTimeout(DEFAULT_MAX_TIMEOUT + 1000);
195
- done('Did not throw');
196
- } catch (e) {
197
- done();
198
- }
199
- });
200
- it('should set to default if no parameter supplied', () => {
201
- request.setTimeout();
202
- expect(request.timeout).toEqual(DEFAULT_TIMEOUT);
203
- });
204
- it('should set the new timeout', () => {
205
- request.setTimeout(2000);
206
- expect(request.timeout).toEqual(2000);
207
- });
208
- it('should return a class instance', () => {
209
- const value = request.setTimeout(2);
210
- expect(value instanceof PipelineRequest).toEqual(true);
211
- });
212
- });
213
- describe('setResponseProcessed()', () => {
214
- it('should throw if input is a number', done => {
215
- try {
216
- request.setResponseProcessed(123);
217
- done('Did not throw');
218
- } catch (e) {
219
- done();
220
- }
221
- });
222
- it('should throw if input is an array', done => {
223
- try {
224
- request.setResponseProcessed(['some value']);
225
- done('Did not throw');
226
- } catch (e) {
227
- done();
228
- }
229
- });
230
- it('should throw if input is an object', done => {
231
- try {
232
- request.setResponseProcessed({
233
- someKey: 'someValue'
234
- });
235
- done('Did not throw');
236
- } catch (e) {
237
- done();
238
- }
239
- });
240
- it('should throw if input not one of the possible values', done => {
241
- try {
242
- request.setResponseProcessed('SOME_WEIRD_THING');
243
- done('Did not throw');
244
- } catch (e) {
245
- done();
246
- }
247
- });
248
- it('should set to default if no parameter supplied', () => {
249
- request.setResponseProcessed();
250
- expect(request.process).toEqual(DEFAULT_PROCESSED);
251
- });
252
- it('should set the new process', () => {
253
- request.setResponseProcessed(processTypes.PROCESS_LAST);
254
- expect(request.process).toEqual(processTypes.PROCESS_LAST);
255
- });
256
- it('should return a class instance', () => {
257
- const value = request.setResponseProcessed();
258
- expect(value instanceof PipelineRequest).toEqual(true);
259
- });
260
- });
261
- describe('setHandleErrors()', () => {
262
- it('should throw if input is a number', done => {
263
- try {
264
- request.setHandleErrors(123);
265
- done('Did not throw');
266
- } catch (e) {
267
- done();
268
- }
269
- });
270
- it('should throw if input is an array', done => {
271
- try {
272
- request.setHandleErrors(['some value']);
273
- done('Did not throw');
274
- } catch (e) {
275
- done();
276
- }
277
- });
278
- it('should throw if input is an object', done => {
279
- try {
280
- request.setHandleErrors({
281
- someKey: 'someValue'
282
- });
283
- done('Did not throw');
284
- } catch (e) {
285
- done();
286
- }
287
- });
288
- it('should throw if input not one of the possible values', done => {
289
- try {
290
- request.setHandleErrors('SOME_WEIRD_THING');
291
- done('Did not throw');
292
- } catch (e) {
293
- done();
294
- }
295
- });
296
- it('should set to default if no parameter supplied', () => {
297
- request.setHandleErrors();
298
- expect(request.handleErrors).toEqual(DEFAULT_HANDLE_ERROR);
299
- });
300
- it('should set the new process', () => {
301
- request.setHandleErrors(errorHandleTypes.ERROR_HANDLE_SUPPRESS);
302
- expect(request.handleErrors).toEqual(errorHandleTypes.ERROR_HANDLE_SUPPRESS);
303
- });
304
- it('should return a class instance', () => {
305
- const value = request.setHandleErrors();
306
- expect(value instanceof PipelineRequest).toEqual(true);
307
- });
308
- });
309
- describe('setHandleErrors()', () => {
310
- it('should set a blacklist of error codes to be not handled', () => {
311
- const codes = ['ETEST1', 'ETEST2'];
312
- request.setErrorBlacklist(codes);
313
- expect(request.errorBlacklist).toEqual(codes);
314
- });
315
- });
316
- describe('deprecation', () => {
317
- it('setSuppressErrors', () => {
318
- request.setSuppressErrors(true);
319
- expect(request.handleErrors).toEqual(errorHandleTypes.ERROR_HANDLE_SUPPRESS);
320
- request.setSuppressErrors(false);
321
- expect(request.handleErrors).toEqual(DEFAULT_HANDLE_ERROR);
322
- expect(mockedWarn).toHaveBeenCalledTimes(2);
323
- expect(mockedWarn).toHaveBeenCalledWith('Deprecated: setSuppressErrors() will be removed. Use setHandleErrors() instead!');
324
- });
325
- it('setHandledErrors', () => {
326
- const codes = ['ETEST'];
327
- request.setHandledErrors(codes);
328
- expect(request.errorBlacklist).toEqual(codes);
329
- expect(mockedWarn).toHaveBeenCalledTimes(1);
330
- expect(mockedWarn).toHaveBeenCalledWith('Deprecated: setHandledErrors() will be removed in favor of setErrorBlacklist()!');
331
- });
332
- });
333
- });
@@ -1,203 +0,0 @@
1
- import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
2
- import Request from "../Request";
3
- import RequestManager from '.';
4
- import { PROCESS_ANY, PROCESS_LAST_REQUEST, PROCESS_FIRST_RESPONSE, PROCESS_SEQUENTIALLY, PROCESS_ORDERED_REQUEST, PROPAGATE_REJECT, PROPAGATE_SINGLE } from "../../constants/RequestManagerModes";
5
-
6
- /* eslint-disable no-unused-vars, class-methods-use-this */
7
- let SuccessfulRequest = /*#__PURE__*/function (_Request) {
8
- function SuccessfulRequest(manager, payload = 42, timeout = 1000) {
9
- var _this;
10
- _this = _Request.call(this, manager) || this;
11
- _this.createSerial('foo');
12
- _this.payload = payload;
13
- _this.timeout = timeout;
14
- return _this;
15
- }
16
- _inheritsLoose(SuccessfulRequest, _Request);
17
- var _proto = SuccessfulRequest.prototype;
18
- _proto.onDispatch = function onDispatch(resolve, reject) {
19
- setTimeout(() => {
20
- this.manager.handleResponse(this, resolve, this.payload);
21
- }, this.timeout);
22
- };
23
- return SuccessfulRequest;
24
- }(Request);
25
- let FailingRequest = /*#__PURE__*/function (_Request2) {
26
- function FailingRequest(manager) {
27
- var _this2;
28
- _this2 = _Request2.call(this, manager) || this;
29
- _this2.createSerial('foo');
30
- return _this2;
31
- }
32
- _inheritsLoose(FailingRequest, _Request2);
33
- var _proto2 = FailingRequest.prototype;
34
- _proto2.onDispatch = function onDispatch(resolve, reject) {
35
- this.manager.handleError(this, reject);
36
- };
37
- return FailingRequest;
38
- }(Request);
39
- let NeverResolvingRequest = /*#__PURE__*/function (_Request3) {
40
- function NeverResolvingRequest(manager) {
41
- var _this3;
42
- _this3 = _Request3.call(this, manager) || this;
43
- _this3.createSerial('foo');
44
- return _this3;
45
- }
46
- _inheritsLoose(NeverResolvingRequest, _Request3);
47
- var _proto3 = NeverResolvingRequest.prototype;
48
- _proto3.onDispatch = function onDispatch(resolve, reject) {};
49
- _proto3.onTimeout = function onTimeout(resolve, reject) {
50
- this.manager.handleError(this, reject);
51
- };
52
- return NeverResolvingRequest;
53
- }(Request);
54
- /* eslint-enable no-unused-vars, class-methods-use-this */
55
- describe.skip('RequestManager', () => {
56
- jest.useFakeTimers();
57
- let timestamp = 0;
58
- RequestManager.currentTime = () => {
59
- timestamp += 1;
60
- return timestamp;
61
- };
62
- it('should default to PROCESS_ANY and PROPAGATE_SINGLE', async () => {
63
- const manager = new RequestManager();
64
- expect(manager.processingMode).toEqual(PROCESS_ANY);
65
- expect(manager.propagationMode).toEqual(PROPAGATE_SINGLE);
66
- });
67
- it('should succeed', async () => {
68
- const succeeded = jest.fn();
69
- const failed = jest.fn();
70
- const manager = new RequestManager();
71
- const request = new SuccessfulRequest(manager);
72
- const req = request.dispatch().then(succeeded).catch(failed);
73
- jest.runAllTimers();
74
- await req;
75
- expect(succeeded).toHaveBeenCalled();
76
- expect(failed).not.toHaveBeenCalled();
77
- });
78
- it('should fail', async () => {
79
- const succeeded = jest.fn();
80
- const failed = jest.fn();
81
- const manager = new RequestManager();
82
- const request = new FailingRequest(manager);
83
- const req = request.dispatch().then(succeeded).catch(failed);
84
- jest.runAllTimers();
85
- await req;
86
- expect(succeeded).not.toHaveBeenCalled();
87
- expect(failed).toHaveBeenCalled();
88
- });
89
- it('should only resolve the last request ', async () => {
90
- const succeeded = jest.fn();
91
- const failed = jest.fn();
92
- const manager = new RequestManager({
93
- processingMode: PROCESS_LAST_REQUEST,
94
- propagationMode: PROPAGATE_REJECT
95
- });
96
- const request1 = new SuccessfulRequest(manager, 1);
97
- const request2 = new SuccessfulRequest(manager, 2);
98
- const req1 = request1.dispatch().then(resp => succeeded(resp)).catch(() => failed(1));
99
- const req2 = request2.dispatch().then(resp => succeeded(resp)).catch(() => failed(2));
100
- jest.runAllTimers();
101
- await req1;
102
- await req2;
103
- expect(succeeded).toHaveBeenCalledTimes(1);
104
- expect(succeeded).toHaveBeenCalledWith(2);
105
- expect(failed).toHaveBeenCalledTimes(1);
106
- expect(failed).toHaveBeenCalledWith(1);
107
- });
108
- it('should resolve both request with first request´s response (1/2)', async () => {
109
- const succeeded = jest.fn();
110
- const failed = jest.fn();
111
- const manager = new RequestManager({
112
- processingMode: PROCESS_FIRST_RESPONSE
113
- });
114
- const request1 = new SuccessfulRequest(manager, 1, 500);
115
- const request2 = new SuccessfulRequest(manager, 2, 1000);
116
- const req1 = request1.dispatch().then(resp => succeeded(resp)).catch(() => failed(1));
117
- const req2 = request2.dispatch().then(resp => succeeded(resp)).catch(() => failed(2));
118
- jest.runAllTimers();
119
- await req1;
120
- await req2;
121
- expect(succeeded).toHaveBeenCalledTimes(2);
122
- expect(succeeded).toHaveBeenCalledWith(1);
123
- expect(failed).not.toHaveBeenCalled();
124
- });
125
- it('should resolve both request with first request´s response (2/2)', async () => {
126
- const succeeded = jest.fn();
127
- const failed = jest.fn();
128
- const manager = new RequestManager({
129
- processingMode: PROCESS_FIRST_RESPONSE
130
- });
131
- const request1 = new SuccessfulRequest(manager, 1, 1000);
132
- const request2 = new SuccessfulRequest(manager, 2, 500);
133
- const req1 = request1.dispatch().then(resp => succeeded(resp)).catch(() => failed(1));
134
- const req2 = request2.dispatch().then(resp => succeeded(resp)).catch(() => failed(2));
135
- jest.runAllTimers();
136
- await req1;
137
- await req2;
138
- expect(succeeded).toHaveBeenCalledTimes(2);
139
- expect(succeeded.mock.calls).toEqual([[2], [2]]);
140
- expect(failed).not.toHaveBeenCalled();
141
- });
142
- it.skip('should reject first request', async () => {
143
- const succeeded = jest.fn();
144
- const failed = jest.fn();
145
- const manager = new RequestManager({
146
- processingMode: PROCESS_FIRST_RESPONSE,
147
- propagationMode: PROPAGATE_REJECT
148
- });
149
- const request1 = new SuccessfulRequest(manager, 1, 1000);
150
- const request2 = new SuccessfulRequest(manager, 2, 500);
151
- const req1 = request1.dispatch().then(resp => succeeded(resp)).catch(() => failed(1));
152
- const req2 = request2.dispatch().then(resp => succeeded(resp)).catch(() => failed(2));
153
- jest.runAllTimers();
154
- await req1;
155
- await req2;
156
- expect(succeeded).toHaveBeenCalledTimes(1);
157
- expect(succeeded).toHaveBeenCalledWith(2);
158
- expect(failed).toHaveBeenCalledTimes(1);
159
- expect(failed).toHaveBeenCalledWith(1);
160
- });
161
- it('should request the second one after the first one resolved', () => {
162
- const manager = new RequestManager({
163
- processingMode: PROCESS_SEQUENTIALLY
164
- });
165
- const request1 = new SuccessfulRequest(manager);
166
- const request2 = new SuccessfulRequest(manager);
167
- request1.dispatch();
168
- request2.dispatch();
169
- expect(manager.requestQueue.length).toBe(2);
170
- jest.runOnlyPendingTimers();
171
- expect(manager.requestQueue.length).toBe(1);
172
- jest.runOnlyPendingTimers();
173
- expect(manager.requestQueue.length).toBe(0);
174
- });
175
- it('should resolve in order of request calling', async () => {
176
- const succeeded = jest.fn();
177
- const manager = new RequestManager({
178
- processingMode: PROCESS_ORDERED_REQUEST
179
- });
180
- const request1 = new SuccessfulRequest(manager, 1, 1000);
181
- const request2 = new SuccessfulRequest(manager, 2, 500);
182
- const req1 = request1.dispatch().then(resp => succeeded(resp));
183
- const req2 = request2.dispatch().then(resp => succeeded(resp));
184
- jest.runOnlyPendingTimers();
185
- expect(manager.requestQueue.length).toBe(0);
186
- await req1;
187
- await req2;
188
- expect(succeeded.mock.calls).toEqual([[1], [2]]);
189
- });
190
- it('should time out', async () => {
191
- const succeeded = jest.fn();
192
- const failed = jest.fn();
193
- const manager = new RequestManager({
194
- timeout: 1000
195
- });
196
- const request = new NeverResolvingRequest(manager);
197
- const req = request.dispatch().then(succeeded).catch(failed);
198
- jest.runAllTimers();
199
- await req;
200
- expect(succeeded).not.toHaveBeenCalled();
201
- expect(failed).toHaveBeenCalled();
202
- });
203
- });