1
0
mirror of https://github.com/videojs/video.js.git synced 2024-12-04 10:34:51 +02:00

fix: copy basic plugin properties onto the wrapper (#4100)

Copy basic plugin properties to prevent breaking older basic plugins.
This commit is contained in:
Brandon Casey 2017-02-21 15:49:42 -05:00 committed by Gary Katsevman
parent 405b29b8f1
commit 127cd7827e
2 changed files with 41 additions and 9 deletions

View File

@ -88,17 +88,25 @@ const markPluginAsActive = (player, name) => {
* @returns {Function}
* A wrapper function for the given plugin.
*/
const createBasicPlugin = (name, plugin) => function() {
const instance = plugin.apply(this, arguments);
const createBasicPlugin = function(name, plugin) {
const basicPluginWrapper = function() {
const instance = plugin.apply(this, arguments);
markPluginAsActive(this, name);
markPluginAsActive(this, name);
// We trigger the "pluginsetup" event on the player regardless, but we want
// the hash to be consistent with the hash provided for advanced plugins.
// The only potentially counter-intuitive thing here is the `instance` is the
// value returned by the `plugin` function.
this.trigger('pluginsetup', {name, plugin, instance});
return instance;
// We trigger the "pluginsetup" event on the player regardless, but we want
// the hash to be consistent with the hash provided for advanced plugins.
// The only potentially counter-intuitive thing here is the `instance` is the
// value returned by the `plugin` function.
this.trigger('pluginsetup', {name, plugin, instance});
return instance;
};
Object.keys(plugin).forEach(function(prop) {
basicPluginWrapper[prop] = plugin[prop];
});
return basicPluginWrapper;
};
/**

View File

@ -38,6 +38,13 @@ QUnit.test('setup', function(assert) {
assert.deepEqual(this.basic.firstCall.args, [{foo: 'bar'}, 123], 'the plugin had the correct arguments');
assert.ok(this.player.usingPlugin('basic'), 'the player now recognizes that the plugin was set up');
assert.ok(this.player.hasPlugin('basic'), 'player has the plugin available');
this.player.basic({foo: 'bar'}, 123);
assert.strictEqual(this.basic.callCount, 2, 'the plugin was called twice');
assert.strictEqual(this.basic.firstCall.thisValue, this.player, 'the plugin `this` value was the player');
assert.deepEqual(this.basic.firstCall.args, [{foo: 'bar'}, 123], 'the plugin had the correct arguments');
assert.ok(this.player.usingPlugin('basic'), 'the player now recognizes that the plugin was set up');
assert.ok(this.player.hasPlugin('basic'), 'player has the plugin available');
});
QUnit.test('"pluginsetup" event', function(assert) {
@ -58,3 +65,20 @@ QUnit.test('"pluginsetup" event', function(assert) {
plugin: this.basic
}, 'the event hash object is correct');
});
QUnit.test('properties are copied', function(assert) {
const foo = () => {};
foo.someProp = () => {};
foo.VERSION = '9.9.9';
Plugin.registerPlugin('foo', foo);
assert.strictEqual(this.player.foo.VERSION, foo.VERSION, 'properties are copied');
assert.strictEqual(this.player.foo.someProp, foo.someProp, 'properties are copied');
this.player.foo();
assert.strictEqual(this.player.foo.VERSION, foo.VERSION, 'properties still exist after init');
assert.strictEqual(this.player.foo.someProp, foo.someProp, 'properties still exist after init');
});