Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use custom Player class #3458

Merged
merged 10 commits into from
Nov 23, 2016
8 changes: 8 additions & 0 deletions src/js/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,14 @@ class Component {
Component.components_ = {};
}

if (name === 'Player' && Component.components_[name]) {
const Player = Component.components_[name];

if (Player.players && Object.keys(Player.players).length > 0) {
throw new Error('Can not register Player component after player has been created');
}
}

Component.components_[name] = comp;

return comp;
Expand Down
3 changes: 2 additions & 1 deletion src/js/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ function videojs(id, options, ready) {
options = mergeOptions(options, opts);
});

const PlayerComponent = Component.getComponent('Player');
// If not, set up a new player
const player = new Player(tag, options, ready);
const player = new PlayerComponent(tag, options, ready);

videojs.hooks('setup').forEach((hookFunction) => hookFunction(player));

Expand Down
33 changes: 33 additions & 0 deletions test/unit/player.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ import TechFaker from './tech/tech-faker.js';
QUnit.module('Player', {
beforeEach() {
this.clock = sinon.useFakeTimers();
// reset players storage
for (const playerId in Player.players) {
if (Player.players[playerId] !== null) {
Player.players[playerId].dispose();
}
delete Player.players[playerId];
}
},
afterEach() {
this.clock.restore();
Expand Down Expand Up @@ -1252,3 +1259,29 @@ QUnit.test('When VIDEOJS_NO_DYNAMIC_STYLE is set, apply sizing directly to the t
assert.equal(player.tech_.el().height, 300, 'the height is equal 300');
player.dispose();
});

QUnit.test('should allow to register custom player when any player has not been created', function(assert) {
class CustomPlayer extends Player {}
videojs.registerComponent('Player', CustomPlayer);

const tag = TestHelpers.makeTag();
const player = videojs(tag);

assert.equal(player instanceof CustomPlayer, true, 'player is custom');
player.dispose();
});

QUnit.test('should not allow to register custom player when any player has been created', function(assert) {
const tag = TestHelpers.makeTag();
const player = videojs(tag);

class CustomPlayer extends Player {}
try {
videojs.registerComponent('Player', CustomPlayer);
} catch (e) {
player.dispose();
return assert.equal(e.message, 'Can not register Player component after player has been created');
}

assert.ok(false, 'It should throw Error when any player has been created');
});