summaryrefslogtreecommitdiff
path: root/javascript/videojs/test/unit/plugin-basic.test.js
blob: 7ae81ebcd13e0a9d1c353cd6c4edf78898fba4e7 (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
/* eslint-env qunit */
import sinon from 'sinon';
import Plugin from '../../src/js/plugin';
import TestHelpers from './test-helpers';

QUnit.module('Plugin: basic', {

  beforeEach() {
    this.basic = sinon.spy();
    this.player = TestHelpers.makePlayer();

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

  afterEach() {
    this.player.dispose();

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

QUnit.test('pre-setup interface', function(assert) {
  assert.strictEqual(typeof this.player.basic, 'function', 'basic plugins are a function on a player');
  assert.ok(this.player.hasPlugin('basic'), 'player has the plugin available');
  assert.notStrictEqual(this.player.basic, this.basic, 'basic plugins are wrapped');
  assert.strictEqual(this.player.basic.dispose, undefined, 'unlike advanced plugins, basic plugins do not have a dispose method');
  assert.notOk(this.player.usingPlugin('basic'), 'the player is not using the plugin');
});

QUnit.test('setup', function(assert) {
  this.player.basic({foo: 'bar'}, 123);
  assert.strictEqual(this.basic.callCount, 1, 'the plugin was called once');
  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');

  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('all "pluginsetup" events', function(assert) {
  const setupSpy = sinon.spy();
  const events = [
    'beforepluginsetup',
    'beforepluginsetup:basic',
    'pluginsetup',
    'pluginsetup:basic'
  ];

  this.player.on(events, setupSpy);

  const instance = this.player.basic();

  events.forEach((type, i) => {
    const event = setupSpy.getCall(i).args[0];
    const hash = setupSpy.getCall(i).args[1];

    assert.strictEqual(event.type, type, `the "${type}" event was triggered`);
    assert.strictEqual(event.target, this.player.el_, 'the event has the correct target');

    assert.deepEqual(hash, {
      name: 'basic',

      // The "before" events have a `null` instance and the others have the
      // return value of the plugin factory.
      instance: i < 2 ? null : instance,
      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');
});