Passed
Push — master ( c5a981...626eac )
by Christian
11:30 queued 10s
created

sw-product-variant-modal.spec.js ➔ getOptions   A

Complexity

Conditions 1

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 40
rs 9.256
c 0
b 0
f 0
cc 1
1
import { shallowMount } from '@vue/test-utils';
2
import 'src/module/sw-product/component/sw-product-variant-modal';
3
4
function getOptions() {
5
    return [
6
        {
7
            name: 'b',
8
            translated: {
9
                name: 'b'
10
            },
11
            group: {
12
                translated: {
13
                    name: 'color'
14
                }
15
            },
16
            position: 1
17
        },
18
        {
19
            name: 'c',
20
            translated: {
21
                name: 'c'
22
            },
23
            group: {
24
                translated: {
25
                    name: 'size'
26
                }
27
            },
28
            position: 5
29
        },
30
        {
31
            name: 'a',
32
            translated: {
33
                name: 'a'
34
            },
35
            group: {
36
                translated: {
37
                    name: 'material'
38
                }
39
            },
40
            position: 1
41
        }
42
    ];
43
}
44
45
function getVariants(returnCurrency = true) {
46
    return {
47
        price: !returnCurrency ? null : [
48
            {
49
                currencyId: 'b7d2554b0ce847cd82f3ac9bd1c0dfca',
50
                net: 24,
51
                gross: 24,
52
                linked: true,
53
                listPrice: null,
54
                extensions: []
55
            }
56
        ],
57
        childCount: 2,
58
        name: 'random product',
59
        translated: {
60
            name: 'random product'
61
        },
62
        id: '72bfaf5d90214ce592715a9649d8760a',
63
        options: getOptions()
64
    };
65
}
66
67
function createWrapper() {
68
    return shallowMount(Shopware.Component.build('sw-product-variant-modal'), {
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...
69
        propsData: {
70
            productEntity: {
71
                price: [
72
                    {
73
                        currencyId: 'b7d2554b0ce847cd82f3ac9bd1c0dfca',
74
                        net: 12,
75
                        gross: 12,
76
                        linked: true,
77
                        listPrice: null,
78
                        extensions: []
79
                    }
80
                ],
81
                productNumber: 'SW10000',
82
                childCount: 2,
83
                name: 'random product',
84
                translated: {
85
                    name: 'name'
86
                },
87
                id: '72bfaf5d90214ce592715a9649d8760a'
88
            }
89
        },
90
        mocks: {
91
            $t: key => key,
92
            $tc: key => key
93
        },
94
        provide: {
95
            repositoryFactory: {
96
                create: () => {
97
                    return {
98
                        get: () => Promise.resolve(),
99
                        search: () => Promise.resolve(getVariants())
100
                    };
101
                }
102
            },
103
            acl: {
104
                can: () => true
105
            }
106
        },
107
        stubs: {
108
            'sw-modal': true,
109
            'sw-label': true,
110
            'sw-simple-search-field': true,
111
            'sw-empty-state': true,
112
            'sw-button': true
113
        }
114
    });
115
}
116
117
describe('module/sw-product/component/sw-product-variant-modal', () => {
118
    let wrapper;
119
120
    beforeEach(() => {
121
        wrapper = createWrapper();
122
    });
123
124
    it('should be a Vue.js component', () => {
125
        expect(wrapper.vm).toBeTruthy();
126
    });
127
128
    it('should sort options by their position', () => {
129
        const sortedOptions = wrapper.vm.sortOptions(getOptions());
130
131
        expect(sortedOptions).toEqual([
132
            { name: 'a', translated: { name: 'a' }, group: { translated: { name: 'material' } }, position: 1 },
133
            { name: 'b', translated: { name: 'b' }, group: { translated: { name: 'color' } }, position: 1 },
134
            { name: 'c', translated: { name: 'c' }, group: { translated: { name: 'size' } }, position: 5 }
135
        ]);
136
    });
137
138
    it('should build variants options', () => {
139
        const builtVariantOptions = wrapper.vm.buildVariantOptions(getVariants());
140
141
        expect(builtVariantOptions).toBe('(material: a, color: b, size: c)');
142
    });
143
144
    it('should variant name', () => {
145
        const builtVariantName = wrapper.vm.buildVariantName(getVariants());
146
147
        expect(builtVariantName).toBe('random product (material: a, color: b, size: c)');
148
    });
149
150
    it('should ommit the paranthesis', () => {
151
        const builtVariantOptions = wrapper.vm.buildVariantOptions(getVariants(), ', ', true);
152
153
        expect(builtVariantOptions).toBe('material: a, color: b, size: c');
154
    });
155
156
    it('should use a custom seperator', () => {
157
        const builtVariantOptions = wrapper.vm.buildVariantOptions(getVariants(), ' - ');
158
159
        expect(builtVariantOptions).toBe('(material: a - color: b - size: c)');
160
    });
161
162
    it('should ommit the group name', () => {
163
        const builtVariantOptions = wrapper.vm.buildVariantOptions(getVariants(), ', ', false, true);
164
165
        expect(builtVariantOptions).toBe('(a, b, c)');
166
    });
167
168
    it('should get variant price of variant', () => {
169
        const variantPriceObject = wrapper.vm.getVariantPrice(getVariants());
170
        const netPrice = variantPriceObject.net;
171
        const grossPrice = variantPriceObject.gross;
172
173
        expect(netPrice).toBe(24);
174
        expect(grossPrice).toBe(24);
175
    });
176
177
    it('should get variant price of parent product', () => {
178
        const variantPriceObject = wrapper.vm.getVariantPrice(getVariants(false));
179
        const netPrice = variantPriceObject.net;
180
        const grossPrice = variantPriceObject.gross;
181
182
        expect(netPrice).toBe(12);
183
        expect(grossPrice).toBe(12);
184
    });
185
186
    it('should return the correct permissions tooltip', () => {
187
        const tooltipObject = wrapper.vm.getNoPermissionsTooltip('product.editor');
188
189
        expect(tooltipObject).toEqual({
190
            showDelay: 300,
191
            message: 'sw-privileges.tooltip.warning',
192
            appearance: 'dark',
193
            showOnDisabledElements: true,
194
            disabled: true
195
        });
196
    });
197
});
198