Passed
Push — master ( 617544...6d91e7 )
by Christian
12:28 queued 11s
created

variant-switch.plugin.test.js ➔ cb   D

Complexity

Conditions 12

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 2
dl 0
loc 3
rs 4.8
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like variant-switch.plugin.test.js ➔ cb often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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;
0 ignored issues
show
Unused Code Comprehensibility introduced by
The assignment of undefined is not necessary as variantSwitchPlugin is implicitly marked as undefined by the declaration.
Loading history...
10
    let spyInit = 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...
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();
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...
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();
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...
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();
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...
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) {
0 ignored issues
show
Bug introduced by
The function cb is declared conditionally. This is not supported by all runtimes. Consider moving it to root scope or using var cb = function() { /* ... */ }; instead.
Loading history...
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