|
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('Ensure the updateBuyWidget event is fired with correct params', () => { |
|
110
|
|
|
function cb(event) { |
|
|
|
|
|
|
111
|
|
|
expect(event.detail.elementId).toEqual('1'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
document.$emitter.subscribe('updateBuyWidget', cb); |
|
115
|
|
|
|
|
116
|
|
|
variantSwitchPlugin._elementId = '1'; |
|
117
|
|
|
|
|
118
|
|
|
// simulate click |
|
119
|
|
|
const mockInput = variantSwitchPlugin.el.firstChild; |
|
120
|
|
|
mockInput.click(); |
|
121
|
|
|
}); |
|
122
|
|
|
}); |
|
123
|
|
|
|