1
|
|
|
/** |
2
|
|
|
* @jest-environment jsdom |
3
|
|
|
*/ |
4
|
|
|
|
5
|
|
|
import VariantSwitchPlugin from 'src/plugin/variant-switch/variant-switch.plugin'; |
6
|
|
|
import NativeEventEmitter from '../../../src/helper/emitter.helper'; |
7
|
|
|
|
8
|
|
|
describe('VariantSwitchPlugin tests', () => { |
9
|
|
|
let variantSwitchPlugin = undefined; |
|
|
|
|
10
|
|
|
let spyInit = jest.fn(); |
|
|
|
|
11
|
|
|
let spyInitializePlugins = jest.fn(); |
12
|
|
|
|
13
|
|
|
beforeEach(() => { |
14
|
|
|
window.csrf = { |
15
|
|
|
enabled: false |
16
|
|
|
}; |
17
|
|
|
|
18
|
|
|
window.router = []; |
19
|
|
|
|
20
|
|
|
window.PluginManager = { |
21
|
|
|
getPluginInstancesFromElement: () => { |
22
|
|
|
return new Map(); |
23
|
|
|
}, |
24
|
|
|
getPlugin: () => { |
25
|
|
|
return { |
26
|
|
|
get: () => [] |
27
|
|
|
}; |
28
|
|
|
}, |
29
|
|
|
initializePlugins: undefined |
30
|
|
|
}; |
31
|
|
|
|
32
|
|
|
document.$emitter = new NativeEventEmitter(); |
33
|
|
|
|
34
|
|
|
// mock variant switch plugins |
35
|
|
|
const mockElement = document.createElement('form'); |
36
|
|
|
const mockInput = document.createElement('input'); |
37
|
|
|
|
38
|
|
|
mockInput.classList.add('product-detail-configurator-option-input'); |
39
|
|
|
mockInput.setAttribute('type', 'radio'); |
40
|
|
|
mockInput.setAttribute('name', 'color'); |
41
|
|
|
mockInput.setAttribute('value', '1'); |
42
|
|
|
|
43
|
|
|
mockElement.appendChild(mockInput); |
44
|
|
|
variantSwitchPlugin = new VariantSwitchPlugin(mockElement); |
45
|
|
|
|
46
|
|
|
// create spy elements |
47
|
|
|
variantSwitchPlugin.init = spyInit; |
48
|
|
|
window.PluginManager.initializePlugins = spyInitializePlugins; |
49
|
|
|
}); |
50
|
|
|
|
51
|
|
|
afterEach(() => { |
52
|
|
|
variantSwitchPlugin = undefined; |
53
|
|
|
spyInit.mockClear(); |
54
|
|
|
spyInitializePlugins.mockClear(); |
55
|
|
|
window.PluginManager.initializePlugins = undefined; |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
test('variant switch plugin exists', () => { |
59
|
|
|
expect(typeof variantSwitchPlugin).toBe('object'); |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
test('_onChange get called on click', () => { |
63
|
|
|
// Mock the function which should be called on click |
64
|
|
|
variantSwitchPlugin._onChange = jest.fn(); |
|
|
|
|
65
|
|
|
const spy = jest.spyOn(variantSwitchPlugin, '_onChange'); |
66
|
|
|
|
67
|
|
|
// simulate click |
68
|
|
|
const mockInput = variantSwitchPlugin.el.firstChild; |
69
|
|
|
mockInput.click(); |
70
|
|
|
|
71
|
|
|
expect(spy).toHaveBeenCalled(); |
72
|
|
|
|
73
|
|
|
// Reset mock |
74
|
|
|
variantSwitchPlugin._onChange.mockRestore(); |
75
|
|
|
}); |
76
|
|
|
|
77
|
|
|
test('_redirectVariant should get called', () => { |
78
|
|
|
// Mock the function which should be called on click |
79
|
|
|
variantSwitchPlugin._redirectToVariant = jest.fn(); |
|
|
|
|
80
|
|
|
const spy = jest.spyOn(variantSwitchPlugin, '_redirectToVariant'); |
81
|
|
|
|
82
|
|
|
// simulate click |
83
|
|
|
const mockInput = variantSwitchPlugin.el.firstChild; |
84
|
|
|
mockInput.click(); |
85
|
|
|
|
86
|
|
|
expect(spy).toHaveBeenCalled(); |
87
|
|
|
|
88
|
|
|
// Reset mock |
89
|
|
|
variantSwitchPlugin._redirectToVariant.mockRestore(); |
90
|
|
|
}); |
91
|
|
|
|
92
|
|
|
test('_redirectVariant should not get called if cms elementId exists', () => { |
93
|
|
|
variantSwitchPlugin._elementId = '1'; |
94
|
|
|
|
95
|
|
|
// Mock the function which should be called on click |
96
|
|
|
variantSwitchPlugin._redirectToVariant = jest.fn(); |
|
|
|
|
97
|
|
|
const spy = jest.spyOn(variantSwitchPlugin, '_redirectToVariant'); |
98
|
|
|
|
99
|
|
|
// simulate click |
100
|
|
|
const mockInput = variantSwitchPlugin.el.firstChild; |
101
|
|
|
mockInput.click(); |
102
|
|
|
|
103
|
|
|
expect(spy).not.toHaveBeenCalled(); |
104
|
|
|
|
105
|
|
|
// Reset mock |
106
|
|
|
variantSwitchPlugin._redirectToVariant.mockRestore(); |
107
|
|
|
}); |
108
|
|
|
|
109
|
|
|
test('_redirectVariant should not get called if cms elementId exists and page type is not product detail', () => { |
110
|
|
|
variantSwitchPlugin._elementId = '1'; |
111
|
|
|
variantSwitchPlugin._pageType = 'landingpage'; |
112
|
|
|
|
113
|
|
|
// Mock the function which should be called on click |
114
|
|
|
variantSwitchPlugin._redirectToVariant = jest.fn(); |
|
|
|
|
115
|
|
|
const spy = jest.spyOn(variantSwitchPlugin, '_redirectToVariant'); |
116
|
|
|
|
117
|
|
|
// simulate click |
118
|
|
|
const mockInput = variantSwitchPlugin.el.firstChild; |
119
|
|
|
mockInput.click(); |
120
|
|
|
|
121
|
|
|
expect(spy).not.toHaveBeenCalled(); |
122
|
|
|
|
123
|
|
|
// Reset mock |
124
|
|
|
variantSwitchPlugin._redirectToVariant.mockRestore(); |
125
|
|
|
}); |
126
|
|
|
|
127
|
|
|
test('_redirectVariant should get called if cms elementId exists and page type is product detail', () => { |
128
|
|
|
variantSwitchPlugin._elementId = '1'; |
129
|
|
|
variantSwitchPlugin._pageType = 'product_detail'; |
130
|
|
|
|
131
|
|
|
// Mock the function which should be called on click |
132
|
|
|
variantSwitchPlugin._redirectToVariant = jest.fn(); |
|
|
|
|
133
|
|
|
const spy = jest.spyOn(variantSwitchPlugin, '_redirectToVariant'); |
134
|
|
|
|
135
|
|
|
// simulate click |
136
|
|
|
const mockInput = variantSwitchPlugin.el.firstChild; |
137
|
|
|
mockInput.click(); |
138
|
|
|
|
139
|
|
|
expect(spy).toHaveBeenCalled(); |
140
|
|
|
|
141
|
|
|
// Reset mock |
142
|
|
|
variantSwitchPlugin._redirectToVariant.mockRestore(); |
143
|
|
|
}); |
144
|
|
|
|
145
|
|
|
test('Ensure the updateBuyWidget event is fired with correct params', () => { |
146
|
|
|
function cb(event) { |
|
|
|
|
147
|
|
|
expect(event.detail.elementId).toEqual('1'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
document.$emitter.subscribe('updateBuyWidget', cb); |
151
|
|
|
|
152
|
|
|
variantSwitchPlugin._elementId = '1'; |
153
|
|
|
|
154
|
|
|
// simulate click |
155
|
|
|
const mockInput = variantSwitchPlugin.el.firstChild; |
156
|
|
|
mockInput.click(); |
157
|
|
|
}); |
158
|
|
|
}); |
159
|
|
|
|