Skip to content

Commit

Permalink
fix: copy basic plugin properties onto the wrapper (#4100)
Browse files Browse the repository at this point in the history
Copy basic plugin properties to prevent breaking older basic plugins.
  • Loading branch information
brandonocasey authored and gkatsev committed Feb 21, 2017
1 parent 405b29b commit 127cd78
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/js/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/**
Expand Down
24 changes: 24 additions & 0 deletions test/unit/plugin-basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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');
});

0 comments on commit 127cd78

Please sign in to comment.