Passed
Push — trunk ( f6d7d2...6c6236 )
by Christian
12:05 queued 14s
created

src/Storefront/Resources/app/storefront/test/plugin/main-menu/offcanvas-menu.plugin.test.js   A

Complexity

Total Complexity 9
Complexity/F 1

Size

Lines of Code 116
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 32
mnd 0
bc 0
fnc 9
dl 0
loc 116
rs 10
bpm 0
cpm 1
noi 8
c 0
b 0
f 0
1
import OffCanvasMenuPlugin from 'src/plugin/main-menu/offcanvas-menu.plugin';
2
3
jest.mock('src/service/http-client.service', () => {
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...
4
    const offCanvasMenuSubCategory = `
5
        <div class="navigation-offcanvas-container js-navigation-offcanvas">
6
            <div class="navigation-offcanvas-overlay-content js-navigation-offcanvas-overlay-content">
7
                <ul class="list-unstyled navigation-offcanvas-list">
8
                    <li class="navigation-offcanvas-list-item">
9
                        <a href="#"
10
                           class="navigation-offcanvas-link nav-item nav-link js-navigation-offcanvas-link"
11
                           data-href="/widgets/menu/offcanvas?navigationId=0188fd3e4ffb7079959622b2785167eb">
12
                            Cars
13
                        </a>
14
                    </li>
15
                    <li class="navigation-offcanvas-list-item">
16
                        <a href="#"
17
                           class="navigation-offcanvas-link nav-item nav-link js-navigation-offcanvas-link"
18
                           data-href="/widgets/menu/offcanvas?navigationId=0188fd3e4ffb7079959622b2785167eb">
19
                            Smartphones
20
                        </a>
21
                    </li>
22
                </ul>
23
            </div>
24
        </div>
25
    `;
26
27
    return function () {
28
        return {
29
            get: (url, callback) => {
30
                return callback(offCanvasMenuSubCategory);
31
            },
32
        };
33
    };
34
});
35
36
describe('OffCanvasMenuPlugin tests', () => {
37
    let plugin;
38
39
    beforeEach(() => {
40
        document.body.innerHTML = `
41
            <button class="btn nav-main-toggle-btn header-actions-btn" type="button" data-offcanvas-menu="true">
42
                <span class="icon icon-stack"></span>
43
            </button>
44
45
            <div class="js-navigation-offcanvas-initial-content d-none">
46
                <div class="offcanvas-body">
47
                    <p>Initial content</p>
48
49
                    <div class="navigation-offcanvas-container js-navigation-offcanvas">
50
                        <div class="navigation-offcanvas-overlay-content js-navigation-offcanvas-overlay-content">
51
                            <ul class="list-unstyled navigation-offcanvas-list">
52
                                <li class="navigation-offcanvas-list-item">
53
                                    <a href="#"
54
                                       class="navigation-offcanvas-link nav-item nav-link js-navigation-offcanvas-link"
55
                                       data-href="/widgets/menu/offcanvas?navigationId=0188fd3e4ffb7079959622b2785167eb">
56
                                        Outdoors
57
                                    </a>
58
                                </li>
59
                                <li class="navigation-offcanvas-list-item">
60
                                    <a href="#"
61
                                       class="navigation-offcanvas-link nav-item nav-link js-navigation-offcanvas-link"
62
                                       data-href="/widgets/menu/offcanvas?navigationId=0188fd3e4ffb7079959622b2785167eb">
63
                                        Automotive
64
                                    </a>
65
                                </li>
66
                            </ul>
67
                        </div>
68
                    </div>
69
                </div>
70
            </div>
71
        `;
72
73
        const el = document.querySelector('[data-offcanvas-menu]');
74
75
        plugin = new OffCanvasMenuPlugin(el);
76
77
        jest.useFakeTimers();
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...
78
    });
79
80
    afterEach(() => {
81
        jest.useRealTimers();
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...
82
    });
83
84
    test('Creates plugin instance', () => {
85
        expect(typeof plugin).toBe('object');
86
    });
87
88
    test('Open OffCanvas menu on click with initial content from DOM', () => {
89
        // Open OffCanvas menu
90
        plugin.el.dispatchEvent(new Event('click'));
0 ignored issues
show
Bug introduced by
The variable Event seems to be never declared. If this is a global, consider adding a /** global: Event */ 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...
91
92
        jest.runAllTimers();
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...
93
94
        const categoryLinks = document.querySelectorAll('.navigation-offcanvas .navigation-offcanvas-link');
95
96
        // Ensure OffCanvas is opened with initial content
97
        expect(categoryLinks[0].textContent).toContain('Outdoors');
98
        expect(categoryLinks[1].textContent).toContain('Automotive');
99
    });
100
101
    test('Fetch and render next category after click on category link', () => {
102
        // Open OffCanvas menu
103
        plugin.el.dispatchEvent(new Event('click', { bubbles: true }));
0 ignored issues
show
Bug introduced by
The variable Event seems to be never declared. If this is a global, consider adding a /** global: Event */ 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...
104
105
        const link = document.querySelector('.js-navigation-offcanvas-link');
106
        plugin._getLinkEventHandler(new MouseEvent('click', { bubbles: true }), link);
0 ignored issues
show
Bug introduced by
The variable MouseEvent seems to be never declared. If this is a global, consider adding a /** global: MouseEvent */ 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...
107
108
        jest.runAllTimers();
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...
109
110
        const subCategoryLinks = document.querySelectorAll('.navigation-offcanvas-overlay.has-transition .navigation-offcanvas-link')
111
112
        // Ensure sub-categories are rendered
113
        expect(subCategoryLinks[0].textContent).toContain('Cars');
114
        expect(subCategoryLinks[1].textContent).toContain('Smartphones');
115
    });
116
});
117