Permalink
Browse files

feat: Add `videojs.getAllPlayers` to get an array of players. (#4842)

  • Loading branch information...
misteroneill authored and gkatsev committed Jan 30, 2018
1 parent bf3eb45 commit 6a0057716b7155dc160a5b759c8bd0519e83cee5
Showing with 40 additions and 31 deletions.
  1. +15 −0 src/js/video.js
  2. +25 −31 test/unit/video.test.js
@@ -285,6 +285,21 @@ videojs.getPlayer = (id) => {
}
};
/**
* Returns an array of all current players.
*
* @return {Array}
* An array of all players. The array will be in the order that
* `Object.keys` provides, which could potentially vary between
* JavaScript engines.
*
*/
videojs.getAllPlayers = () =>
// Disposed players leave a key with a `null` value, so we need to make sure
// we filter those out.
Object.keys(Player.players).map(k => Player.players[k]).filter(Boolean);
/**
* Expose players object.
*
@@ -11,6 +11,7 @@ QUnit.module('video.js', {
},
afterEach() {
this.clock.restore();
videojs.getAllPlayers().forEach(p => p.dispose());
}
});
@@ -48,9 +49,6 @@ QUnit.test('should return a video player instance', function(assert) {
const player2 = videojs(tag2, { techOrder: ['techFaker'] });
assert.ok(player2.id() === 'test_vid_id2', 'created player from element');
player.dispose();
player2.dispose();
});
QUnit.test('should log if the supplied element is not included in the DOM',
@@ -89,8 +87,6 @@ function(assert) {
assert.equal(warnLogs.length, 1, 'did not log another warning');
log.warn = origWarnLog;
player.dispose();
player2.dispose();
});
QUnit.test('should log about already initalized players if options already passed',
@@ -127,8 +123,6 @@ function(assert) {
'logged the right message');
log.warn = origWarnLog;
player.dispose();
});
QUnit.test('should return a video player instance from el html5 tech', function(assert) {
@@ -155,9 +149,6 @@ QUnit.test('should return a video player instance from el html5 tech', function(
const player2 = videojs(tag2, { techOrder: ['techFaker'] });
assert.ok(player2.id() === 'test_vid_id2', 'created player from element');
player.dispose();
player2.dispose();
});
QUnit.test('should return a video player instance from el techfaker', function(assert) {
@@ -183,9 +174,6 @@ QUnit.test('should return a video player instance from el techfaker', function(a
const player2 = videojs(tag2, { techOrder: ['techFaker'] });
assert.ok(player2.id() === 'test_vid_id2', 'created player from element');
player.dispose();
player2.dispose();
});
QUnit.test('should add the value to the languages object', function(assert) {
@@ -295,8 +283,6 @@ QUnit.test('ingest player div if data-vjs-player attribute is present on video p
assert.equal(player.el(), playerDiv, 'we re-used the given div');
assert.ok(player.hasClass('foo'), 'keeps any classes that were around previously');
player.dispose();
});
QUnit.test('ingested player div should not create a new tag for movingMediaElementInDOM', function(assert) {
@@ -330,7 +316,6 @@ QUnit.test('ingested player div should not create a new tag for movingMediaEleme
assert.equal(player.tech_.el(), vid, 'we re-used the video element');
assert.ok(player.hasClass('foo'), 'keeps any classes that were around previously');
player.dispose();
Html5.prototype.movingMediaElementInDOM = oldMoving;
Html5.isSupported = oldIS;
Html5.nativeSourceHandler.canPlayType = oldCPT;
@@ -366,7 +351,6 @@ QUnit.test('should create a new tag for movingMediaElementInDOM', function(asser
assert.notEqual(player.el(), playerDiv, 'we used a new div');
assert.notEqual(player.tech_.el(), vid, 'we a new video element');
player.dispose();
Html5.prototype.movingMediaElementInDOM = oldMoving;
Html5.isSupported = oldIS;
Html5.nativeSourceHandler.canPlayType = oldCPT;
@@ -389,6 +373,28 @@ QUnit.test('getPlayer', function(assert) {
player.dispose();
});
QUnit.test('getAllPlayers', function(assert) {
const fixture = document.getElementById('qunit-fixture');
fixture.innerHTML += '<video id="test_vid_id"></video>' +
'<video id="test_vid_id2"></video>';
let all = videojs.getAllPlayers();
assert.ok(Array.isArray(all), 'an array was returned');
assert.strictEqual(all.length, 0, 'the array was empty because no players have been created yet');
const player = videojs('test_vid_id');
const player2 = videojs('test_vid_id2');
all = videojs.getAllPlayers();
assert.ok(Array.isArray(all), 'an array was returned');
assert.strictEqual(all.length, 2, 'the array had two items');
assert.notStrictEqual(all.indexOf(player), -1, 'the first player was in the array');
assert.notStrictEqual(all.indexOf(player2), -1, 'the second player was in the array');
});
/* **************************************************** *
* div embed tests copied from video emebed tests above *
* **************************************************** */
@@ -398,8 +404,10 @@ QUnit.module('video.js video-js embed', {
},
afterEach() {
this.clock.restore();
videojs.getAllPlayers().forEach(p => p.dispose());
}
});
QUnit.test('should return a video player instance', function(assert) {
const fixture = document.getElementById('qunit-fixture');
@@ -423,9 +431,6 @@ QUnit.test('should return a video player instance', function(assert) {
const player2 = videojs(tag2, { techOrder: ['techFaker'] });
assert.ok(player2.id() === 'test_vid_id2', 'created player from element');
player.dispose();
player2.dispose();
});
QUnit.test('should log about already initalized players if options already passed',
@@ -462,8 +467,6 @@ function(assert) {
'logged the right message');
log.warn = origWarnLog;
player.dispose();
});
QUnit.test('should return a video player instance from el html5 tech', function(assert) {
@@ -490,9 +493,6 @@ QUnit.test('should return a video player instance from el html5 tech', function(
const player2 = videojs(tag2, { techOrder: ['techFaker'] });
assert.ok(player2.id() === 'test_vid_id2', 'created player from element');
player.dispose();
player2.dispose();
});
QUnit.test('should return a video player instance from el techfaker', function(assert) {
@@ -518,9 +518,6 @@ QUnit.test('should return a video player instance from el techfaker', function(a
const player2 = videojs(tag2, { techOrder: ['techFaker'] });
assert.ok(player2.id() === 'test_vid_id2', 'created player from element');
player.dispose();
player2.dispose();
});
QUnit.test('adds video-js class name with the video-js embed', function(assert) {
@@ -536,7 +533,4 @@ QUnit.test('adds video-js class name with the video-js embed', function(assert)
assert.ok(player.hasClass('video-js'), 'video-js class was added to the first embed');
assert.ok(player2.hasClass('video-js'), 'video-js class was preserved to the second embed');
player.dispose();
player2.dispose();
});

0 comments on commit 6a00577

Please sign in to comment.