summaryrefslogtreecommitdiff
path: root/javascript/videojs/test/unit/plugin-static.test.js
blob: ad2ac34c8044c044c33a1efe0975836ff0003457 (plain)
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
/* eslint-env qunit */
import sinon from 'sinon';
import log from '../../src/js/utils/log';
import Player from '../../src/js/player';
import Plugin from '../../src/js/plugin';

class MockPlugin extends Plugin {}

MockPlugin.VERSION = 'v1.2.3';

QUnit.module('Plugin: static methods', {

  beforeEach() {
    this.basic = () => {};

    Plugin.registerPlugin('basic', this.basic);
    Plugin.registerPlugin('mock', MockPlugin);
  },

  afterEach() {
    Object.keys(Plugin.getPlugins()).forEach(key => {
      if (key !== Plugin.BASE_PLUGIN_NAME) {
        Plugin.deregisterPlugin(key);
      }
    });
  }
});

QUnit.test('registerPlugin() works with basic plugins', function(assert) {
  const foo = () => {};

  assert.strictEqual(Plugin.registerPlugin('foo', foo), foo, 'the plugin is returned');
  assert.strictEqual(Plugin.getPlugin('foo'), foo, 'the plugin can be retrieved');
  assert.strictEqual(Object.prototype.toString.call(Player.prototype.foo), '[object Function]', 'the plugin has a wrapper function');
  assert.notStrictEqual(Player.prototype.foo, foo, 'the function on the player prototype is a wrapper');

  Plugin.deregisterPlugin('foo');
});

QUnit.test('registerPlugin() works with class-based plugins', function(assert) {
  class Foo extends Plugin {}

  assert.strictEqual(Plugin.registerPlugin('foo', Foo), Foo, 'the plugin is returned');
  assert.strictEqual(Plugin.getPlugin('foo'), Foo, 'the plugin can be retrieved');
  assert.strictEqual(Object.prototype.toString.call(Player.prototype.foo), '[object Function]', 'the plugin has a factory function');
  assert.notStrictEqual(Player.prototype.foo, Foo, 'the function on the player prototype is a factory');

  Plugin.deregisterPlugin('foo');
});

QUnit.test('registerPlugin() illegal arguments', function(assert) {
  assert.throws(
    () => Plugin.registerPlugin(),
    new Error('Illegal plugin name, "undefined", must be a string, was undefined.'),
    'plugins must have a name'
  );

  assert.throws(
    () => Plugin.registerPlugin('foo'),
    new Error('Illegal plugin for "foo", must be a function, was undefined.'),
    'plugins require both arguments'
  );

  assert.throws(
    () => Plugin.registerPlugin('foo', {}),
    new Error('Illegal plugin for "foo", must be a function, was object.'),
    'plugins must be functions'
  );

  assert.throws(
    () => Plugin.registerPlugin('play', function() {}),
    new Error('Illegal plugin name, "play", cannot share a name with an existing player method!'),
    'plugins must be functions'
  );

  sinon.stub(log, 'warn');
  Plugin.registerPlugin('foo', function() {});
  Plugin.registerPlugin('foo', function() {});
  assert.strictEqual(log.warn.callCount, 1, 'warn on re-registering a plugin');
  log.warn.restore();
});

QUnit.test('getPlugin()', function(assert) {
  assert.ok(Plugin.getPlugin('basic'), 'the "basic" plugin exists');
  assert.ok(Plugin.getPlugin('mock'), 'the "mock" plugin exists');
  assert.strictEqual(Plugin.getPlugin(), undefined, 'returns undefined with no arguments');
  assert.strictEqual(Plugin.getPlugin('nonExistent'), undefined, 'returns undefined with non-existent plugin');
  assert.strictEqual(Plugin.getPlugin(123), undefined, 'returns undefined with an invalid type');
});

QUnit.test('getPluginVersion()', function(assert) {
  assert.strictEqual(Plugin.getPluginVersion('basic'), '', 'the basic plugin has no version');
  assert.strictEqual(Plugin.getPluginVersion('mock'), 'v1.2.3', 'a plugin with a version returns its version');
});

QUnit.test('getPlugins()', function(assert) {
  assert.strictEqual(Object.keys(Plugin.getPlugins()).length, 3, 'all plugins are returned by default');
  assert.strictEqual(Plugin.getPlugins().basic, this.basic, 'the "basic" plugin is included');
  assert.strictEqual(Plugin.getPlugins().mock, MockPlugin, 'the "mock" plugin is included');
  assert.strictEqual(Plugin.getPlugins().plugin, Plugin, 'the "plugin" plugin is included');
  assert.strictEqual(Object.keys(Plugin.getPlugins(['basic'])).length, 1, 'a subset of plugins can be requested');
  assert.strictEqual(Plugin.getPlugins(['basic']).basic, this.basic, 'the correct subset of plugins is returned');
});

QUnit.test('deregisterPlugin()', function(assert) {
  const foo = () => {};

  Plugin.registerPlugin('foo', foo);
  Plugin.deregisterPlugin('foo');

  assert.strictEqual(Player.prototype.foo, undefined, 'the player prototype method is removed');
  assert.strictEqual(Plugin.getPlugin('foo'), undefined, 'the plugin can no longer be retrieved');

  assert.throws(
    () => Plugin.deregisterPlugin('plugin'),
    new Error('Cannot de-register base plugin.'),
    'the base plugin cannot be de-registered'
  );
});

QUnit.test('isBasic()', function(assert) {
  assert.ok(Plugin.isBasic(this.basic), 'the "basic" plugin is a basic plugin (by reference)');
  assert.ok(Plugin.isBasic('basic'), 'the "basic" plugin is a basic plugin (by name)');
  assert.notOk(Plugin.isBasic(MockPlugin), 'the "mock" plugin is NOT a basic plugin (by reference)');
  assert.notOk(Plugin.isBasic('mock'), 'the "mock" plugin is NOT a basic plugin (by name)');
});