|
1
|
|
|
import OffCanvasMenuPlugin from 'src/plugin/main-menu/offcanvas-menu.plugin'; |
|
2
|
|
|
|
|
3
|
|
|
jest.mock('src/service/http-client.service', () => { |
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
78
|
|
|
}); |
|
79
|
|
|
|
|
80
|
|
|
afterEach(() => { |
|
81
|
|
|
jest.useRealTimers(); |
|
|
|
|
|
|
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')); |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
jest.runAllTimers(); |
|
|
|
|
|
|
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 })); |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
const link = document.querySelector('.js-navigation-offcanvas-link'); |
|
106
|
|
|
plugin._getLinkEventHandler(new MouseEvent('click', { bubbles: true }), link); |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
jest.runAllTimers(); |
|
|
|
|
|
|
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
|
|
|
|
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.