Passed
Push — master ( 45b9f0...afcb5b )
by Christian
12:38 queued 11s
created

  F

Complexity

Conditions 14

Size

Total Lines 89
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 61
dl 0
loc 89
c 0
b 0
f 0
rs 3.6
cc 14

How to fix   Long Method    Complexity   

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:

Complexity

Complex classes like sw-users-permissions-user-create.spec.js ➔ createWrapper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import { shallowMount, createLocalVue } from '@vue/test-utils';
2
import 'src/module/sw-users-permissions/page/sw-users-permissions-user-detail';
3
import 'src/module/sw-users-permissions/page/sw-users-permissions-user-create';
4
5
function createWrapper(privileges = []) {
6
    const localVue = createLocalVue();
7
    localVue.directive('tooltip', {});
8
9
    return shallowMount(Shopware.Component.build('sw-users-permissions-user-create'), {
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...
10
        localVue,
11
        provide: {
12
            acl: {
13
                can: (identifier) => {
14
                    if (!identifier) { return true; }
15
16
                    return privileges.includes(identifier);
17
                }
18
            },
19
            loginService: {},
20
            userService: {
21
                getUser: () => Promise.resolve()
22
            },
23
            userValidationService: {},
24
            integrationService: {},
25
            repositoryFactory: {
26
                create: (entityName) => {
27
                    if (entityName === 'user') {
28
                        return {
29
                            search: () => Promise.resolve(),
30
                            get: () => {
31
                                return Promise.resolve(
32
                                    {
33
                                        localeId: '7dc07b43229843d387bb5f59233c2d66',
34
                                        username: 'admin',
35
                                        firstName: '',
36
                                        lastName: 'admin',
37
                                        email: '[email protected]'
38
                                    }
39
                                );
40
                            },
41
                            create: () => {
42
                                return {
43
                                    localeId: '',
44
                                    username: '',
45
                                    firstName: '',
46
                                    lastName: '',
47
                                    email: '',
48
                                    password: ''
49
                                };
50
                            }
51
                        };
52
                    }
53
54
                    if (entityName === 'language') {
55
                        return {
56
                            search: () => Promise.resolve(),
57
                            get: () => Promise.resolve()
58
                        };
59
                    }
60
61
                    return {};
62
                }
63
            },
64
            feature: {
65
                isActive: () => true
66
            }
67
68
        },
69
        mocks: {
70
            $tc: v => v,
71
            $route: {
72
                params: {
73
                    id: '1a2b3c4d'
74
                }
75
            }
76
        },
77
        stubs: {
78
            'sw-page': '<div><slot name="content"></slot></div>',
79
            'sw-card-view': true,
80
            'sw-card': true,
81
            'sw-text-field': true,
82
            'sw-upload-listener': true,
83
            'sw-media-upload-v2': true,
84
            'sw-password-field': {
85
                template: '<input type="password" :value="value" @input="$emit(\'input\', $event.target.value)">',
86
                props: ['value']
87
            },
88
            'sw-select-field': true,
89
            'sw-switch-field': true,
90
            'sw-entity-multi-select': true
91
        }
92
    });
93
}
94
// TODO: fix these tests and add test cases
95
describe('modules/sw-users-permissions/page/sw-users-permissions-user-create', () => {
96
    let wrapper;
97
98
    beforeEach(() => {
99
        Shopware.State.get('session').languageId = '123456789';
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...
100
        wrapper = createWrapper();
101
    });
102
103
    afterEach(() => {
104
        wrapper.destroy();
105
        Shopware.State.get('session').languageId = '';
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...
106
    });
107
108
    it('should be a Vue.js component', () => {
109
        expect(wrapper.isVueInstance()).toBeTruthy();
110
    });
111
112
    it('should create a new user', () => {
113
        expect(wrapper.vm.user).toStrictEqual({
114
            admin: true,
115
            localeId: '',
116
            username: '',
117
            firstName: '',
118
            lastName: '',
119
            email: '',
120
            password: ''
121
        });
122
    });
123
124
    it('should allow to set the password', () => {
125
        expect(wrapper.vm.user.password).toBe('');
126
127
        const fieldPassword = wrapper.find('.sw-settings-user-detail__grid-password');
128
        fieldPassword.setValue('Passw0rd!');
129
130
        expect(wrapper.vm.user.password).toBe('Passw0rd!');
131
    });
132
});
133