forked from capricorn86/happy-dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTMLMediaElement.test.ts
300 lines (263 loc) · 7.9 KB
/
HTMLMediaElement.test.ts
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import Window from '../../../src/window/Window';
import IWindow from '../../../src/window/IWindow';
import DOMException from '../../../src/exception/DOMException';
import DOMExceptionNameEnum from '../../../src/exception/DOMExceptionNameEnum';
import IDocument from '../../../src/nodes/document/IDocument';
import IHTMLMediaElement from '../../../src/nodes/html-media-element/IHTMLMediaElement';
import Event from '../../../src/event/Event';
describe('HTMLMediaElement', () => {
let window: IWindow;
let document: IDocument;
let element: IHTMLMediaElement;
beforeEach(() => {
window = new Window();
document = window.document;
element = <IHTMLMediaElement>document.createElement('audio');
});
afterEach(() => {
jest.restoreAllMocks();
});
for (const property of ['autoplay', 'controls', 'loop', 'muted']) {
describe(`get ${property}()`, () => {
it('Returns attribute value.', () => {
expect(element[property]).toBe(false);
element.setAttribute(property, '');
expect(element[property]).toBe(true);
});
});
describe(`set ${property}()`, () => {
it('Sets attribute value.', () => {
element[property] = true;
expect(element.getAttribute(property)).toBe('');
});
it('Remove attribute value.', () => {
element.setAttribute(property, '');
element[property] = false;
expect(element.getAttribute(property)).toBeNull();
});
});
}
for (const property of ['src']) {
describe(`get ${property}()`, () => {
it(`Returns the "${property}" attribute.`, () => {
element.setAttribute(property, 'test');
expect(element[property]).toBe('test');
});
});
describe(`set ${property}()`, () => {
it(`Sets the attribute "${property}".`, () => {
element[property] = 'test';
expect(element.getAttribute(property)).toBe('test');
});
});
}
describe(`get defaultMuted()`, () => {
it('Returns value.', () => {
expect(element.defaultMuted).toBe(false);
element.defaultMuted = true;
expect(element.defaultMuted).toBe(true);
});
});
describe(`set defaultMuted()`, () => {
it('Sets attribute value.', () => {
element.defaultMuted = true;
expect(element.getAttribute('muted')).toBe('');
});
it('Remove attribute value.', () => {
element.defaultMuted = true;
element.defaultMuted = false;
expect(element.getAttribute('muted')).toBeNull();
});
});
describe('canplay event', () => {
it('Should dispatch after src set', () => {
let dispatchedEvent: Event = null;
element.addEventListener('canplay', (event: Event) => (dispatchedEvent = event));
element.src = 'https://songURL';
expect(dispatchedEvent.cancelable).toBe(false);
expect(dispatchedEvent.bubbles).toBe(false);
});
it('Should not dispatch if src is empty', () => {
let dispatchedEvent: Event = null;
element.addEventListener('canplay', (event: Event) => (dispatchedEvent = event));
element.src = '';
expect(dispatchedEvent).toBeNull();
});
});
describe('currentSrc', () => {
it('Returns the current src', () => {
const src = 'https://src';
element.src = src;
expect(element.currentSrc).toBe(src);
});
});
describe('paused', () => {
it('Default is true', () => {
expect(element.paused).toBeTruthy();
});
it('Set false with play', () => {
element.play();
expect(element.paused).toBeFalsy();
});
it('Set true with pause', () => {
element.play();
element.pause();
expect(element.paused).toBeTruthy();
});
});
describe('volume()', () => {
it('Returns default value', () => {
expect(element.volume).toBe(1);
});
it('Set value', () => {
element.volume = 0.5;
expect(element.volume).toBe(0.5);
});
it('Set parse volmue as a number', () => {
element.volume = '0.5';
expect(element.volume).toBe(0.5);
});
it('Throw type error if volume is not a number', () => {
expect(() => {
element.volume = 'zeropointfive';
}).toThrowError(
new TypeError(
`Failed to set the 'volume' property on 'HTMLMediaElement': The provided double value is non-finite.`
)
);
});
for (const volume of [-0.4, 1.3]) {
it(`Throw error if out of range: ${volume}`, () => {
expect(() => {
element.volume = volume;
}).toThrowError(
new DOMException(
`Failed to set the 'volume' property on 'HTMLMediaElement': The volume provided (${volume}) is outside the range [0, 1].`,
DOMExceptionNameEnum.indexSizeError
)
);
});
}
});
describe('canPlayType', () => {
it('Returns empty string', () => {
expect(element.canPlayType('notValidMIMEtype')).toBe('');
});
});
describe('enden', () => {
it('Returns false', () => {
expect(element.ended).toBeFalsy();
});
});
describe('CrossOrigin', () => {
for (const crossOrigin of ['', null, 'use-credentials', 'anonymous']) {
it(`Set ${crossOrigin} as a valid crossOrigin`, () => {
element.crossOrigin = crossOrigin;
expect(element.getAttribute('crossorigin')).toBe(crossOrigin);
expect(element.crossOrigin).toBe(crossOrigin);
});
}
it(`Return 'anonymous' if crossOrigin is not valid`, () => {
element.crossOrigin = 'randomString';
expect(element.getAttribute('crossorigin')).toBe('anonymous');
expect(element.crossOrigin).toBe('anonymous');
});
});
describe('duration', () => {
it('Return NaN by default', () => {
expect(element.duration).toBe(NaN);
});
});
describe('currentTime', () => {
it('Return default value', () => {
expect(element.currentTime).toBe(0);
});
it('Set value', () => {
element.currentTime = 42;
expect(element.currentTime).toBe(42);
});
it('Set value as a string', () => {
element.currentTime = '42';
expect(element.currentTime).toBe(42);
});
it('Throw type error if currentTime is not a number', () => {
expect(() => {
element.currentTime = 'zeropointfive';
}).toThrowError(
new TypeError(
`Failed to set the 'currentTime' property on 'HTMLMediaElement': The provided double value is non-finite.`
)
);
});
});
describe('playbackRate', () => {
it('Return default value', () => {
expect(element.playbackRate).toBe(1);
});
it('Set value', () => {
element.playbackRate = 2.3;
expect(element.playbackRate).toBe(2.3);
});
it('Set value as a string', () => {
element.playbackRate = '2.3';
expect(element.playbackRate).toBe(2.3);
});
it('Throw type error if playbackRate is not a number', () => {
expect(() => {
element.playbackRate = 'zeropointfive';
}).toThrowError(
new TypeError(
`Failed to set the 'playbackRate' property on 'HTMLMediaElement': The provided double value is non-finite.`
)
);
});
});
describe('defaultPlaybackRate', () => {
it('Return default value', () => {
expect(element.defaultPlaybackRate).toBe(1);
});
it('Set value', () => {
element.defaultPlaybackRate = 2.3;
expect(element.defaultPlaybackRate).toBe(2.3);
});
it('Set value as a string', () => {
element.defaultPlaybackRate = '0.3';
expect(element.defaultPlaybackRate).toBe(0.3);
});
it('Throw type error if defaultPlaybackRate is not a number', () => {
expect(() => {
element.defaultPlaybackRate = 'zeropointfive';
}).toThrowError(
new TypeError(
`Failed to set the 'defaultPlaybackRate' property on 'HTMLMediaElement': The provided double value is non-finite.`
)
);
});
});
describe('error', () => {
it('Return null by default', () => {
expect(element.error).toBeNull();
});
});
describe('networkState', () => {
it('Return 0 by default', () => {
expect(element.networkState).toBe(0);
});
});
describe('preservesPitch', () => {
it('Return true by default', () => {
expect(element.preservesPitch).toBe(true);
});
for (const property of [null, undefined, false]) {
it(`Set false with ${property}`, () => {
element.preservesPitch = property;
expect(element.preservesPitch).toBe(false);
});
}
});
describe('readyState', () => {
it('Return 0 by default', () => {
expect(element.readyState).toBe(0);
});
});
});