Conditions | 14 |
Total Lines | 89 |
Code Lines | 61 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
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'; |
||
5 | function createWrapper(privileges = []) { |
||
6 | const localVue = createLocalVue(); |
||
7 | localVue.directive('tooltip', {}); |
||
8 | |||
9 | return shallowMount(Shopware.Component.build('sw-users-permissions-user-create'), { |
||
|
|||
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 |
||
133 |
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.