Passed
Push — trunk ( a269b8...ca43e7 )
by Christian
17:02 queued 13s
created

src/Administration/Resources/app/administration/src/module/sw-cms/service/cms-element-favorites.service.spec.vue3.js   A

Complexity

Total Complexity 12
Complexity/F 1

Size

Lines of Code 126
Function Count 12

Duplication

Duplicated Lines 126
Ratio 100 %

Importance

Changes 0
Metric Value
wmc 12
eloc 74
mnd 0
bc 0
fnc 12
dl 126
loc 126
rs 10
bpm 0
cpm 1
noi 4
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1 View Code Duplication
import CmsElementFavorites from 'src/module/sw-cms/service/cms-element-favorites.service';
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
3
const responses = global.repositoryFactoryMock.responses;
4
5
responses.addResponse({
6
    method: 'Post',
7
    url: '/search/user-config',
8
    status: 200,
9
    response: {
10
        data: [{
11
            id: '8badf7ebe678ab968fe88c269c214ea6',
12
            userId: '8fe88c269c214ea68badf7ebe678ab96',
13
            key: CmsElementFavorites.USER_CONFIG_KEY,
14
            value: [],
15
        }],
16
    },
17
});
18
19
responses.addResponse({
20
    method: 'Post',
21
    url: '/user-config',
22
    status: 200,
23
    response: {
24
        data: [],
25
    },
26
});
27
28
describe('module/sw-cms/service/cms-block-favorites.service.spec.js', () => {
29
    let service;
30
31
    beforeEach(() => {
32
        Shopware.State.get('session').currentUser = {
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...
33
            id: '8fe88c269c214ea68badf7ebe678ab96',
34
        };
35
36
        service = new CmsElementFavorites();
37
    });
38
39
    afterEach(() => {
40
        service = null;
41
    });
42
43
    it('getFavoriteElementNames > should return favorites from internal state', () => {
44
        const expected = ['foo', 'bar'];
45
        service.state.favorites = expected;
46
47
        expect(service.getFavoriteElementNames()).toEqual(expected);
48
    });
49
50
    it('isFavorite > checks if given string is included in favorites', () => {
51
        const expected = 'bar';
52
        service.state.favorites = ['foo', 'bar'];
53
54
        expect(service.isFavorite(expected)).toBeTruthy();
55
    });
56
57
    it('update > pushes new item to favorites and calls "saveUserConfig"', () => {
58
        const newItem = 'biz';
59
60
        service.saveUserConfig = jest.fn();
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ 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...
61
        service.state.favorites = ['foo', 'bar'];
62
63
        service.update(true, newItem);
64
65
        expect(service.isFavorite(newItem)).toBeTruthy();
66
        expect(service.saveUserConfig).toHaveBeenCalled();
67
    });
68
69
    it('update > removes existing item from favorites and calls "saveUserConfig"', () => {
70
        const removedItem = 'bar';
71
72
        service.saveUserConfig = jest.fn();
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ 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...
73
        service.state.favorites = ['foo', 'bar'];
74
75
        service.update(false, removedItem);
76
77
        expect(service.isFavorite(removedItem)).toBeFalsy();
78
        expect(service.saveUserConfig).toHaveBeenCalled();
79
    });
80
81
    it('update > does not add or remove items with a wrong state', () => {
82
        const existingItem = 'foo';
83
        const nonExistingItem = 'biz';
84
85
        service.state.favorites = ['foo', 'bar'];
86
87
        service.update(false, nonExistingItem);
88
        expect(service.isFavorite(nonExistingItem)).toBeFalsy();
89
90
        service.update(true, existingItem);
91
        expect(service.isFavorite(existingItem)).toBeTruthy();
92
    });
93
94
    it('createUserConfigEntity > entity has specific values', () => {
95
        const expectedValues = {
96
            userId: Shopware.State.get('session').currentUser.id,
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...
97
            key: CmsElementFavorites.USER_CONFIG_KEY,
98
            value: [],
99
        };
100
101
        const entity = service.createUserConfigEntity(CmsElementFavorites.USER_CONFIG_KEY);
102
103
        expect(entity).toMatchObject(expectedValues);
104
    });
105
106
    it('handleEmptyUserConfig > replaces the property "value" with an empty array', () => {
107
        const userConfigMock = {
108
            value: {},
109
        };
110
111
        service.handleEmptyUserConfig(userConfigMock);
112
113
        expect(Array.isArray(userConfigMock.value)).toBeTruthy();
114
    });
115
116
    it('getCriteria > returns a criteria including specific filters', () => {
117
        const criteria = service.getCriteria(CmsElementFavorites.USER_CONFIG_KEY);
118
119
        expect(criteria.filters).toContainEqual({ type: 'equals', field: 'key', value: CmsElementFavorites.USER_CONFIG_KEY });
120
        expect(criteria.filters).toContainEqual({ type: 'equals', field: 'userId', value: '8fe88c269c214ea68badf7ebe678ab96' });
121
    });
122
123
    it('getCurrentUserId > returns the userId of the current session user', () => {
124
        expect(service.getCurrentUserId()).toBe('8fe88c269c214ea68badf7ebe678ab96');
125
    });
126
});
127