forked from benmag1/mdl-ext
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlightbox.spec.js
More file actions
282 lines (238 loc) · 8.6 KB
/
lightbox.spec.js
File metadata and controls
282 lines (238 loc) · 8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
'use strict';
import requireUncached from 'require-uncached';
import jsdomify from 'jsdomify';
import {patchJsDom} from '../testutils/patch-jsdom';
import { expect, assert } from 'chai';
import sinon from 'sinon';
import { shouldBehaveLikeAMdlComponent } from '../testutils/shared-component-behaviours';
import { spyOnKeyboardEvent } from '../testutils/spy-on-keyboard-event';
import {
VK_ESC,
VK_SPACE,
VK_END,
VK_HOME,
VK_ARROW_LEFT,
VK_ARROW_UP,
VK_ARROW_RIGHT,
VK_ARROW_DOWN
} from '../../src/utils/constants';
describe('MaterialExtLightbox', () => {
const fixture = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Accordion Fixture</title>
</head>
<body>
<div id='mount'>
<p>Some text filler</p>
</div>
<dialog class="mdlext-dialog">
<div id="lightbox" class="mdlext-lightbox mdlext-js-lightbox mdl-card">
<div class="mdl-card__menu">
<button data-action="close" class="mdl-button mdl-button--icon mdl-js-button mdl-js-ripple-effect">
<i class="material-icons">close</i>
</button>
</div>
<figure class="mdl-card__media">
<img src="" alt>
<figcaption></figcaption>
</figure>
<footer class="mdl-card__actions mdl-card--border">
<div class="mdl-card__supporting-text">
<span>Image title</span>
</div>
<nav>
<button data-action="first" class="mdl-button mdl-button--icon mdl-js-button mdl-js-ripple-effect" title="First">
<i class="material-icons">first_page</i>
</button>
<button data-action="prev" class="mdl-button mdl-button--icon mdl-js-button mdl-js-ripple-effect" title="Previous">
<i class="material-icons">chevron_left</i>
</button>
<button data-action="next" class="mdl-button mdl-button--icon mdl-js-button mdl-js-ripple-effect" title="Next">
<i class="material-icons">chevron_right</i>
</button>
<button data-action="last" class="mdl-button mdl-button--icon mdl-js-button mdl-js-ripple-effect" title="Last">
<i class="material-icons">last_page</i>
</button>
<button data-action="play" class="mdl-button mdl-button--icon mdl-js-button mdl-js-ripple-effect" title="Play">
<i class="material-icons">play_circle_outline</i>
</button>
</nav>
</footer>
</div>
</dialog>
<div id="mount-2">
</div>
</body>
</html>`;
const lightboxFragment = `
<div id="lightbox-2" class="mdlext-lightbox mdlext-js-lightbox mdl-card">
</div>`;
before ( () => {
patchJsDom(fixture);
// Must load MDL after jsdom, see: https://github.com/mochajs/mocha/issues/1722
requireUncached( 'material-design-lite/material');
global.componentHandler = window.componentHandler;
assert.isObject(componentHandler, 'No global MDL component handler');
requireUncached('../../src/lightbox/lightbox');
assert.isNotNull(window.MaterialExtLightbox, 'Expected MaterialExtAccordion not to be null');
global.MaterialExtLightbox = window.MaterialExtLightbox;
// Simulate open dialog
const dialog = document.querySelector('dialog');
dialog.setAttribute('open', '');
});
after ( () => {
jsdomify.destroy();
});
shouldBehaveLikeAMdlComponent({
componentName: 'MaterialExtLightbox',
componentCssClass: 'mdlext-js-lightbox',
newComponenrMountNodeSelector: '#mount-2',
newComponentHtml: lightboxFragment
});
it('has tabindex', () => {
const element = document.querySelector('#lightbox');
expect(element.getAttribute('tabindex')).not.to.be.NaN;
});
it('has "data-action" attributes', () => {
const elements = document.querySelectorAll('#lightbox [data-action]');
expect(elements).to.have.length.of.at.least(1);
});
it('can load image', () => {
const lightbox = document.querySelector('#lightbox');
const img = document.querySelector('img', lightbox);
const spy = sinon.spy();
img.addEventListener('load', spy);
img.src = './smiley.jpg';
assert.isTrue(spy.called, 'Expected "action" event to fire');
});
it('can not click an image', () => {
const lightbox = document.querySelector('#lightbox');
const img = lightbox.querySelector('img');
const spy = sinon.spy();
img.addEventListener('click', spy);
const event = new MouseEvent('click', {
'view': window,
'bubbles': true,
'cancelable': true
});
try {
img.dispatchEvent(event);
}
finally {
img.removeEventListener('click', spy);
}
assert.isTrue(spy.called, 'Expected "click" event to fire when image is clicked');
assert.isTrue(event.defaultPrevented, 'Expected "event.preventDefault" to be called when image is clicked');
});
it('can drag an image', () => {
const lightbox = document.querySelector('#lightbox');
const img = lightbox.querySelector('img');
img.src = './smiley.jpg';
const mouseDownSpy = sinon.spy();
img.addEventListener('mousedown', mouseDownSpy, true);
const mouseMoveSpy = sinon.spy();
window.addEventListener('mousemove', mouseMoveSpy, true);
const mouseUpSpy = sinon.spy();
window.addEventListener('mouseup', mouseUpSpy, true);
try {
let event = new MouseEvent('mousedown', {
'view': window,
'bubbles': true,
'cancelable': true,
'clientX': 10, // clientX/clientY is readonly...
'clientY': 0 // ... not shure if I can test mouse movement
});
img.dispatchEvent(event);
event = new MouseEvent('mousemove', {
'view': window,
'bubbles': true,
'cancelable': true,
'clientX': 20,
'clientY': 0
});
window.dispatchEvent(event);
event = new MouseEvent('mouseup', {
'view': window,
'bubbles': true,
'cancelable': true,
'clientX': 40,
'clientY': 0
});
window.dispatchEvent(event);
}
finally {
img.removeEventListener('mousedown', mouseDownSpy);
window.removeEventListener('mousemove', mouseMoveSpy);
window.removeEventListener('mouseup', mouseUpSpy);
}
assert.isTrue(mouseDownSpy.called, 'Expected "mousedown" event to fire');
assert.isTrue(mouseMoveSpy.called, 'Expected "mousemove" event to fire');
assert.isTrue(mouseUpSpy.called, 'Expected "mouseup" event to fire');
});
it('interacts with the keyboard', () => {
const lightbox = document.querySelector('#lightbox');
spyOnKeyboardEvent(lightbox, VK_ARROW_DOWN);
spyOnKeyboardEvent(lightbox, VK_ARROW_UP);
spyOnKeyboardEvent(lightbox, VK_ARROW_LEFT);
spyOnKeyboardEvent(lightbox, VK_ARROW_RIGHT);
spyOnKeyboardEvent(lightbox, VK_END);
spyOnKeyboardEvent(lightbox, VK_HOME);
spyOnKeyboardEvent(lightbox, VK_ESC);
spyOnKeyboardEvent(lightbox, VK_SPACE);
});
it('listens to resize', () => {
const lightbox = document.querySelector('#lightbox');
const spy = sinon.spy();
window.addEventListener('resize', spy, true);
try {
const event = new Event('resize');
window.dispatchEvent(event);
}
finally {
window.removeEventListener('resize', spy);
}
assert.isTrue(spy.called, 'Expected "resize" event to fire');
});
it('listens to orientationchange', () => {
const lightbox = document.querySelector('#lightbox');
const spy = sinon.spy();
window.addEventListener('orientationchange', spy, true);
try {
const event = new Event('orientationchange');
window.dispatchEvent(event);
}
finally {
window.removeEventListener('orientationchange', spy);
}
assert.isTrue(spy.called, 'Expected "orientationchange" event to fire');
});
it('emits an "action" custom event when a button is clicked', () => {
const lightbox = document.querySelector('#lightbox');
const button = lightbox.querySelector('.mdl-button');
assert.isNotNull(button, 'Expected handle to button');
const spy = sinon.spy();
lightbox.addEventListener('action', spy);
const actionListener = ( event ) => {
assert.isDefined(event.detail, 'Expected detail to be defined in event');
assert.isDefined(event.detail.source, 'Expected detail.source to be defined in event');
};
lightbox.addEventListener('action', actionListener);
try {
// Trigger click on a button
const evt = new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window
});
button.dispatchEvent(evt);
}
finally {
lightbox.removeEventListener('select', spy);
lightbox.removeEventListener('select', actionListener);
}
assert.isTrue(spy.called, 'Expected "action" event to fire');
});
});