Passed
Push — trunk ( 9b0a66...54d57b )
by Christian
27:18 queued 11:16
created

  A

Complexity

Conditions 2

Size

Total Lines 67
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 67
rs 9.0399
c 0
b 0
f 0
cc 2

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 swFlowListFlowTemplates from 'src/module/sw-flow/view/listing/sw-flow-list-flow-templates';
3
4
const { Context } = Shopware;
5
const { EntityCollection } = Shopware.Data;
6
7
Shopware.Component.register('sw-flow-list-flow-templates', swFlowListFlowTemplates);
8
9
const mockData = [
10
    {
11
        id: '44de136acf314e7184401d36406c1e90',
12
        name: 'test flow template',
13
        config: {
14
            eventName: 'checkout.order.placed'
15
        }
16
    }
17
];
18
19
async function createWrapper(privileges = []) {
20
    return shallowMount(await Shopware.Component.build('sw-flow-list-flow-templates'), {
21
        mocks: {
22
            $route: {
23
                query: {
24
                    page: 1,
25
                    limit: 25
26
                }
27
            }
28
        },
29
30
        provide: {
31
            repositoryFactory: {
32
                create: () => ({
33
                    search: () => {
34
                        return Promise.resolve(new EntityCollection('', '', Context.api, null, mockData, 1));
35
                    }
36
                })
37
            },
38
39
            acl: {
40
                can: (identifier) => {
41
                    if (!identifier) {
42
                        return true;
43
                    }
44
45
                    return privileges.includes(identifier);
46
                }
47
            },
48
49
            searchRankingService: {}
50
        },
51
52
        stubs: {
53
            'sw-page': {
54
                template: `
55
                    <div class="sw-page">
56
                        <slot name="search-bar"></slot>
57
                        <slot name="smart-bar-back"></slot>
58
                        <slot name="smart-bar-header"></slot>
59
                        <slot name="language-switch"></slot>
60
                        <slot name="smart-bar-actions"></slot>
61
                        <slot name="side-content"></slot>
62
                        <slot name="content"></slot>
63
                        <slot name="sidebar"></slot>
64
                        <slot></slot>
65
                    </div>
66
                `
67
            },
68
            'sw-icon': true,
69
            'sw-button': true,
70
            'sw-entity-listing': {
71
                props: ['items'],
72
                template: `
73
                    <div class="sw-data-grid">
74
                    <div class="sw-data-grid__row" v-for="item in items">
75
                        <slot name="column-name" v-bind="{ item }"></slot>
76
                        <slot name="column-createFlow" v-bind="{ item }"></slot>
77
                        <slot name="actions" v-bind="{ item }"></slot>
78
                    </div>
79
                    </div>
80
                `
81
            },
82
            'sw-context-menu-item': true,
83
            'sw-empty-state': true,
84
            'sw-search-bar': true
85
        }
86
    });
87
}
88
89
describe('module/sw-flow/view/listing/sw-flow-list-flow-templates', () => {
90
    it('should be able to create a flow from template', async () => {
91
        const wrapper = await createWrapper([
92
            'flow.creator'
93
        ]);
94
        await flushPromises();
95
96
        const createFlowLink = wrapper.find('.sw-flow-list-my-flows__content__create-flow-link');
97
        expect(createFlowLink.exists()).toBe(true);
98
99
        expect(createFlowLink.attributes().disabled).toBe(undefined);
100
    });
101
102
    it('should not be able to create a flow from template', async () => {
103
        const wrapper = await createWrapper([
104
            'flow.viewer'
105
        ]);
106
        await flushPromises();
107
108
        const createFlowLink = wrapper.find('.sw-flow-list-my-flows__content__create-flow-link');
109
        expect(createFlowLink.exists()).toBe(true);
110
111
        expect(createFlowLink.attributes().disabled).toBe('disabled');
112
    });
113
114
    it('should be able to redirect to create flow page from flow template', async () => {
115
        const wrapper = await createWrapper([
116
            'flow.creator'
117
        ]);
118
        await flushPromises();
119
        await wrapper.find('.sw-flow-list-my-flows__content__create-flow-link').trigger('click');
120
121
        const routerPush = wrapper.vm.$router.push;
122
123
        expect(routerPush).toHaveBeenLastCalledWith({
124
            name: 'sw.flow.create',
125
            params: { flowTemplateId: '44de136acf314e7184401d36406c1e90' }
126
        });
127
    });
128
129
    it('should be able to view detail flow template', async () => {
130
        const wrapper = await createWrapper([
131
            'flow.creator'
132
        ]);
133
134
        await flushPromises();
135
        await wrapper.find('.sw-flow-list-my-flows__content__update-flow-template-link').trigger('click');
136
137
        const routerPush = wrapper.vm.$router.push;
138
139
        expect(routerPush).toHaveBeenLastCalledWith({
140
            name: 'sw.flow.detail',
141
            params: { id: '44de136acf314e7184401d36406c1e90' },
142
            query: {
143
                type: 'template'
144
            }
145
        });
146
147
        wrapper.vm.$router.push = jest.fn();
148
        wrapper.vm.onEditFlow({});
149
        await flushPromises();
150
151
        expect(wrapper.vm.$router.push).toBeCalledTimes(0);
152
    });
153
});
154