Passed
Push — trunk ( 0f15cc...900a09 )
by Christian
12:21 queued 14s
created

  B

Complexity

Conditions 2

Size

Total Lines 67
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 52
dl 0
loc 67
rs 8.5709
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import { shallowMount } from '@vue/test-utils';
2
import type Shopware from 'src/core/shopware';
3
import swFirstRunWizardWelcome from 'src/module/sw-first-run-wizard/view/sw-first-run-wizard-welcome';
4
import swPluginCard from 'src/module/sw-first-run-wizard/component/sw-plugin-card';
5
import 'src/app/component/base/sw-container';
6
import 'src/app/component/base/sw-button-process';
7
import 'src/app/component/base/sw-button';
8
import 'src/app/component/base/sw-modal';
9
import 'src/app/component/form/sw-select-field';
10
import 'src/app/component/form/field-base/sw-block-field';
11
import 'src/app/component/form/field-base/sw-base-field';
12
import 'src/app/component/form/field-base/sw-field-error';
13
import 'src/app/component/form/sw-password-field';
14
import 'src/app/component/form/sw-text-field';
15
import 'src/app/component/form/field-base/sw-contextual-field';
16
17
Shopware.Component.register('sw-first-run-wizard-welcome', swFirstRunWizardWelcome);
18
Shopware.Component.register('sw-plugin-card', swPluginCard);
19
20
const setLocaleWithIdMock = jest.fn(() => Promise.resolve({}));
21
22
Shopware.Service().register('localeHelper', () => {
23
    return {
24
        setLocaleWithId: setLocaleWithIdMock
25
    };
26
});
27
28
const languagePlugins = {
29
    items: [{
30
        extensions: [],
31
        name: 'SwagLanguagePack',
32
        label: 'Shopware Language Pack',
33
        // eslint-disable-next-line max-len
34
        shortDescription: 'With all languages in one extension, switching languages in your online shop has never been easier! Simply choose the languages for your admin and storefront for you and your customers.',
35
        iconPath: 'https://sbp-plugin-images.s3.eu-west-1.amazonaws.com/php6TDNDF',
36
        version: null,
37
        description: null,
38
        changelog: null,
39
        releaseDate: null,
40
        installed: false,
41
        active: false,
42
        language: null,
43
        region: null,
44
        category: null,
45
        manufacturer: 'shopware AG',
46
        position: null,
47
        isCategoryLead: false,
48
    }],
49
    total: 1,
50
};
51
52
const userProfile = {
53
    data: {
54
        id: 'c0cf3e77ad0e4ed8b855d3eb820dbfb4',
55
        type: 'user',
56
        relationships: [],
57
        attributes: {
58
            id: 'c0cf3e77ad0e4ed8b855d3eb820dbfb4',
59
            localeId: 'c2e20247ec8e42c0b224a03d458eb8e0',
60
        },
61
    },
62
};
63
64
const searchUser = {
65
    data: [
66
        {
67
            id: 'c0cf3e77ad0e4ed8b855d3eb820dbfb4',
68
            type: 'user',
69
            relationships: [],
70
            attributes: {
71
                id: 'c0cf3e77ad0e4ed8b855d3eb820dbfb4',
72
                localeId: 'c2e20247ec8e42c0b224a03d458eb8e0',
73
            },
74
        },
75
    ],
76
};
77
78
const searchLanguage = [
79
    {
80
        id: '2fbb5fe2e29a4d70aa5854ce7ce3e20b',
81
        type: 'language',
82
        locale: {
83
            id: 'c2e20247ec8e42c0b224a03d458eb8e0',
84
            translated: {
85
                name: 'English (US)',
86
                territory: 'United States',
87
            },
88
        },
89
    }, {
90
        id: 'ba44d1a797b8474b9497b59837c63efb',
91
        type: 'language',
92
        locale: {
93
            id: '4aed63b2afcd44049ba0cd898769cdbb',
94
            translated: {
95
                name: 'German',
96
                territory: 'Germany',
97
            },
98
        },
99
    },
100
];
101
102
const searchSnippetSet = [];
103
104
/**
105
 * @package merchant-services
106
 */
