@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,232 +0,0 @@
1
- import "core-js/modules/es.array.includes.js";
2
- import DevServerBridge from "./index";
3
-
4
- // A libVersion for the bridge method calls
5
- const libVersion = '16.1';
6
- const devServerIp = '192.168.0.1';
7
- const devServerPort = '1337';
8
-
9
- // Mocks of the env variables.
10
- global.process.env = {
11
- IP: devServerIp,
12
- PORT: devServerPort
13
- };
14
-
15
- // Mocks of the global Headers class.
16
- global.Headers = function Headers() {};
17
-
18
- // Create a mock for the fetch method.
19
- const mockedFetchResponse = {};
20
- let mockedFetch;
21
- beforeAll(() => {
22
- global.fetch = jest.fn(); // mock fetch globally
23
- });
24
-
25
- // Create a mock for the Event class.
26
- const mockedEventCall = jest.fn();
27
- jest.mock("../Event", () => ({
28
- call: (...args) => mockedEventCall.apply(void 0, args)
29
- }));
30
-
31
- // Create a mock for the error logger.
32
- const mockedLoggerError = jest.fn();
33
- jest.mock("../../helpers", () => ({
34
- logger: {
35
- error: (...args) => {
36
- mockedLoggerError.apply(void 0, args);
37
- }
38
- }
39
- }));
40
- let dispatchCommandSpy;
41
- let processResponseSpy;
42
-
43
- /**
44
- * Updates the mock for the fetch module.
45
- * @param {boolean} throwError Whether the mocked fetch shall throw an error.
46
- */
47
- const updateMockedFetch = (throwError = false) => {
48
- mockedFetch = throwError ? jest.fn().mockRejectedValue(new Error()) : jest.fn().mockResolvedValue({
49
- json: () => mockedFetchResponse
50
- });
51
- global.fetch.mockImplementation((...args) => mockedFetch.apply(void 0, args));
52
- };
53
- describe('DevServerBridge', () => {
54
- let instance;
55
- beforeEach(() => {
56
- mockedEventCall.mockClear();
57
- updateMockedFetch();
58
- instance = new DevServerBridge();
59
- dispatchCommandSpy = jest.spyOn(instance, 'dispatchCommandForVersion');
60
- processResponseSpy = jest.spyOn(instance, 'processDevServerResponse');
61
- });
62
- describe('.constructor()', () => {
63
- it('should work as expected without parameters', () => {
64
- expect(instance.ip).toBe(devServerIp);
65
- expect(instance.port).toBe(devServerPort);
66
- });
67
- it('should apply custom parameters', () => {
68
- const customIp = '127.0.0.1';
69
- const customPort = '4711';
70
- instance = new DevServerBridge(customIp, customPort);
71
- expect(instance.ip).toBe(customIp);
72
- expect(instance.port).toBe(customPort);
73
- });
74
- });
75
- describe('.dispatchCommandsForVersion()', () => {
76
- it('should call dispatchCommand for every single command', () => {
77
- const commands = [{
78
- c: 'sendPipelineRequest'
79
- }, {
80
- c: 'openPage'
81
- }];
82
- const result = instance.dispatchCommandsForVersion(commands, libVersion);
83
- expect(result).toEqual(instance);
84
- expect(dispatchCommandSpy).toHaveBeenCalledTimes(2);
85
- expect(dispatchCommandSpy.mock.calls[0][0].c).toBe('sendPipelineRequest');
86
- expect(dispatchCommandSpy.mock.calls[0][1]).toBe(libVersion);
87
- expect(dispatchCommandSpy.mock.calls[1][0].c).toBe('openPage');
88
- expect(dispatchCommandSpy.mock.calls[1][1]).toBe(libVersion);
89
- });
90
- it('should call dispatchCommand when no commands where passed', () => {
91
- const result = instance.dispatchCommandsForVersion(null, libVersion);
92
- expect(result).toEqual(instance);
93
- expect(dispatchCommandSpy).toHaveBeenCalledTimes(0);
94
- });
95
- });
96
- describe('.dispatchCommandForVersion()', () => {
97
- it('should dispatch a webStorage entry command', done => {
98
- const name = 'getWebStorageEntry';
99
- const command = {
100
- c: name
101
- };
102
- const result = instance.dispatchCommandForVersion(command, libVersion);
103
- expect(result).toEqual(instance);
104
- // The dispatch method has async behavior. So we wait for the next tick before we check.
105
- setTimeout(() => {
106
- expect(mockedFetch).toHaveBeenCalledTimes(1);
107
- expect(mockedFetch.mock.calls[0][0].endsWith('web_storage')).toBe(true);
108
- expect(mockedFetch.mock.calls[0][1].body.includes(name)).toBe(true);
109
- expect(processResponseSpy).toHaveBeenCalledTimes(1);
110
- done();
111
- }, 0);
112
- });
113
- it('should dispatch a httpRequest command', done => {
114
- const name = 'sendHttpRequest';
115
- const command = {
116
- c: name
117
- };
118
- const result = instance.dispatchCommandForVersion(command, libVersion);
119
- expect(result).toEqual(instance);
120
- // The dispatch method has async behavior. So we wait for the next tick before we check.
121
- setTimeout(() => {
122
- expect(mockedFetch).toHaveBeenCalledTimes(1);
123
- expect(mockedFetch.mock.calls[0][0].endsWith('http_request')).toBe(true);
124
- expect(mockedFetch.mock.calls[0][1].body.includes(name)).toBe(true);
125
- expect(processResponseSpy).toHaveBeenCalledTimes(1);
126
- done();
127
- }, 0);
128
- });
129
- it('should not do anything if the command is not whitelisted', done => {
130
- const command = {
131
- c: 'openPage'
132
- };
133
- const result = instance.dispatchCommandForVersion(command, libVersion);
134
- expect(result).toEqual(instance);
135
- // The dispatch method has async behavior. So we wait for the next tick before we check.
136
- setTimeout(() => {
137
- expect(mockedFetch).toHaveBeenCalledTimes(0);
138
- expect(processResponseSpy).toHaveBeenCalledTimes(0);
139
- done();
140
- }, 0);
141
- });
142
- it('should not do anything if the command is empty', done => {
143
- const result = instance.dispatchCommandForVersion(null, libVersion);
144
- expect(result).toEqual(instance);
145
- // The dispatch method has async behavior. So we wait for the next tick before we check.
146
- setTimeout(() => {
147
- expect(mockedFetch).toHaveBeenCalledTimes(0);
148
- expect(processResponseSpy).toHaveBeenCalledTimes(0);
149
- done();
150
- }, 0);
151
- });
152
- it('should handle fetch errors', done => {
153
- updateMockedFetch(true);
154
- const command = {
155
- c: 'sendPipelineRequest'
156
- };
157
- const result = instance.dispatchCommandForVersion(command, libVersion);
158
- expect(result).toEqual(instance);
159
- // The dispatch method has async behavior. So we wait for the next tick before we check.
160
- setTimeout(() => {
161
- expect(mockedFetch).toHaveBeenCalledTimes(1);
162
- expect(processResponseSpy).toHaveBeenCalledTimes(0);
163
- expect(mockedLoggerError).toHaveBeenCalledTimes(1);
164
- expect(mockedLoggerError.mock.calls[0][0]).toBeInstanceOf(Error);
165
- done();
166
- }, 0);
167
- });
168
- });
169
- describe('.processDevServerResponse()', () => {
170
- it('should handle a response commands as expected', () => {
171
- const serial = 'abc123';
172
- const commands = [{
173
- c: 'pipelineResponse',
174
- p: {
175
- serial,
176
- error: null,
177
- output: {}
178
- }
179
- }, {
180
- c: 'httpResponse',
181
- p: {
182
- serial,
183
- error: null,
184
- response: {}
185
- }
186
- }, {
187
- c: 'dataResponse',
188
- p: {
189
- serial,
190
- status: 200,
191
- body: '',
192
- bodyContentType: ''
193
- }
194
- }, {
195
- c: 'webStorageResponse',
196
- p: {
197
- serial,
198
- age: 40,
199
- value: {}
200
- }
201
- }, {
202
- c: 'unknownResponse',
203
- p: {}
204
- }];
205
- const result = instance.processDevServerResponse({
206
- cmds: commands
207
- });
208
- expect(result).toEqual(instance);
209
- expect(mockedEventCall).toHaveBeenCalledTimes(5);
210
- const [pipelineResponse, httpResponse, dataResponse, webStorageResponse, unknownResponse] = mockedEventCall.mock.calls;
211
- expect(pipelineResponse[0]).toBe('pipelineResponse');
212
- expect(pipelineResponse[1]).toHaveLength(3);
213
- expect(pipelineResponse[1][1]).toBe(serial);
214
- expect(httpResponse[0]).toBe('httpResponse');
215
- expect(httpResponse[1]).toHaveLength(3);
216
- expect(httpResponse[1][1]).toBe(serial);
217
- expect(dataResponse[0]).toBe('dataResponse');
218
- expect(dataResponse[1]).toHaveLength(4);
219
- expect(dataResponse[1][0]).toBe(serial);
220
- expect(webStorageResponse[0]).toBe('webStorageResponse');
221
- expect(webStorageResponse[1]).toHaveLength(3);
222
- expect(webStorageResponse[1][0]).toBe(serial);
223
- expect(unknownResponse[0]).toBe('unknownResponse');
224
- expect(unknownResponse[1]).toHaveLength(0);
225
- });
226
- it('should work as expected when nothing was passed', () => {
227
- const result = instance.processDevServerResponse(null);
228
- expect(result).toEqual(instance);
229
- expect(mockedEventCall).toHaveBeenCalledTimes(0);
230
- });
231
- });
232
- });
@@ -1,244 +0,0 @@
1
- import errorManager, { emitter } from '.';
2
- import { DEFAULT_CONTEXT, DEFAULT_SEVERITY } from "../../constants/ErrorManager";
3
- describe('ErrorManager', () => {
4
- beforeEach(() => {
5
- errorManager.errorQueue.clear();
6
- errorManager.messages = {};
7
- });
8
- describe('errorManager.validate', () => {
9
- it('should accept a valid error object', () => {
10
- const code = 'EUNKNOWN';
11
- const message = 'Something went horribly wrong!';
12
- const source = 'pipeline';
13
- const response = errorManager.validate({
14
- code,
15
- message,
16
- source
17
- });
18
- expect(response).toEqual(true);
19
- });
20
- it('should accept a valid error object with cb message', () => {
21
- const code = 'EUNKNOWN';
22
- const message = jest.fn();
23
- const source = 'pipeline';
24
- const response = errorManager.validate({
25
- code,
26
- message,
27
- source
28
- });
29
- expect(response).toEqual(true);
30
- });
31
- it('should reject an error object with missing mandatory fields', () => {
32
- const response = errorManager.validate();
33
- expect(response).toEqual(false);
34
- });
35
- it('should reject an error object with fields that are not a string', () => {
36
- const code = 404;
37
- const message = 'Something went horribly wrong!';
38
- const source = 'pipeline';
39
- const response = errorManager.validate({
40
- code,
41
- message,
42
- source
43
- });
44
- expect(response).toEqual(false);
45
- });
46
- });
47
- describe('errorManager.getMessage', () => {
48
- const code = 'EUNKNOWN';
49
- const source = 'pipeline';
50
- const context = 'shopgate.catalog.getFoo';
51
- const message = 'Test Message';
52
- it('should return the null when no override message is found', () => {
53
- const errorMessage = errorManager.getMessage({
54
- code,
55
- context,
56
- source
57
- });
58
- expect(errorMessage).toBeNull();
59
- });
60
- it('should return the message', () => {
61
- errorManager.setMessage({
62
- code,
63
- context,
64
- message
65
- });
66
- const errorMessage = errorManager.getMessage({
67
- code,
68
- context,
69
- source,
70
- message
71
- });
72
- expect(errorMessage).toBe(message);
73
- });
74
- it('should use callback for error message', () => {
75
- const overrideMessage = jest.fn().mockReturnValue('CB Error');
76
- errorManager.setMessage({
77
- code,
78
- context,
79
- message: overrideMessage
80
- });
81
- const error = {
82
- code,
83
- context,
84
- source,
85
- message
86
- };
87
- expect(errorManager.getMessage(error)).toBe('CB Error');
88
- expect(overrideMessage).toBeCalledWith(error);
89
- });
90
- });
91
- describe('errorManager.setMessage', () => {
92
- it('should add an override message', () => {
93
- const code = 'EUNKNOWN';
94
- const context = 'shopgate.catalog.getUser';
95
- const message = 'Something went horribly wrong!';
96
- const source = 'pipeline';
97
- errorManager.setMessage({
98
- code,
99
- context,
100
- message,
101
- source
102
- });
103
- expect(errorManager.messages[`${source}-${context}-${code}`]).toEqual(message);
104
- });
105
- it('should add an override message with no set context', () => {
106
- const code = 'EUNKNOWN';
107
- const message = 'Something went horribly wrong!';
108
- const source = 'pipeline';
109
- errorManager.setMessage({
110
- code,
111
- message,
112
- source
113
- });
114
- expect(errorManager.messages[`${source}-${DEFAULT_CONTEXT}-${code}`]).toEqual(message);
115
- });
116
- it('should ignore setting a message with missing error object', () => {
117
- const code = 'EUNKNOWN';
118
- const source = 'pipeline';
119
- errorManager.setMessage();
120
- expect(errorManager.messages[`${source}-${DEFAULT_CONTEXT}-${code}`]).toBeUndefined();
121
- });
122
- it('should ignore setting a message with missing mandatory fields', () => {
123
- const code = 'EUNKNOWN';
124
- const message = 'Something went horribly wrong!';
125
- const source = 'pipeline';
126
- errorManager.setMessage({
127
- message,
128
- source
129
- });
130
- expect(errorManager.messages[`${source}-${DEFAULT_CONTEXT}-${code}`]).toBeUndefined();
131
- });
132
- it('should ignore setting a message with invalid input', () => {
133
- const code = 404;
134
- const message = 'Something went horribly wrong!';
135
- const source = 'pipeline';
136
- errorManager.setMessage({
137
- code,
138
- message,
139
- source
140
- });
141
- expect(errorManager.messages[`${source}-${DEFAULT_CONTEXT}-${code}`]).toBeUndefined();
142
- });
143
- });
144
- describe('errorManager.queue', () => {
145
- it('should not queue a missing error', () => {
146
- const code = 'EUNKNOWN';
147
- const source = 'pipeline';
148
- errorManager.queue();
149
- expect(errorManager.messages[`${source}-${DEFAULT_CONTEXT}-${code}`]).toBeUndefined();
150
- });
151
- it('should not queue an invalid error', () => {
152
- const code = 404;
153
- const message = 'Something went horribly wrong!';
154
- const source = 'pipeline';
155
- errorManager.queue({
156
- code,
157
- message,
158
- source
159
- });
160
- expect(errorManager.errorQueue.has(`${source}-${DEFAULT_CONTEXT}-${code}`)).toEqual(false);
161
- });
162
- it('should queue errors only once', () => {
163
- const code = 'EUNKNOWN';
164
- const message = 'Something went horribly wrong!';
165
- const source = 'pipeline';
166
- const callback = jest.fn();
167
- emitter.addListener('pipeline', callback);
168
- errorManager.queue({
169
- code,
170
- message,
171
- source
172
- });
173
- errorManager.queue({
174
- code,
175
- message,
176
- source
177
- });
178
- expect(errorManager.errorQueue.has(`${source}-${DEFAULT_CONTEXT}-${code}`)).toEqual(true);
179
- expect(errorManager.errorQueue.size).toEqual(1);
180
- });
181
- });
182
- it('should not dispatch when there are no errors', async () => {
183
- const callback = jest.fn();
184
- emitter.addListener('pipeline', callback);
185
- const dispatch = errorManager.dispatch();
186
- expect(dispatch).toEqual(false);
187
- });
188
- it('should dispatch the errors through events', async () => {
189
- const code = 'EUNKNOWN';
190
- const message = 'Something went horribly wrong!';
191
- const source = 'pipeline';
192
- const callback = jest.fn();
193
- const callback2 = jest.fn();
194
- emitter.addListener('pipeline', callback);
195
- emitter.addListener('pipeline', callback2);
196
- await errorManager.queue({
197
- code,
198
- message,
199
- source
200
- });
201
- await errorManager.queue({
202
- code: 'EUNKNOWN2',
203
- message,
204
- source
205
- });
206
- expect(errorManager.errorQueue.size).toEqual(2);
207
- errorManager.dispatch();
208
- expect(callback).toBeCalled();
209
- expect(callback2).toBeCalled();
210
- expect(errorManager.errorQueue.size).toEqual(0);
211
- });
212
- it('should set a queue entry with meta data', () => {
213
- const code = 'EUNKNOWN';
214
- const replacementMessage = 'Replacement Message';
215
- const message = 'Original Message';
216
- const source = 'pipeline';
217
-
218
- // Setup a replacement message for the error code.
219
- errorManager.setMessage({
220
- code,
221
- source,
222
- message: replacementMessage
223
- });
224
- const callback = jest.fn();
225
- emitter.addListener('pipeline', callback);
226
- errorManager.queue({
227
- message,
228
- code,
229
- source
230
- });
231
- errorManager.dispatch();
232
- expect(callback).toBeCalledWith({
233
- id: `${source}-${DEFAULT_CONTEXT}-${code}`,
234
- context: DEFAULT_CONTEXT,
235
- message: replacementMessage,
236
- code,
237
- source,
238
- meta: {
239
- message
240
- },
241
- severity: DEFAULT_SEVERITY
242
- });
243
- });
244
- });
@@ -1,48 +0,0 @@
1
- const pipelineName = 'TestPipeline';
2
- const dependecies1 = ['TestPipeline1', 'TestPipeline2'];
3
- const dependecies2 = ['TestPipeline3', 'TestPipeline4'];
4
- const defaultResult = new Set();
5
- const mockLogGroup = jest.fn();
6
- jest.mock("../../helpers/logGroup", () => (...args) => mockLogGroup(args));
7
-
8
- /**
9
- * Creates a fresh instance
10
- * @return {PipelineDependencies}
11
- */
12
- const getInstance = () => {
13
- const instance = jest.requireActual("./index").default;
14
- return instance;
15
- };
16
- describe('PipelineDependencies', () => {
17
- beforeEach(() => {
18
- jest.resetModules();
19
- jest.clearAllMocks();
20
- });
21
- it('should get default', () => {
22
- const pipelineDependencies = getInstance();
23
- let result = pipelineDependencies.get(pipelineName);
24
- expect(result).toEqual(defaultResult);
25
- result = pipelineDependencies.get();
26
- expect(result).toEqual(defaultResult);
27
- expect(mockLogGroup).toHaveBeenCalledTimes(0);
28
- });
29
- it('should set default', () => {
30
- const pipelineDependencies = getInstance();
31
- pipelineDependencies.set(pipelineName);
32
- const result = pipelineDependencies.get(pipelineName);
33
- expect(result).toEqual(defaultResult);
34
- expect(mockLogGroup).toHaveBeenCalledTimes(0);
35
- });
36
- it('should set dependencies', () => {
37
- const pipelineDependencies = getInstance();
38
- pipelineDependencies.set(pipelineName, dependecies1);
39
- let result = new Set(dependecies1);
40
- expect(pipelineDependencies.get(pipelineName)).toEqual(result);
41
-
42
- // Add more
43
- pipelineDependencies.set(pipelineName, dependecies2);
44
- result = new Set([].concat(dependecies1, dependecies2));
45
- expect(pipelineDependencies.get(pipelineName)).toEqual(result);
46
- expect(mockLogGroup).toHaveBeenCalledTimes(2);
47
- });
48
- });