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

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

Complexity

Total Complexity 11
Complexity/F 1

Size

Lines of Code 124
Function Count 11

Duplication

Duplicated Lines 124
Ratio 100 %

Importance

Changes 0
Metric Value
wmc 11
eloc 73
mnd 0
bc 0
fnc 11
dl 124
loc 124
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 CmsBlockFavorites from 'src/module/sw-cms/service/cms-block-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
            data: [{
12
                id: '8badf7ebe678ab968fe88c269c214ea6',
13
                userId: '8fe88c269c214ea68badf7ebe678ab96',
14
                key: CmsBlockFavorites.USER_CONFIG_KEY,
15
                value: [],
16
            }],
17
        },
18
    },
19
});
20
21
responses.addResponse({
22
    method: 'Post',
23
    url: '/user-config',
24
    status: 200,
25
    response: {
26
        data: [],
27
    },
28
});
29
30
describe('module/sw-cms/service/cms-block-favorites.service.spec.js', () => {
31
    let service;
32
33
    beforeEach(() => {
34
        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...
35
            id: '8fe88c269c214ea68badf7ebe678ab96',
36
        };
37
38
        service = new CmsBlockFavorites();
39
    });
40
41
    it('getFavoriteBlockNames > should return favorites from internal state', () => {
42
        const expected = ['foo', 'bar'];
43
        service.state.favorites = expected;
44
45
        expect(service.getFavoriteBlockNames()).toEqual(expected);
46
    });
47
48
    it('isFavorite > checks if given string is included in favorites', () => {
49
        const expected = 'bar';
50
        service.state.favorites = ['foo', 'bar'];
51
52
        expect(service.isFavorite(expected)).toBeTruthy();
53
    });
54
55
    it('update > pushes new item to favorites and calls "saveUserConfig"', () => {
56
        const newItem = 'biz';
57
58
        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...
59
        service.state.favorites = ['foo', 'bar'];
60
61
        service.update(true, newItem);
62
63
        expect(service.isFavorite(newItem)).toBeTruthy();
64
        expect(service.saveUserConfig).toHaveBeenCalled();
65
    });
66
67
    it('update > removes existing item from favorites and calls "saveUserConfig"', () => {
68
        const removedItem = 'bar';
69
70
        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...
71
        service.state.favorites = ['foo', 'bar'];
72
73
        service.update(false, removedItem);
74
75
        expect(service.isFavorite(removedItem)).toBeFalsy();
76
        expect(service.saveUserConfig).toHaveBeenCalled();
77
    });
78
79
    it('update > does not add or remove items with a wrong state', () => {
80
        const existingItem = 'foo';
81
        const nonExistingItem = 'biz';
82
83
        service.state.favorites = ['foo', 'bar'];
84
85
        service.update(false, nonExistingItem);
86
        expect(service.isFavorite(nonExistingItem)).toBeFalsy();
87
88
        service.update(true, existingItem);
89
        expect(service.isFavorite(existingItem)).toBeTruthy();
90
    });
91
92
    it('createUserConfigEntity > entity has specific values', () => {
93
        const expectedValues = {
94
            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...
95
            key: CmsBlockFavorites.USER_CONFIG_KEY,
96
            value: [],
97
        };
98
99
        const entity = service.createUserConfigEntity(CmsBlockFavorites.USER_CONFIG_KEY);
100
101
        expect(entity).toMatchObject(expectedValues);
102
    });
103
104
    it('handleEmptyUserConfig > replaces the property "value" with an empty array', () => {
105
        const userConfigMock = {
106
            value: {},
107
        };
108
109
        service.handleEmptyUserConfig(userConfigMock);
110
111
        expect(Array.isArray(userConfigMock.value)).toBeTruthy();
112
    });
113
114
    it('getCriteria > returns a criteria including specific filters', () => {
115
        const criteria = service.getCriteria(CmsBlockFavorites.USER_CONFIG_KEY);
116
117
        expect(criteria.filters).toContainEqual({ type: 'equals', field: 'key', value: CmsBlockFavorites.USER_CONFIG_KEY });
118
        expect(criteria.filters).toContainEqual({ type: 'equals', field: 'userId', value: '8fe88c269c214ea68badf7ebe678ab96' });
119
    });
120
121
    it('getCurrentUserId > returns the userId of the current session user', () => {
122
        expect(service.getCurrentUserId()).toBe('8fe88c269c214ea68badf7ebe678ab96');
123
    });
124
});
125