107
describe('src/module/sw-first-run-wizard/view/sw-first-run-wizard-welcome', () => {
108
    async function createWrapper() {
109
        return shallowMount(await Shopware.Component.build('sw-first-run-wizard-welcome'), {
110
            stubs: {
111
                'sw-container': await Shopware.Component.build('sw-container'),
112
                'sw-plugin-card': await Shopware.Component.build('sw-plugin-card'),
113
                'sw-button-process': await Shopware.Component.build('sw-button-process'),
114
                'sw-button': await Shopware.Component.build('sw-button'),
115
                'sw-modal': await Shopware.Component.build('sw-modal'),
116
                'sw-select-field': await Shopware.Component.build('sw-select-field'),
117
                'sw-block-field': await Shopware.Component.build('sw-block-field'),
118
                'sw-base-field': await Shopware.Component.build('sw-base-field'),
119
                'sw-field-error': await Shopware.Component.build('sw-field-error'),
120
                'sw-password-field': await Shopware.Component.build('sw-password-field'),
121
                'sw-text-field': await Shopware.Component.build('sw-text-field'),
122
                'sw-contextual-field': await Shopware.Component.build('sw-contextual-field'),
123
                'sw-icon': true,
124
                'sw-loader': true,
125
            },
126
            provide: {
127
                languagePluginService: {
128
                    getPlugins: () => Promise.resolve(languagePlugins),
129
                },
130
                userService: {
131
                    getUser: () => Promise.resolve(userProfile),
132
                },
133
                loginService: {
134
                    verifyUserToken: () => Promise.resolve(),
135
                },
136
                extensionStoreActionService: {},
137
                cacheApiService: {
138
                    clear: () => Promise.resolve()
139
                },
140
                extensionHelperService: {
141
                    downloadAndActivateExtension: (extension) => Promise.resolve(extension),
142
                },
143
                shortcutService: {
144
                    startEventListener: () => {},
145
                    stopEventListener: () => {}
146
                },
147
                validationService: {
148
                    validate: () => true,
149
                },
150
                repositoryFactory: {
151
                    create: (entity: String) => {
152
                        switch (entity) {
153
                            case 'language':
154
                                return {
155
                                    search: () => Promise.resolve(searchLanguage)
156
                                };
157
                            case 'user':
158
                                return {
159
                                    search: () => Promise.resolve(searchUser),
160
                                    get: () => Promise.resolve(searchUser),
161
                                    save: () => Promise.resolve(),
162
                                };
163
                            case 'snippet_set':
164
                                return {
165
                                    search: () => Promise.resolve(searchSnippetSet)
166
                                };
167
                            default:
168
                                throw new Error(`No repositoryFactory registered for entity "${entity}"`);
169
                        }
170
                    },
171
                },
172
            },
173
            mixins: [
174
                Shopware.Mixin.getByName('notification'),
175
            ]
176
        });
177
    }
178
179
    it('should install the SwagLanguagePack plugin and show the language switch modal', async () => {
180
        const wrapper = await createWrapper();
181
        await wrapper.vm.$nextTick();
182
183
        expect(wrapper.findAll('.sw-plugin-card').length).toBe(1);
184
185
        await wrapper.find('.button-plugin-install').trigger('click');
186
        await flushPromises();
187
        await wrapper.vm.$nextTick();
188
189
        const modal = await wrapper.find('.sw-first-run-wizard-confirmLanguageSwitch-modal');
190
        expect(modal.isVisible()).toBe(true);
191
192
        const languageSelect = await modal.find('[name=sw-field--user-localeId]');
193
        await languageSelect.findAll('option').at(1).setSelected();
194
        const selectedLanguage = languageSelect.find('option:checked').element.value;
195
        await modal.find('[name=sw-field--user-pw]').setValue('p4ssw0rd');
196
        await modal.find('.sw-button--primary').trigger('click');
197
198
        expect(setLocaleWithIdMock).toHaveBeenCalledWith(selectedLanguage);
199
    });
200
});
201