Passed
Push — trunk ( c4454b...a860c5 )
by Christian
14:18 queued 12s
created

sw-plugin-card.spec.ts ➔ createWrapper   A

Complexity

Conditions 1

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
dl 0
loc 21
rs 9.55
c 0
b 0
f 0
1
import { shallowMount } from '@vue/test-utils';
2
import type { Wrapper } from '@vue/test-utils';
3
import 'src/module/sw-extension/mixin/sw-extension-error.mixin';
4
import SwPluginCard from 'src/module/sw-first-run-wizard/component/sw-plugin-card';
5
import SwExtensionIcon from 'src/app/asyncComponent/extension/sw-extension-icon';
6
import 'src/app/component/base/sw-button-process';
7
import 'src/app/component/base/sw-button';
8
import 'src/app/component/utils/sw-loader';
9
10
Shopware.Component.register('sw-plugin-card', SwPluginCard);
11
Shopware.Component.register('sw-extension-icon', SwExtensionIcon);
12
13
async function createWrapper(plugin: unknown, showDescription: boolean): Promise<Wrapper<SwPluginCard>> {
14
    return shallowMount(await Shopware.Component.build('sw-plugin-card'), {
15
        propsData: {
16
            plugin,
17
            showDescription,
18
        },
19
        provide: {
20
            cacheApiService: {
21
                clear: () => { return Promise.resolve(); },
22
            },
23
            extensionHelperService: {
24
                downloadAndActivateExtension: () => { return Promise.resolve(); },
25
            },
26
        },
27
        stubs: {
28
            'sw-extension-icon': await Shopware.Component.build('sw-extension-icon'),
29
            'sw-icon': true,
30
            'sw-button-process': await Shopware.Component.build('sw-button-process'),
31
            'sw-button': await Shopware.Component.build('sw-button'),
32
            'sw-loader': await Shopware.Component.build('sw-loader'),
33
        },
34
    });
35
}
36
37
describe('src/module/sw-first-run-wizard/component/sw-plugin-card', () => {
38
    it('displays correct icon and basic information', async () => {
39
        const pluginConfig = {
40
            iconPath: 'path/to/plugin-icon',
41
            active: true,
42
            label: 'example extension',
43
            manufacturer: 'shopware AG',
44
            shortDescription: 'this is a example extension',
45
        };
46
47
        const wrapper = await createWrapper(pluginConfig, true);
48
49
        const extensionIcon = wrapper.get('.sw-extension-icon');
50
51
        expect(extensionIcon.vm).toBeDefined();
52
        expect(extensionIcon.props('src')).toBe(pluginConfig.iconPath);
53
54
        expect(wrapper.get('.sw-plugin-card__label').text()).toBe(pluginConfig.label);
55
        expect(wrapper.get('.sw-plugin-card__manufacturer').text()).toBe(pluginConfig.manufacturer);
56
        expect(wrapper.get('.sw-plugin-card__short-description').text()).toBe(pluginConfig.shortDescription);
57
    });
58
59
    it('hides description', async () => {
60
        const pluginConfig = {
61
            iconPath: 'path/to/plugin-icon',
62
            active: true,
63
            label: 'example extension',
64
            manufacturer: 'shopware AG',
65
            shortDescription: 'this is a example extension',
66
        };
67
68
        const wrapper = await createWrapper(pluginConfig, false);
69
70
        expect(wrapper.find('.sw-plugin-card__short-description').exists()).toBe(false);
71
    });
72
73
    it('truncates short description correctly', async () => {
74
        const shortDescription = Array.from({ length: 50 }, () => 'a').join(', ');
75
        expect(shortDescription.length).toBeGreaterThan(140);
76
77
        const pluginConfig = {
78
            iconPath: 'path/to/plugin-icon',
79
            active: true,
80
            label: 'example extension',
81
            manufacturer: 'shopware AG',
82
            shortDescription,
83
        };
84
85
        const wrapper = await createWrapper(pluginConfig, true);
86
87
        const truncatedDescription = wrapper.get('.sw-plugin-card__short-description').text();
88
89
        expect(truncatedDescription).toHaveLength(140);
90
        expect(truncatedDescription.endsWith('...')).toBe(true);
91
        expect(truncatedDescription.slice(0, 137)).toEqual(shortDescription.slice(0, 137));
92
    });
93
94
    it('displays that an extension is already installed', async () => {
95
        const wrapper = await createWrapper({
96
            iconPath: 'path/to/plugin-icon',
97
            active: true,
98
            label: 'example extension',
99
            manufacturer: 'shopware AG',
100
            shortDescription: 'short description',
101
        }, true);
102
103
        const isInstalled = wrapper.get('.plugin-installed');
104
105
        expect(isInstalled.get('sw-icon-stub').attributes('name')).toBe('regular-check-circle-s');
106
        expect(isInstalled.text()).toBe('sw-first-run-wizard.general.pluginInstalled');
107
    });
108
109
    it('can install an extension', async () => {
110
        const wrapper = await createWrapper({
111
            name: 'SwExamplePlugin',
112
            iconPath: 'path/to/plugin-icon',
113
            active: false,
114
            label: 'example extension',
115
            manufacturer: 'shopware AG',
116
            shortDescription: 'short description',
117
        }, true);
118
119
        const downloadSpy = jest.spyOn(wrapper.vm.extensionHelperService, 'downloadAndActivateExtension');
120
        const cacheApiSpy = jest.spyOn(wrapper.vm.cacheApiService, 'clear');
121
122
        await wrapper.get('.sw-button-process').trigger('click');
123
124
        expect(downloadSpy).toHaveBeenCalled();
125
        expect(downloadSpy).toHaveBeenCalledWith('SwExamplePlugin');
126
        expect(cacheApiSpy).toHaveBeenCalled();
127
128
        expect(wrapper.emitted('onPluginInstalled')).toEqual([['SwExamplePlugin']]);
129
    });
130
131
    it('displays errors on failed installation', async () => {
132
        const wrapper = await createWrapper({
133
            name: 'SwExamplePlugin',
134
            iconPath: 'path/to/plugin-icon',
135
            active: false,
136
            label: 'example extension',
137
            manufacturer: 'shopware AG',
138
            shortDescription: 'short description',
139
        }, true);
140
141
        const downloadError = new Error('installation error');
142
143
        const downloadSpy = jest.spyOn(wrapper.vm.extensionHelperService, 'downloadAndActivateExtension');
144
        downloadSpy.mockImplementationOnce(() => { return Promise.reject(downloadError); });
145
146
        const showExtensionErrorsSpy = jest.spyOn(wrapper.vm, 'showExtensionErrors');
147
        showExtensionErrorsSpy.mockImplementationOnce(() => {});
148
149
        const cacheApiSpy = jest.spyOn(wrapper.vm.cacheApiService, 'clear');
150
151
        await wrapper.get('.sw-button-process').trigger('click');
152
153
        expect(downloadSpy).toHaveBeenCalled();
154
        expect(downloadSpy).toHaveBeenCalledWith('SwExamplePlugin');
155
        expect(cacheApiSpy).toHaveBeenCalled();
156
        expect(showExtensionErrorsSpy).toHaveBeenCalled();
157
        expect(showExtensionErrorsSpy).toHaveBeenCalledWith(downloadError);
158
159
        expect(wrapper.emitted('onPluginInstalled')).toEqual([['SwExamplePlugin']]);
160
    });
161
});
162