Passed
Push — trunk ( fc18b0...3f698e )
by Christian
15:27 queued 15s
created

src/Administration/Resources/app/administration/test/_setup/prepare_vue3_environment.js   A

Complexity

Total Complexity 32
Complexity/F 1.33

Size

Lines of Code 191
Function Count 24

Duplication

Duplicated Lines 33
Ratio 17.28 %

Importance

Changes 0
Metric Value
wmc 32
eloc 110
mnd 8
bc 8
fnc 24
dl 33
loc 191
rs 9.84
bpm 0.3333
cpm 1.3333
noi 4
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
/**
2
 * @package admin
3
 */
4
5
import { config, enableAutoUnmount } from '@vue/test-utils_v3';
6
import Vue from 'vue';
7
8
// eslint-disable-next-line import/no-extraneous-dependencies
9
import '@testing-library/jest-dom';
10
11
// eslint-disable-next-line import/no-extraneous-dependencies
12
import aclService from './_mocks_/acl.service.mock';
13
import feature from './_mocks_/feature.service.mock';
14
import repositoryFactory from './_mocks_/repositoryFactory.service.mock';
15
import flushPromises from '../_helper_/flushPromises';
16
17
// Setup Vue Test Utils configuration
18
config.showDeprecationWarnings = true;
19
20
// enable autoUnmount for wrapper after each test
21
enableAutoUnmount(afterEach);
0 ignored issues
show
Bug introduced by
The variable afterEach seems to be never declared. If this is a global, consider adding a /** global: afterEach */ 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...
22
23
// Make common utils available globally as well
24
global.Vue = Vue;
25
26
// Add all directives
27
const directiveRegistry = Shopware.Directive.getDirectiveRegistry();
0 ignored issues
show
Bug introduced by
The variable Shopware seems to be never declared. If this is a global, consider adding a /** global: Shopware */ 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...
28
directiveRegistry.forEach((value, key) => {
29
    global.Vue.directive(key, value);
30
});
31
32
// Add all filters
33
const filterRegistry = Shopware.Filter.getRegistry();
34
filterRegistry.forEach((value, key) => {
35
    global.Vue.filter(key, value);
36
});
37
38
// Add services
39
Shopware.Service().register('acl', () => aclService);
40
Shopware.Service().register('feature', () => feature);
41
Shopware.Feature = Shopware.Service('feature');
42
Shopware.Service().register('repositoryFactory', () => repositoryFactory);
43
44
// Provide all services
45
Shopware.Service().list().forEach(serviceKey => {
46
    config.global.provide[serviceKey] = Shopware.Service(serviceKey);
0 ignored issues
show
Bug introduced by
The variable Shopware seems to be never declared. If this is a global, consider adding a /** global: Shopware */ 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...
47
});
48
49
// Set important functions for Shopware Core
50
Shopware.Application.view = {
51
    setReactive: (target, propertyName, value) => {
52
        return Vue.set(target, propertyName, value);
53
    },
54
    root: {
55
        $tc: v => v,
56
    },
57
    i18n: {
58
        tc: v => v,
59
        te: v => v,
60
        t: v => v,
61
    },
62
};
63
64
// Prepare Context
65
Shopware.State.commit('context/setApiInstallationPath', 'installationPath');
66
Shopware.State.commit('context/setApiApiPath', '/api');
67
Shopware.State.commit('context/setApiApiResourcePath', '/api/v3');
68
Shopware.State.commit('context/setApiAssetsPath', '');
69
Shopware.State.commit('context/setApiLanguageId', '2fbb5fe2e29a4d70aa5854ce7ce3e20b');
70
Shopware.State.commit('context/setApiInheritance', false);
71
Shopware.State.commit('context/setApiSystemLanguageId', '2fbb5fe2e29a4d70aa5854ce7ce3e20b');
72
Shopware.State.commit('context/setApiLiveVersionId', '0fa91ce3e96a4bc2be4bd9ce752c3425');
73
Shopware.Context.api.authToken = {
74
    // eslint-disable-next-line max-len
75
    access: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImI0MTdkYjQ1MzMwNTY1MGIyY2QxMWVhYTBmZjRjNWJmZTVjZWYxYTI3NzBjY2JmY2M3MGY2Y2FiZDIzYWQyYmZiMzc1NTZhNDFlNGE3M2M5In0.eyJhdWQiOiJhZG1pbmlzdHJhdGlvbiIsImp0aSI6ImI0MTdkYjQ1MzMwNTY1MGIyY2QxMWVhYTBmZjRjNWJmZTVjZWYxYTI3NzBjY2JmY2M3MGY2Y2FiZDIzYWQyYmZiMzc1NTZhNDFlNGE3M2M5IiwiaWF0IjoxNjAyODM5OTgxLCJuYmYiOjE2MDI4Mzk5ODEsImV4cCI6MTYwMjg0MDU4MSwic3ViIjoiNzk5Y2NmNzY3MzZjNDkxYTgzNTA5MzA0Mjc3YzI3MTkiLCJzY29wZXMiOlsid3JpdGUiLCJhZG1pbiJdfQ.Df0EnZyZ-eY1iNCB-0x-0Ir8a8XW_HOdhq9HEcx7AbCEogHIFtU_0UPxTLX9_Wo3r-5C4FmbQrN31ReBWxkbEldMb3EU-UL4FIJA2gYhFWAXV2ZhaEJ5hRQ04n4gra0Os48vzYIEOq87_0lPPQqqVZLi68aHLVSF962VE1SkbofKqS2l2mDh9JJjnyhZavpkmpLhLkoWBBUWJS7G-EHo_-DttxPpA8W0Kgyg8Ch4Z2xqZ1r0zaB6hIS97-m8qLFHtjPhrbLW8NIMURIU3_brkkO2wFXrLKc0Y6MLJac8BVEe8VTEoEo8x8Ft2dCQU5aF2Aht3Y_55m1VjUMXBSb77A',
76
    expiry: 1602840582,
77
    // eslint-disable-next-line max-len
78
    refresh: 'def5020065a671fb38ec810a50bb627db679fd9a046ca0187215d418986fce75d3b55f7e0588c33318f3f7a280edc1e82b764a6b1fb82275e457459e58fff73afaa2aac08acd23322d398a74babbd9e02c11228985a5f140742eaa2c30af55ae350aca32e898ca9a5955c0bf057dee2b39bb5134aa6176668744fe05d6dbc9a0294bf6fa4dd6b4b07ed5d235d89005eeffc0e69ddc072e2023e522a5fd699c3e68b1dcdcc9f60c63f62ff4ed1778abfb0f3b95c4b44ad92d885bf1dca115f086b1a2368e7326f467331b6a0e65049e790c4d3a35fc1d77dfbd91da74c4d7cc449604adecd41bd84596efa4651b75bef0eeba6aef0d33338be22bf4e816584aefce9588a85d1dafbe311e330835d54dc19f43baa7a7ad63ee9573c98444219d80266b52b6e840354596d369e8350f3df18dae21a9dc607dcf70d66ddf78652a0d4083b85a832cc808d61ad15c196e1579cdea3829a8b480572f7afd590cd18fe811b5596554a58c5800756fdb1c051a461e4d7cf7c94c552ccf79d7a1368dfe8e63f4402abbaa6cabbd92437cf3f78c302ea7492dd60f5cfd8f7b4e8aa714',
79
};
80
Shopware.State.commit('setAdminLocale', {
81
    locales: ['en-GB'],
82
    locale: 'en-GB',
83
    languageId: '2fbb5fe2e29a4d70aa5854ce7ce3e20b',
84
});
85
86
// Add global mocks
87
config.global.mocks = {
88
    $tc: v => v,
89
    $t: v => v,
90
    $te: () => true,
91
    $sanitize: key => key,
92
    $device: {
93
        onResize: jest.fn(),
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...
94
        removeResizeListener: jest.fn(),
95
        getSystemKey: jest.fn(() => 'CTRL'),
96
        getViewportWidth: jest.fn(() => 1920),
97
    },
98
    $router: {
99
        replace: jest.fn(),
100
        push: jest.fn(),
101
        go: jest.fn(),
102
        resolve: jest.fn(() => {
103
            return {
104
                resolved: {
105
                    matched: [],
106
                },
107
            };
108
        }),
109
    },
110
    $route: {
111
        params: {},
112
    },
113
    $store: Shopware.State._store,
114
};
115
116
global.allowedErrors = [
117
    {
118
        method: 'warn',
119
        msg: 'No extension found for origin ""',
120
    },
121
];
122
123
global.flushPromises = flushPromises;
124
125
let consoleHasError = false;
126
let errorArgs = null;
127
const { error } = console;
128
129 View Code Duplication
global.console.error = (...args) => {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
130
    let silenceError = false;
131
    // eslint-disable-next-line array-callback-return
132
    global.allowedErrors.some(allowedError => {
133
        if (allowedError.method !== 'error') {
134
            return;
135
        }
136
137
        if (typeof allowedError.msg === 'string') {
138
            if (typeof args[0] === 'string') {
139
                const shouldBeSilenced = args[0].includes(allowedError.msg);
140
141
                if (shouldBeSilenced) {
142
                    silenceError = true;
143
                }
144
            }
145
146
            return;
147
        }
148
149
        const shouldBeSilenced = allowedError.msg.test(args[0]);
150
151
        if (shouldBeSilenced) {
152
            silenceError = true;
153
        }
154
    });
155
156
    if (!silenceError) {
157
        consoleHasError = true;
158
        errorArgs = args;
159
        error(...args);
160
    }
161
};
162
163
// Mute warnings for now as they are expected due to compat options
164
global.console.warn = () => {};
165
166
// eslint-disable-next-line jest/require-top-level-describe
167
beforeEach(() => {
168
    consoleHasError = false;
169
    errorArgs = null;
170
    global.activeFeatureFlags = ['VUE3'];
171
});
172
173
// eslint-disable-next-line jest/require-top-level-describe
174
afterEach(() => {
175
    if (consoleHasError) {
176
        // reset variable for next test
177
        consoleHasError = false;
178
179
        if (errorArgs) {
180
            throw new Error(...errorArgs);
181
        }
182
183
        throw new Error('A console.error occurred without any arguments.');
184
    }
185
});
186
187
process.on('unhandledRejection', (reason, promise) => {
188
    console.error('Unhandled Rejection at:', promise, 'reason:', reason);
189
});
190
191
// This is here to always get the Vue 3 version of templates
192
window._features_ = {
193
    VUE3: true,
194
    vue3: true,
195
};
196