-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathtest.js
More file actions
318 lines (240 loc) · 9.11 KB
/
Copy pathtest.js
File metadata and controls
318 lines (240 loc) · 9.11 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import { test } from 'zora';
function createVideoElement() {
return fixture(`<youtube-video
src="https://www.youtube.com/watch?v=H3KSKS3TTbc"
muted
></youtube-video>`);
}
function createPlaylistElement() {
return fixture(`<youtube-video
src="https://www.youtube.com/playlist?list=PLRfhDHeBTBJ7MU5DX4P_oBIRN457ah9lA"
muted
></youtube-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('seeking while paused stays paused', async function (t) {
const video = await createVideoElement();
t.equal(video.paused, true, 'is paused on initialization');
await video.loadComplete;
video.currentTime = 23;
await promisify(video.addEventListener.bind(video))('seeked');
await delay(300); // postMessage is not instant
t.equal(video.paused, true, 'is paused after seek');
t.equal(Math.floor(video.currentTime), 23);
});
test('seeking while playing stays playing', async function (t) {
const video = await createVideoElement();
t.equal(video.paused, true, 'is paused on initialization');
await video.loadComplete;
try {
await video.play();
} catch (error) {
console.warn(error);
}
t.ok(!video.paused, 'is playing after video.play()');
video.currentTime = 23;
await promisify(video.addEventListener.bind(video))('seeked');
await delay(300); // postMessage is not instant
t.ok(!video.paused, 'is playing after seek');
t.equal(Math.floor(video.currentTime), 23);
});
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(700); // 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), 254, `is 254s long`);
});
test('load promise', async function (t) {
const video = await createVideoElement();
await video.loadComplete;
const loadComplete = video.loadComplete;
video.src = 'https://www.youtube.com/watch?v=C7dPqrmDWxs';
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), 235, `is 235s 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()');
});
test('playlist', async function (t) {
const playlist = await createPlaylistElement();
await playlist.loadComplete;
t.equal(playlist.paused, true, 'is paused on initialization');
t.ok(!playlist.ended, 'is not ended');
t.ok(playlist.muted, 'is muted');
if (playlist.duration == null || Number.isNaN(playlist.duration)) {
await promisify(playlist.addEventListener.bind(playlist))('durationchange');
}
t.ok(playlist.duration > 0, `has a duration of ${playlist.duration}`);
});
test('t parameter - basic seconds', async function (t) {
const video = await fixture(`<youtube-video
src="https://www.youtube.com/watch?v=H3KSKS3TTbc&t=171"
muted
></youtube-video>`);
await video.loadComplete;
const iframe = video.shadowRoot.querySelector('iframe');
const iframeUrl = new URL(iframe.src);
const startParam = iframeUrl.searchParams.get('start');
t.equal(startParam, '171', 'start parameter is set to 171 seconds');
});
test('t parameter - youtu.be format', async function (t) {
const video = await fixture(`<youtube-video
src="https://youtu.be/H3KSKS3TTbc?t=171"
muted
></youtube-video>`);
await video.loadComplete;
const iframe = video.shadowRoot.querySelector('iframe');
const iframeUrl = new URL(iframe.src);
const startParam = iframeUrl.searchParams.get('start');
t.equal(startParam, '171', 'start parameter is set to 171 seconds from youtu.be URL');
});
test('t parameter - with seconds suffix', async function (t) {
const video = await fixture(`<youtube-video
src="https://www.youtube.com/watch?v=H3KSKS3TTbc&t=171s"
muted
></youtube-video>`);
await video.loadComplete;
const iframe = video.shadowRoot.querySelector('iframe');
const iframeUrl = new URL(iframe.src);
const startParam = iframeUrl.searchParams.get('start');
t.equal(startParam, '171', 'start parameter is set to 171 seconds from t=171s');
});
test('t parameter - minutes and seconds', async function (t) {
const video = await fixture(`<youtube-video
src="https://www.youtube.com/watch?v=H3KSKS3TTbc&t=2m51s"
muted
></youtube-video>`);
await video.loadComplete;
const iframe = video.shadowRoot.querySelector('iframe');
const iframeUrl = new URL(iframe.src);
const startParam = iframeUrl.searchParams.get('start');
t.equal(startParam, '171', 'start parameter is set to 171 seconds from t=2m51s (2*60 + 51)');
});
test('t parameter - minutes only', async function (t) {
const video = await fixture(`<youtube-video
src="https://www.youtube.com/watch?v=H3KSKS3TTbc&t=3m"
muted
></youtube-video>`);
await video.loadComplete;
const iframe = video.shadowRoot.querySelector('iframe');
const iframeUrl = new URL(iframe.src);
const startParam = iframeUrl.searchParams.get('start');
t.equal(startParam, '180', 'start parameter is set to 180 seconds from t=3m (3*60)');
});
test('t parameter - no t parameter', async function (t) {
const video = await fixture(`<youtube-video
src="https://www.youtube.com/watch?v=H3KSKS3TTbc"
muted
></youtube-video>`);
await video.loadComplete;
const iframe = video.shadowRoot.querySelector('iframe');
const iframeUrl = new URL(iframe.src);
const startParam = iframeUrl.searchParams.get('start');
t.equal(startParam, null, 'start parameter is not set when t parameter is absent');
});
test('t parameter - case insensitive', async function (t) {
const video = await fixture(`<youtube-video
src="https://www.youtube.com/watch?v=H3KSKS3TTbc&T=171"
muted
></youtube-video>`);
await video.loadComplete;
const iframe = video.shadowRoot.querySelector('iframe');
const iframeUrl = new URL(iframe.src);
const startParam = iframeUrl.searchParams.get('start');
t.equal(startParam, '171', 'start parameter is set from uppercase T parameter');
});
test('destroys the player when disconnected', async function (t) {
const video = await createVideoElement();
await video.loadComplete;
t.ok(video.api, 'has a player once loaded');
t.ok(video.shadowRoot.querySelector('iframe'), 'has an iframe once loaded');
video.remove();
t.equal(video.api, null, 'the player reference is released on disconnect');
t.equal(video.isLoaded, false, 'the element is no longer marked loaded');
t.equal(
video.shadowRoot.querySelector('iframe'),
null,
'destroy() removed the iframe from the shadow root'
);
});
test('creates a new player when reconnected', async function (t) {
const video = await createVideoElement();
await video.loadComplete;
const firstApi = video.api;
video.remove();
document.body.append(video);
await video.loadComplete;
t.ok(video.api, 'has a player again after reconnecting');
t.ok(video.api !== firstApi, 'a new player was created, not the destroyed one');
t.ok(video.shadowRoot.querySelector('iframe'), 'the iframe was rebuilt');
});
test('disconnecting before the player is ready does not throw', async function (t) {
const video = await createVideoElement();
// Do not await loadComplete: the API may still be loading.
video.remove();
t.equal(video.api, null, 'no player is left behind');
t.ok(true, 'disconnecting mid-load did not throw');
});
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]);
});
});
}