forked from capricorn86/happy-dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTMLMediaElement.test.ts
148 lines (129 loc) · 4.09 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
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('');
});
});
}
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('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('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('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);
});
}
it(`Return 'anonymous' if crossOrigin is not valid`, () => {
element.crossOrigin = 'randomString';
expect(element.getAttribute('crossorigin')).toBe('anonymous');
});
});
});