Passed
Push — trunk ( 17531a...3f79bf )
by Christian
14:06 queued 13s
created

  B

Complexity

Conditions 2

Size

Total Lines 74
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

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