summaryrefslogtreecommitdiff
path: root/javascript/videojs/test/unit/utils/deprecate.test.js
blob: b3f4a043493d0ba0244ea64b27baa1117c85d90e (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
/* eslint-env qunit */
import window from 'global/window';
import sinon from 'sinon';
import {deprecate, deprecateForMajor} from '../../../src/js/utils/deprecate.js';

QUnit.module('utils/deprecate', {
  beforeEach() {

    // Back up the original console.
    this.originalConsole = window.console;

    // Replace the native console for testing. In IE8 `console.log` is not a
    // 'function' so sinon chokes on it when trying to spy:
    //   https://github.com/cjohansen/Sinon.JS/issues/386
    //
    // Instead we'll temporarily replace them with no-op functions
    window.console = {
      debug: sinon.spy(),
      info: sinon.spy(),
      log: sinon.spy(),
      warn: sinon.spy(),
      error: sinon.spy()
    };
  },
  afterEach() {

    // Restore the native/original console.
    window.console = this.originalConsole;
  }
}, function() {

  QUnit.module('deprecate');

  QUnit.test('should pass through arguments to a function and return expected value', function(assert) {
    assert.expect(1);

    const test = deprecate('test', (...args) => args);
    const result = test(1, 2, 3);

    assert.deepEqual(result, [1, 2, 3]);
  });

  QUnit.test('should maintain the correct "this" value', function(assert) {
    assert.expect(1);

    const that = {};
    const test = deprecate('test', function() {
      return this;
    }.bind(that));

    const result = test();

    assert.strictEqual(result, that, 'the "this" value was returned as the expected object');
  });

  QUnit.test('should log the specified message only once', function(assert) {
    assert.expect(2);

    const test = deprecate('test', function() {});

    test();
    test();
    test();

    assert.ok(window.console.warn.calledOnce);
    assert.deepEqual(window.console.warn.firstCall.args, ['VIDEOJS:', 'WARN:', 'test']);
  });

  QUnit.module('deprecateForMajor');

  QUnit.test('should pass through arguments to a function and return expected value', function(assert) {
    assert.expect(1);

    const test = deprecateForMajor(1, 'A', 'B', (...args) => args);
    const result = test(1, 2, 3);

    assert.deepEqual(result, [1, 2, 3]);
  });

  QUnit.test('should maintain the correct "this" value', function(assert) {
    assert.expect(1);

    const that = {};
    const test = deprecateForMajor(1, 'A', 'B', function() {
      return this;
    }.bind(that));

    const result = test();

    assert.strictEqual(result, that, 'the "this" value was returned as the expected object');
  });

  QUnit.test('should log the expected message only once', function(assert) {
    assert.expect(2);

    const test = deprecateForMajor(1, 'A', 'B', function() {});

    test();
    test();
    test();

    assert.ok(window.console.warn.calledOnce);
    assert.deepEqual(window.console.warn.firstCall.args, ['VIDEOJS:', 'WARN:', 'A is deprecated and will be removed in 1.0; please use B instead.']);
  });
});