Passed
Push — trunk ( 58a8b0...e62b2a )
by Christian
12:52 queued 13s
created

src/Administration/Resources/app/administration/src/app/state/error.store.spec.js   B

Complexity

Total Complexity 46
Complexity/F 1.1

Size

Lines of Code 342
Function Count 42

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 46
eloc 194
mnd 4
bc 4
fnc 42
dl 0
loc 342
rs 8.72
bpm 0.0952
cpm 1.0952
noi 7
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like src/Administration/Resources/app/administration/src/app/state/error.store.spec.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import instance from './error.store';
2
3
// helper for testing action with expected mutations
4
const testAction = (action, payload, state, expectedMutations, done) => {
5
    let count = 0;
6
7
    // mock commit
8
    const commit = (commitType, commitPayload) => {
9
        const mutation = expectedMutations[count];
10
11
        try {
12
            expect(commitType).toEqual(mutation.type);
13
            expect(commitPayload).toEqual(mutation.payload);
14
        } catch (error) {
15
            done(error);
16
        }
17
18
        count += 1;
19
        if (count >= expectedMutations.length) {
20
            done();
21
        }
22
    };
23
24
    // call the action with mocked store and arguments
25
    action({ commit, state }, payload);
26
27
    // check if no mutations should have been dispatched
28
    if (expectedMutations.length === 0) {
29
        expect(count).toEqual(0);
30
        done();
31
    }
32
};
33
34
describe('Test actions at file src/app/state/error.store.js', () => {
35
    let actions = {};
36
37
    beforeAll(() => {
38
        actions = instance.actions;
39
    });
40
41
    it('addApiError', (done) => {
42
        testAction(actions.addApiError, {
43
            expression: 'dummy expression',
44
            error: 'dummy error'
45
        }, {}, [
46
            {
47
                type: 'addApiError',
48
                payload: {
49
                    expression: 'dummy expression',
50
                    error: 'dummy error'
51
                },
52
            },
53
        ], done);
54
    });
55
56
    it('resetApiErrors', (done) => {
57
        testAction(actions.resetApiErrors, null, {}, [
58
            { type: 'resetApiErrors' }
59
        ], done);
60
    });
61
62
    it('removeApiError', (done) => {
63
        testAction(actions.removeApiError, { expression: 'dummy expression' }, {}, [
64
            {
65
                type: 'removeApiError',
66
                payload: {
67
                    expression: 'dummy expression',
68
                },
69
            },
70
        ], done);
71
    });
72
73
    it('removeSystemError', (done) => {
74
        testAction(actions.removeSystemError, { id: 'dummy id' }, {}, [
75
            { type: 'removeSystemError', payload: { id: 'dummy id' } },
76
        ], done);
77
    });
78
79
    it('addSystemError', (done) => {
80
        // mock commit
81
        const commit = (commitType, commitPayload) => {
82
            try {
83
                expect(commitType).toEqual('addSystemError');
84
                expect(commitPayload).toEqual(
85
                    { error: { dummyKey: 'dummy error' }, id: 'dummy id' }
86
                );
87
            } catch (error) {
88
                done(error);
89
            }
90
91
            done();
92
        };
93
94
        // call the action with mocked store and arguments
95
        const id = actions.addSystemError({ commit }, { error: { dummyKey: 'dummy error' }, id: 'dummy id' });
96
97
        expect(id).toEqual('dummy id');
98
99
        done();
100
    });
101
});
102
103
describe('Test mutations at file src/app/state/error.store.js', () => {
104
    let mutations = {};
105
106
    let state = {
107
        api: {},
108
        system: {},
109
    };
110
111
    beforeAll(() => {
112
        mutations = instance.mutations;
113
    });
114
115
    beforeEach(() => {
116
        state = {
117
            api: {},
118
            system: {},
119
        };
120
    });
121
122
    it('removeApiError', () => {
123
        mutations.removeApiError(state, { expression: 'dummy.expression' });
124
125
        expect(state.api).toEqual({});
126
    });
127
128
    it('addApiError', () => {
129
        mutations.addApiError(state, { expression: 'dummy.expression', error: {} });
130
131
        expect(state.api).toEqual({ dummy: { expression: { selfLink: 'dummy.expression' } } });
132
    });
133
134
    it('resetApiErrors', () => {
135
        mutations.resetApiErrors(state);
136
137
        expect(state.api).toEqual({});
138
    });
139
140
    it('addSystemError', () => {
141
        mutations.addSystemError(state, { id: 'dummy id', error: { code: 'dummy code' } });
142
143
        expect(state.system).toEqual({ 'dummy id': { code: 'dummy code' } });
144
    });
145
146
    it('removeSystemError', () => {
147
        mutations.removeSystemError(state, { id: 'dummy id' });
148
149
        expect(state.system).toEqual({});
150
    });
151
});
152
153
describe('Test getters at file src/app/state/error.store.js', () => {
154
    let getters = {};
155
156
    let state = {};
157
158
    beforeAll(() => {
159
        getters = instance.getters;
160
161
        state = {
162
            api: {
163
                dummyEntityName: {
164
                    dummyId: {
165
                        dummyField: {
166
                            selfLink: 'dummy.expression',
167
                        },
168
                        0: {
169
                            age: {
170
                                selfLink: 'dummy.expression',
171
                            },
172
                        }
173
                    },
174
                },
175
                dummySystemConfig: {
176
                    dummySaleChannelId: {
177
                        dummyKey: {
178
                            error: 'dummy error',
179
                        },
180
                    },
181
                },
182
            },
183
            system: {
184
                'dummy id': {},
185
            },
186
        };
187
    });
188
189
    it('getErrorsForEntity', () => {
190
        const result = getters.getErrorsForEntity(state)('dummyEntityName', 'dummyId');
191
192
        const expected = {
193
            dummyField: { selfLink: 'dummy.expression' },
194
            0: { age: { selfLink: 'dummy.expression' } },
195
        };
196
        expect(result).toEqual(expected);
197
    });
198
199
    it('getErrorsForEntity empty entity name', () => {
200
        const result = getters.getErrorsForEntity(state)('not exists entity name', 'dummyId');
201
202
        expect(result).toBeNull();
203
    });
204
205
    it('getApiErrorFromPath', () => {
206
        const spy = jest.spyOn({
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
207
            getErrorsForEntity: () => {
208
                return {};
209
            }
210
        }, 'getErrorsForEntity');
211
        spy.mockReturnValue({ 0: { age: { selfLink: 'dummy.expression' } } });
212
213
        const result = getters.getApiErrorFromPath(state, { getErrorsForEntity: spy })('dummyEntityName', 'dummyId', [
214
            '0',
215
            'age',
216
        ]);
217
218
        expect(result).toEqual({ selfLink: 'dummy.expression' });
219
    });
220
221
    it('getApiErrorFromPath with empty', () => {
222
        const spy = jest.spyOn({
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
223
            getErrorsForEntity: () => {
224
                return {};
225
            }
226
        }, 'getErrorsForEntity');
227
        spy.mockReturnValue({ 0: { age: { selfLink: 'dummy.expression' } } });
228
229
        const result = getters.getApiErrorFromPath(state, { getErrorsForEntity: spy })('dummyEntityName', 'dummyId', [
230
            'empty field',
231
            'empty field 2',
232
        ]);
233
234
        expect(result).toBeNull();
235
    });
236
237
    it('getApiError', () => {
238
        const entity = {
239
            getEntityName: () => 'dummyEntityName',
240
            id: 'dummyId'
241
        };
242
        const field = '0.age';
243
244
        const spy = jest.spyOn({
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
245
            getApiErrorFromPath: () => {
246
                return {};
247
            },
248
        }, 'getApiErrorFromPath');
249
        spy.mockReturnValue({ selfLink: 'dummy.expression' });
250
251
        const result = getters.getApiError(state, { getApiErrorFromPath: spy })(entity, field);
252
253
        expect(result).toEqual({ selfLink: 'dummy.expression' });
254
    });
255
256
    it('getSystemConfigApiError', () => {
257
        const spy = jest.spyOn({
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
258
            getErrorsForEntity: () => {
259
                return {
260
                    dummyKey: { error: 'dummy error' }
261
                };
262
            },
263
        }, 'getErrorsForEntity');
264
265
        const result = getters.getSystemConfigApiError(state, { getErrorsForEntity: spy })('dummySystemConfig', 'dummySaleChannelId', 'dummyKey');
266
267
        expect(result).toEqual({ error: 'dummy error' });
268
    });
269
270
    it('getSystemConfigApiError with null entity name', () => {
271
        const spy = jest.spyOn({
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
272
            getErrorsForEntity: () => {
273
                return {};
274
            },
275
        }, 'getErrorsForEntity');
276
277
        const result = getters.getSystemConfigApiError(state, { getErrorsForEntity: spy })('empty entity name', 'dummySaleChannelId', 'dummyKey');
278
279
        expect(result).toBeNull();
280
    });
281
282
    it('getSystemConfigApiError with null key', () => {
283
        const spy = jest.spyOn({
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
284
            getErrorsForEntity: () => {
285
                return {
286
                    dummyKey: { error: 'dummy error' }
287
                };
288
            },
289
        }, 'getErrorsForEntity');
290
291
        const result = getters.getSystemConfigApiError(state, { getErrorsForEntity: spy })('dummySystemConfig', 'dummySaleChannelId', 'empty key');
292
293
        expect(result).toBeNull();
294
    });
295
296
    it('getSystemConfigApiError with null entity name and sale channel ud', () => {
297
        const spy = jest.spyOn({
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
298
            getErrorsForEntity: () => {
299
                return null;
300
            },
301
        }, 'getErrorsForEntity');
302
303
        const result = getters.getSystemConfigApiError(state, { getErrorsForEntity: spy })('dummySystemConfig', 'dummySaleChannelId', 'dummyKey');
304
305
        expect(result).toBeNull();
306
    });
307
308
    it('getAllApiErrors', () => {
309
        const result = getters.getAllApiErrors(state)();
310
311
        const expected = [
312
            { dummyId: { 0: { age: { selfLink: 'dummy.expression', }, }, dummyField: { selfLink: 'dummy.expression', } } },
313
            { dummySaleChannelId: { dummyKey: { error: 'dummy error' } } }
314
        ];
315
316
        expect(result).toEqual(expected);
317
    });
318
319
    it('getSystemError', () => {
320
        const result = getters.getSystemError(state)('dummy id');
321
322
        expect(result).toEqual({});
323
    });
324
325
    it('existsErrorInProperty', () => {
326
        const result = getters.existsErrorInProperty(state)('dummyEntityName', '0/age');
327
328
        expect(result).toBeTruthy();
329
    });
330
331
    it('existsErrorInProperty with empty entity', () => {
332
        const result = getters.existsErrorInProperty(state)('empty entity', 'dummyId');
333
334
        expect(result).toBeFalsy();
335
    });
336
337
    it('countSystemError', () => {
338
        const result = getters.countSystemError(state)();
339
340
        expect(result).toEqual(1);
341
    });
342
});
343