This repository was archived by the owner on May 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
112 lines (88 loc) · 2.83 KB
/
test.js
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
import { test } from 'zora';
function createVideoElement() {
return fixture(`<jwplayer-video
src="https://cdn.jwplayer.com/players/C8YE48zj-IxzuqJ4M.html"
muted
></jwplayer-video>`);
}
test('has default video props', async function (t) {
const video = await createVideoElement();
t.equal(video.paused, true, 'is paused on initialization');
await video.loadComplete;
t.equal(video.paused, true, 'is paused on initialization');
t.ok(!video.ended, 'is not ended');
t.ok(video.muted, 'is muted');
});
test('volume', async function (t) {
const video = await createVideoElement();
await video.loadComplete;
video.volume = 1;
await delay(100); // postMessage is not instant
t.equal(video.volume, 1, 'is all turned up. volume: ' + video.volume);
video.volume = 0.5;
await delay(100); // postMessage is not instant
t.equal(video.volume, 0.5, 'is half volume');
});
test('loop', async function (t) {
const video = await createVideoElement();
await video.loadComplete;
t.ok(!video.loop, 'loop is false by default');
video.loop = true;
t.ok(video.loop, 'loop is true');
});
test('duration', async function (t) {
const video = await createVideoElement();
await video.loadComplete;
if (video.duration == null || Number.isNaN(video.duration)) {
await promisify(video.addEventListener.bind(video))('durationchange');
}
t.equal(Math.round(video.duration), 115, `is 115s long`);
});
test('load promise', async function (t) {
const video = await createVideoElement();
await video.loadComplete;
const loadComplete = video.loadComplete;
video.src = 'https://cdn.jwplayer.com/players/R12Nj7bO-Pd4r8gwe.html';
await video.loadComplete;
t.ok(
loadComplete != video.loadComplete,
'creates a new promise after new src'
);
if (video.duration == null || Number.isNaN(video.duration)) {
await promisify(video.addEventListener.bind(video))('durationchange');
}
t.equal(Math.round(video.duration), 90, `is 90s long`);
});
test('play promise', async function (t) {
const video = await createVideoElement();
await video.loadComplete;
video.muted = true;
try {
await video.play();
} catch (error) {
console.warn(error);
}
t.ok(!video.paused, 'is playing after video.play()');
});
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function fixture(html) {
const template = document.createElement('template');
template.innerHTML = html;
const fragment = template.content.cloneNode(true);
const result = fragment.children.length > 1
? [...fragment.children]
: fragment.children[0];
document.body.append(fragment);
return result;
}
function promisify(fn) {
return (...args) =>
new Promise((resolve) => {
fn(...args, (...res) => {
if (res.length > 1) resolve(res);
else resolve(res[0]);
});
});
}