1
|
|
View Code Duplication |
import CmsElementFavorites from 'src/module/sw-cms/service/cms-element-favorites.service'; |
|
|
|
|
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 = { |
|
|
|
|
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(); |
|
|
|
|
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(); |
|
|
|
|
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, |
|
|
|
|
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
|
|
|
|