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