-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtest.js
More file actions
333 lines (296 loc) · 8.23 KB
/
test.js
File metadata and controls
333 lines (296 loc) · 8.23 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
var Player = window.youtube.Player;
var player;
var elParent;
var elPlayer;
var videoId;
var originalStatics = {};
var original_Player_prepareYTScript = Player.prepareYTScript;
Player.prepareYTScript = function(callback) {
callback();
};
var original_createPlayer = Player.prototype._createPlayer;
Player.prototype._createPlayer = function(options) {
var instance = this;
return {
playVideo: function() {
instance.onStateChange({ data:Player.PlayerState.PLAYING });
},
destroy: function() {
}
};
};
for (var name in Player) {
originalStatics[name] = Player[name];
}
beforeEach(function() {
// restore statics
for (var name in Player) {
delete Player[name];
}
for (var name in originalStatics) {
Player[name] = originalStatics[name];
}
// create materials
elParent = document.createElement('div');
elPlayer = document.createElement('div');
videoId = '123';
elParent.appendChild(elPlayer);
// build an instance
player = new Player({
el: elPlayer,
id: videoId
});
});
afterEach(function() {
player.destroy();
});
describe('Statics', function() {
describe('interface', function() {
it('builds new instance', function() {
var player = window.youtube({ el:elPlayer, id:videoId });
expect(player instanceof Player).toBeTruthy();
});
});
describe('bind', function() {
it('fixes its context', function() {
var obj = {};
var fn = function() { return this; };
var binded = Player.bind(fn, obj);
expect(binded()).toBe(obj);
});
it('fixes its arguments', function() {
var obj = {};
var fn = function(arg1) { return arg1; };
var binded = Player.bind(fn, null, obj);
expect(binded()).toBe(obj);
});
});
});
describe('Constructing', function() {
describe('instance', function() {
it('is an instance', function() {
expect(player instanceof Player).toBeTruthy();
});
});
describe('video options', function() {
it('throws an error if the options is not specified', function() {
expect(function() {
player._getVideoOptions();
}).toThrow(new Error('`options.el` is require.'));
});
it('throws an error if the target element is not specified', function() {
expect(function() {
player._getVideoOptions({ el:null });
}).toThrow(new Error('`options.el` is require.'));
});
it('throws an error if the target element is an element', function() {
expect(function() {
player._getVideoOptions({ el:{} });
}).toThrow(new Error('`options.el` is require.'));
});
it('has videoId if ID is specified as a data attribute on the element', function() {
elPlayer.setAttribute('data-youtube-videoid', videoId);
var videoOptions = player._getVideoOptions({ el:elPlayer });
expect(videoOptions.videoId).toBe(videoId);
});
describe('playerVars settings', function() {
it('uses the value specified in options if specified on the element', function() {
elPlayer.setAttribute('data-youtube-controls', '1');
var videoOptions = player._getVideoOptions({ el:elPlayer, controls:0 });
expect(videoOptions.playerVars.controls).toBe(0);
});
it('let youtube fallback to default settings if not specified', function() {
elPlayer.removeAttribute('data-youtube-controls');
var videoOptions = player._getVideoOptions({ el:elPlayer });
expect(videoOptions.playerVars.controls).toBe(undefined);
});
it('turns setting on if "true" is specified', function() {
elPlayer.setAttribute('data-youtube-controls', 'true');
var videoOptions = player._getVideoOptions({ el:elPlayer });
expect(videoOptions.playerVars.controls).toBe(1);
});
it('let youtube fallback to default settings if invalid number like "-1" is specified', function() {
elPlayer.setAttribute('data-youtube-controls', '-1');
var videoOptions = player._getVideoOptions({ el:elPlayer });
expect(videoOptions.playerVars.controls).toBe(undefined);
});
it('turns setting off if "false" is specified', function() {
elPlayer.setAttribute('data-youtube-controls', 'false');
var videoOptions = player._getVideoOptions({ el:elPlayer });
expect(videoOptions.playerVars.controls).toBe(0);
});
it('accepts strings like "playlist" as valid values', function() {
elPlayer.setAttribute('data-youtube-listType', 'playlist');
var videoOptions = player._getVideoOptions({ el:elPlayer });
expect(videoOptions.playerVars.listType).toBe('playlist');
});
it('allows youtube playerVars settings as booleans', function() {
elPlayer.removeAttribute('data-youtube-controls');
var videoOptions = player._getVideoOptions({
el:elPlayer,
autohide: false,
autoplay: true,
cc_load_policy: true,
color: 'white',
controls: false,
disablekb: false,
enablejsapi: true,
end: 23,
fs: false,
hl: 'en',
iv_load_policy: 3,
list: 'PLC77007E23FF423C6',
listType: 'playlist',
loop: false,
modestbranding: true,
origin: 'localhost',
playerapiid: '1234abcd',
playlist: '2EEsa_pqGAs,KFstP0C9sVk',
playsinline: true,
rel: false,
showinfo: false
});
expect(videoOptions.playerVars).toEqual({
autohide: 0,
autoplay: 1,
cc_load_policy: 1,
color: 'white',
controls: 0,
disablekb: 0,
enablejsapi: 1,
end: 23,
fs: 0,
hl: 'en',
iv_load_policy: 3,
list: 'PLC77007E23FF423C6',
listType: 'playlist',
loop: 0,
modestbranding: 1,
origin: 'localhost',
playerapiid: '1234abcd',
playlist: '2EEsa_pqGAs,KFstP0C9sVk',
playsinline: 1,
rel: 0,
showinfo: 0,
start: undefined,
theme: undefined
});
});
it('allows youtube playerVars settings as numbers', function() {
var videoOptions = player._getVideoOptions({
el:elPlayer,
autohide: 0,
autoplay: 1,
cc_load_policy: 1,
color: 'white',
controls: 0,
disablekb: 0,
enablejsapi: 1,
end: 23,
fs: 0,
hl: 'en',
iv_load_policy: 3,
list: 'PLC77007E23FF423C6',
listType: 'playlist',
loop: 0,
modestbranding: 1,
origin: 'localhost',
playerapiid: '1234abcd',
playlist: '2EEsa_pqGAs,KFstP0C9sVk',
playsinline: 1,
rel: 0,
showinfo: 0,
start: 10,
theme: 'light'
});
expect(videoOptions.playerVars).toEqual({
autohide: 0,
autoplay: 1,
cc_load_policy: 1,
color: 'white',
controls: 0,
disablekb: 0,
enablejsapi: 1,
end: 23,
fs: 0,
hl: 'en',
iv_load_policy: 3,
list: 'PLC77007E23FF423C6',
listType: 'playlist',
loop: 0,
modestbranding: 1,
origin: 'localhost',
playerapiid: '1234abcd',
playlist: '2EEsa_pqGAs,KFstP0C9sVk',
playsinline: 1,
rel: 0,
showinfo: 0,
start: 10,
theme: 'light'
});
});
});
});
describe('src', function() {
var videoId;
beforeEach(function() {
videoId = 'videoId' + Date.now();
});
it('loads the video by specified ID from the beginning', function() {
var result;
player._createPlayer = function(el, options) {
result = options.videoId;
};
player.player = null;
player.initialize({ el:elPlayer, id:videoId });
expect(result).toBe(videoId);
});
it('loads the video by ID that is set as `src` after ready event', function() {
var ytPlayer = player.player;
ytPlayer.cueVideoById = function(value) {
player._src = value;
};
ytPlayer.getVideoUrl = function(value) {
return player._src;
};
player.player = null;
player.src = videoId;
player.player = ytPlayer;
player.onReady();
expect(player.src).toBe(videoId);
});
});
});
describe('Events', function() {
describe('on()', function() {
it('adds a listener', function() {
var called = 0;
player.on('play', function() {
called++;
});
player.play();
expect(called).toBe(1);
});
it('returns its own', function() {
expect(player.on('', function(){})).toBe(player);
});
});
describe('off()', function() {
var called, listener;
beforeEach(function() {
called = 0;
listener = function() {
called++;
};
player.on('play', listener);
});
it('removes a listener', function() {
player.off('play', listener);
player.play();
expect(called).toBe(0);
});
it('returns its own', function() {
expect(player.off('play', listener)).toBe(player);
});
});
});