Passed
Push — 6.4 ( a2ab34...70d76f )
by Christian
13:23 queued 14s
created

  A

Complexity

Conditions 2

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 22
dl 0
loc 30
rs 9.352
c 0
b 0
f 0
1
import Vue from 'vue';
2
import Vuex from 'vuex';
3
import { createLocalVue, shallowMount } from '@vue/test-utils';
4
import type { Wrapper } from '@vue/test-utils';
5
import 'src/module/sw-order/mixin/cart-notification.mixin';
6
import swOrderCreateInitialModal from 'src/module/sw-order/component/sw-order-create-initial-modal';
7
import 'src/app/component/base/sw-button';
8
9
import orderStore from 'src/module/sw-order/state/order.store';
10
11
const lineItem = {
12
    label: 'Product',
13
    productId: 'product1'
14
};
15
16
const cartResponse = {
17
    data: {
18
        token: 'token',
19
        deliveries: [],
20
        lineItems: [],
21
    },
22
};
23
24
Shopware.Component.register('sw-order-create-initial-modal', swOrderCreateInitialModal);
25
26
async function createWrapper(): Promise<Wrapper<Vue>> {
27
    const localVue = createLocalVue();
28
    localVue.use(Vuex);
29
30
    return shallowMount(await Shopware.Component.build('sw-order-create-initial-modal'), {
31
        localVue,
32
        stubs: {
33
            'sw-modal': {
34
                template: `
35
                    <div class="sw-modal">
36
                        <slot name="default"></slot>
37
                        <slot name="modal-footer"></slot>
38
                    </div>`
39
            },
40
            'sw-container': {
41
                template: '<div class="sw-container"><slot></slot></div>'
42
            },
43
            'sw-tabs': {
44
                data() {
45
                    return { active: 'customer' };
46
                },
47
                template: '<div class="sw-tabs"><slot></slot><slot name="content" v-bind="{ active }"></slot></div>'
48
            },
49
            'sw-tabs-item': true,
50
            'sw-order-customer-grid': true,
51
            'sw-order-line-items-grid-sales-channel': true,
52
            'sw-order-create-options': true,
53
            'sw-button': await Shopware.Component.build('sw-button'),
54
            'sw-icon': true,
55
        },
56
    });
57
}
58
59
60
const tabs = [
61
    '.sw-order-create-initial-modal__tab-product',
62
    '.sw-order-create-initial-modal__tab-options',
63
];
64
65
describe('src/module/sw-order/view/sw-order-create-initial-modal', () => {
66
    beforeAll(() => {
67
        Shopware.Service().register('cartStoreService', () => {
68
            return {
69
                cancelCart: () => Promise.resolve({}),
70
                saveLineItem: () => Promise.resolve({
71
                    data: {
72
                        ...cartResponse.data,
73
                        lineItems: [{ ...lineItem }]
74
                    },
75
                }),
76
                removeLineItems: () => Promise.resolve(cartResponse),
77
                disableAutomaticPromotions: () => Promise.resolve(cartResponse),
78
                addMultipleLineItems: () => Promise.resolve(cartResponse),
79
                modifyShippingCosts: () => Promise.resolve({ data: { ...cartResponse } }),
80
            };
81
        });
82
83
        Shopware.Service().register('contextStoreService', () => {
84
            return {
85
                updateContext: () => Promise.resolve({}),
86
            };
87
        });
88
89
        Shopware.State.registerModule('swOrder', orderStore);
90
    });
91
92
    afterEach(() => {
93
        Shopware.State.commit('swOrder/setCart', {
94
            token: null,
95
            lineItems: [],
96
            deliveries: [],
97
        });
98
    });
99
100
    it('should disabled other tabs if customer is not selected', async () => {
101
        const wrapper = await createWrapper();
102
103
        tabs.forEach(tab => {
104
            expect(wrapper.find(tab).attributes().disabled).toBeTruthy();
105
        });
106
    });
107
108
    it('should enable other tabs if customer is selected', async () => {
109
        Shopware.State.commit('swOrder/setCustomer', {
110
            id: '1234'
111
        });
112
113
        const wrapper = await createWrapper();
114
115
        tabs.forEach(tab => {
116
            expect(wrapper.find(tab).attributes().disabled).toBeUndefined();
117
        });
118
    });
119
120
    it('should show tab content correctly', async () => {
121
        Shopware.State.commit('swOrder/setCustomer', {
122
            id: '1234'
123
        });
124
        const wrapper = await createWrapper();
125
126
        expect(wrapper.find('sw-order-customer-grid-stub')
127
            .attributes('style')).toBeUndefined();
128
129
        expect(wrapper.find('sw-order-line-items-grid-sales-channel-stub')
130
            .attributes('style')).toEqual('display: none;');
131
132
        expect(wrapper.find('sw-order-create-options-stub')
133
            .exists()).toBeFalsy();
134
135
        await wrapper.find('.sw-tabs').setData({
136
            active: 'products'
137
        });
138
139
        expect(wrapper.find('sw-order-customer-grid-stub')
140
            .attributes('style')).toEqual('display: none;');
141
142
        expect(wrapper.find('sw-order-line-items-grid-sales-channel-stub')
143
            .attributes('style')).toBeFalsy();
144
145
        expect(wrapper.find('sw-order-create-options-stub')
146
            .exists()).toBeFalsy();
147
148
        await wrapper.find('.sw-tabs').setData({
149
            active: 'options'
150
        });
151
152
        expect(wrapper.find('sw-order-customer-grid-stub')
153
            .attributes('style')).toEqual('display: none;');
154
155
        expect(wrapper.find('sw-order-line-items-grid-sales-channel-stub')
156
            .attributes('style')).toEqual('display: none;');
157
158
        expect(wrapper.find('sw-order-create-options-stub')
159
            .exists()).toBeTruthy();
160
    });
161
162
    it('should emit modal-close when click cancel button', async () => {
163
        const wrapper = await createWrapper();
164
        const buttonCancel = wrapper.find('.sw-order-create-initial-modal__button-cancel');
165
166
        await buttonCancel.trigger('click');
167
        expect(wrapper.emitted('modal-close')).toBeTruthy();
168
    });
169
170
    it('should cancel cart when click cancel button', async () => {
171
        Shopware.State.commit('swOrder/setCartToken', 'token');
172
173
        const wrapper = await createWrapper();
174
        const spyCancelCart = jest.spyOn(wrapper.vm, 'cancelCart');
175
176
        const buttonCancel = wrapper.find('.sw-order-create-initial-modal__button-cancel');
177
        await buttonCancel.trigger('click');
178
179
        expect(spyCancelCart).toHaveBeenCalled();
180
    });
181
182
    it('should be able to save line item', async () => {
183
        const wrapper = await createWrapper();
184
185
        const productGrid = wrapper.find('sw-order-line-items-grid-sales-channel-stub');
186
        productGrid.vm.$emit('on-save-item', lineItem);
187
188
        await flushPromises();
189
190
        expect(wrapper.vm.cart.lineItems).toEqual([lineItem]);
191
    });
192
193
    it('should be able to remove line item', async () => {
194
        const wrapper = await createWrapper();
195
196
        const productGrid = wrapper.find('sw-order-line-items-grid-sales-channel-stub');
197
        productGrid.vm.$emit('on-remove-items', ['product1']);
198
199
        await flushPromises();
200
201
        expect(wrapper.vm.cart.lineItems).toEqual([]);
202
    });
203
204
    it('should able to get disable auto promotion value when it is toggled', async () => {
205
        const wrapper = await createWrapper();
206
207
        await wrapper.find('.sw-tabs').setData({
208
            active: 'options'
209
        });
210
211
        expect(wrapper.vm.disabledAutoPromotion).toBeFalsy();
212
213
        const optionsView = wrapper.find('sw-order-create-options-stub');
214
        optionsView.vm.$emit('auto-promotion-toggle', true);
215
216
        expect(wrapper.vm.disabledAutoPromotion).toBeTruthy();
217
    });
218
219
    it('should able to get promotion codes change', async () => {
220
        const wrapper = await createWrapper();
221
222
        await wrapper.find('.sw-tabs').setData({
223
            active: 'options'
224
        });
225
226
        expect(wrapper.vm.promotionCodes).toEqual([]);
227
228
        const optionsView = wrapper.find('sw-order-create-options-stub');
229
        optionsView.vm.$emit('promotions-change', ['DISCOUNT', 'XMAS']);
230
231
        expect(wrapper.vm.promotionCodes).toEqual(['DISCOUNT', 'XMAS']);
232
    });
233
234
    it('should able to get shipping cost change', async () => {
235
        const wrapper = await createWrapper();
236
237
        await wrapper.find('.sw-tabs').setData({
238
            active: 'options'
239
        });
240
241
        expect(wrapper.vm.shippingCosts).toBeNull();
242
243
        const optionsView = wrapper.find('sw-order-create-options-stub');
244
        optionsView.vm.$emit('shipping-cost-change', 100);
245
246
        expect(wrapper.vm.shippingCosts).toEqual(100);
247
    });
248
249
    it('should able to preview order', async () => {
250
        Shopware.State.commit('swOrder/setCart', {
251
            token: 'token',
252
            lineItems: [],
253
            deliveries: [{
254
                shippingCosts: {
255
                    totalPrice: 50
256
                }
257
            }],
258
        });
259
260
        const wrapper = await createWrapper();
261
262
        await wrapper.find('.sw-tabs').setData({
263
            active: 'options'
264
        });
265
266
        const optionsView = wrapper.find('sw-order-create-options-stub');
267
        optionsView.vm.$emit('auto-promotion-toggle', true);
268
        optionsView.vm.$emit('promotions-change', ['DISCOUNT']);
269
        optionsView.vm.$emit('shipping-cost-change', 100);
270
271
        const buttonPreview = wrapper.find('.sw-order-create-initial-modal__button-preview');
272
        await buttonPreview.trigger('click');
273
274
        await flushPromises();
275
276
        expect(wrapper.emitted('order-preview')).toBeTruthy();
277
    });
278
279
    it('should update context when salesChannelContext change', async () => {
280
        const wrapper = await createWrapper();
281
282
        expect(wrapper.vm.context).toEqual({
283
            currencyId: '',
284
            paymentMethodId: '',
285
            shippingMethodId: '',
286
            languageId: '',
287
            billingAddressId: '',
288
            shippingAddressId: '',
289
        });
290
291
        Shopware.State.commit('swOrder/setContext', {
292
            context: {
293
                currencyId: 'euro',
294
                languageIdChain: [
295
                    'english'
296
                ],
297
            },
298
            shippingMethod: {
299
                id: 'standard'
300
            },
301
            paymentMethod: {
302
                id: 'cash'
303
            },
304
            customer: {
305
                activeBillingAddress: {
306
                    id: '1234'
307
                },
308
                activeShippingAddress: {
309
                    id: '5678'
310
                }
311
            },
312
        });
313
314
        await wrapper.vm.$nextTick();
315
316
        expect(wrapper.vm.context).toEqual({
317
            currencyId: 'euro',
318
            paymentMethodId: 'cash',
319
            shippingMethodId: 'standard',
320
            languageId: 'english',
321
            billingAddressId: '1234',
322
            shippingAddressId: '5678',
323
        });
324
    });
325
});
326