Passed
Push — trunk ( 3009d4...5cf225 )
by Christian
15:03 queued 01:27
created

sw-dashboard-statistics.spec.ts ➔ createWrapper   B

Complexity

Conditions 5

Size

Total Lines 75
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 57
dl 0
loc 75
rs 7.9406
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, createLocalVue } from '@vue/test-utils';
2
import swDashboardStatistics from 'src/module/sw-dashboard/component/sw-dashboard-statistics';
3
import 'src/app/component/form/sw-select-field';
4
import 'src/app/component/form/field-base/sw-block-field';
5
import 'src/app/component/form/field-base/sw-base-field';
6
import 'src/app/component/base/sw-chart-card';
7
import 'src/app/component/base/sw-card';
8
import dictionary from 'src/module/sw-dashboard/snippet/en-GB.json';
9
import { currency } from 'src/core/service/utils/format.utils';
10
11
Shopware.Component.register('sw-dashboard-statistics', swDashboardStatistics);
12
13
async function createWrapper(privileges = [], orderSumToday = null) {
14
    const localVue = createLocalVue();
15
    localVue.filter('asset', v => v);
16
    localVue.filter('date', v => v);
17
    localVue.filter('currency', currency);
18
19
    const responseMock = {
20
        aggregations: {
21
            order_count_bucket: {
22
                buckets: []
23
            },
24
            order_sum_bucket: {
25
                buckets: []
26
            }
27
        }
28
    };
29
30
    const options = {
31
        localVue,
32
        stubs: {
33
            'sw-card': await Shopware.Component.build('sw-card'),
34
            'sw-chart-card': await Shopware.Component.build('sw-chart-card'),
35
            'sw-entity-listing': true,
36
            'sw-chart': true,
37
            'sw-select-field': await Shopware.Component.build('sw-select-field'),
38
            'sw-block-field': await Shopware.Component.build('sw-block-field'),
39
            'sw-base-field': await Shopware.Component.build('sw-base-field'),
40
            'sw-skeleton': true,
41
            'sw-help-text': true,
42
            'sw-ignore-class': true,
43
            'sw-extension-component-section': true,
44
            'sw-icon': true,
45
            'sw-field-error': true,
46
        },
47
        mocks: {
48
            $tc: (...args) => JSON.stringify([...args]),
49
            $i18n: {
50
                locale: 'en-GB',
51
                messages: {
52
                    'en-GB': dictionary
53
                }
54
            }
55
        },
56
        provide: {
57
            repositoryFactory: {
58
                create: () => ({
59
                    search: () => Promise.resolve(responseMock)
60
                })
61
            },
62
            stateStyleDataProviderService: {},
63
            acl: {
64
                can: (identifier) => {
65
                    if (!identifier) { return true; }
66
67
                    return privileges.includes(identifier);
68
                }
69
            }
70
        },
71
        computed: {
72
            systemCurrencyISOCode() {
73
                return 'EUR';
74
            },
75
            isSessionLoaded() {
76
                return true;
77
            }
78
        }
79
    };
80
81
    if (orderSumToday !== null) {
82
        options.computed.hasOrderToday = () => true;
83
        options.computed.orderSumToday = () => orderSumToday;
84
    }
85
86
    return shallowMount(await Shopware.Component.build('sw-dashboard-statistics'), options);
87
}
88
89
/**
90
 * @package merchant-services
91
 */
92
describe('module/sw-dashboard/component/sw-dashboard-statistics', () => {
93
    let wrapper = null;
94
95
    beforeAll(() => {
96
        Shopware.State.registerModule('session', {
97
            state: {
98
                currentUser: null
99
            },
100
            mutations: {
101
                setCurrentUser(state, user) {
102
                    state.currentUser = user;
103
                }
104
            }
105
        });
106
        jest.useFakeTimers('modern');
107
    });
108
109
    afterEach(() => {
110
        wrapper.destroy();
111
    });
112
113
    afterAll(() => {
114
        jest.useRealTimers();
115
    });
116
117
    it('should be a Vue.js component', async () => {
118
        wrapper = await createWrapper();
119
120
        expect(wrapper.vm).toBeTruthy();
121
    });
122
123
    it('should not show the stats', async () => {
124
        wrapper = await createWrapper();
125
126
        const orderToday = wrapper.find('.sw-dashboard-statistics__intro-stats-today');
127
        const statisticsCount = wrapper.find('.sw-dashboard-statistics__statistics-count');
128
        const statisticsSum = wrapper.find('.sw-dashboard-statistics__statistics-sum');
129
130
        expect(orderToday.exists()).toBeFalsy();
131
        expect(statisticsCount.exists()).toBeFalsy();
132
        expect(statisticsSum.exists()).toBeFalsy();
133
    });
134
135
    it('should show the stats', async () => {
136
        wrapper = await createWrapper(['order.viewer']);
137
        await flushPromises();
138
139
        const orderToday = wrapper.find('.sw-dashboard-statistics__intro-stats-today');
140
        const statisticsCount = wrapper.find('.sw-dashboard-statistics__statistics-count');
141
        const statisticsSum = wrapper.find('.sw-dashboard-statistics__statistics-sum');
142
143
        expect(orderToday.exists()).toBeFalsy();
144
        expect(statisticsCount.exists()).toBeTruthy();
145
        expect(statisticsSum.exists()).toBeTruthy();
146
    });
147
148
    it('should not exceed decimal places of two', async () => {
149
        wrapper = await createWrapper(['order.viewer'], 43383.13234554);
150
        await flushPromises();
151
152
        const todaysTotalSum = wrapper.find('.sw-dashboard-statistics__intro-stats-today-single-stat:nth-of-type(2) span:nth-of-type(2)').text();
153
        expect(todaysTotalSum).toBe('€43,383.13');
154
    });
155
156
    it('should allow the possibility to extend the date ranges', async () => {
157
        Shopware.Component.override('sw-dashboard-statistics', {
158
            computed: {
159
                rangesValueMap(): Array<HistoryDateRange> {
160
                    return [
161
                        ...this.$super('rangesValueMap'),
162
                        {
163
                            label: '72Hours',
164
                            range: 72,
165
                            interval: 'hour',
166
                        },
167
                        {
168
                            label: '90Days',
169
                            range: 90,
170
                            interval: 'day',
171
                        },
172
                    ];
173
                }
174
            }
175
        });
176
177
        wrapper = await createWrapper(['order.viewer']);
178
        await flushPromises();
179
180
        const dateRanges = wrapper.get('#sw-field--selectedRange').findAll('option');
181
182
        expect(dateRanges.at(dateRanges.length - 2).text()).toBe('["sw-dashboard.monthStats.dateRanges.72Hours"]');
183
        expect(dateRanges.at(dateRanges.length - 1).text()).toBe('["sw-dashboard.monthStats.dateRanges.90Days"]');
184
    });
185
});
186