summaryrefslogtreecommitdiff
path: root/javascript/videojs/test/unit/tracks/text-track-cue-list.test.js
blob: e62142ddee781426e54bf7d5595695d111352d3b (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
/* eslint-env qunit */
import TextTrackCueList from '../../../src/js/tracks/text-track-cue-list.js';

const genericTracks = [
  {
    id: '1'
  }, {
    id: '2'
  }, {
    id: '3'
  }
];

QUnit.module('Text Track Cue List');

QUnit.test('TextTrackCueList\'s length is set correctly', function(assert) {
  const ttcl = new TextTrackCueList(genericTracks);

  assert.equal(ttcl.length, genericTracks.length, 'the length is ' + genericTracks.length);
});

QUnit.test('can get cues by id', function(assert) {
  const ttcl = new TextTrackCueList(genericTracks);

  assert.equal(ttcl.getCueById('1').id, 1, 'id "1" has id of "1"');
  assert.equal(ttcl.getCueById('2').id, 2, 'id "2" has id of "2"');
  assert.equal(ttcl.getCueById('3').id, 3, 'id "3" has id of "3"');
  assert.ok(!ttcl.getCueById(1), 'there isn\'t an item with "numeric" id of `1`');
});

QUnit.test('length is updated when new tracks are added or removed', function(assert) {
  const ttcl = new TextTrackCueList(genericTracks);

  ttcl.setCues_(genericTracks.concat([{id: '100'}]));
  assert.equal(
    ttcl.length, genericTracks.length + 1,
    'the length is ' + (genericTracks.length + 1)
  );
  ttcl.setCues_(genericTracks.concat([{id: '100'}, {id: '101'}]));
  assert.equal(
    ttcl.length, genericTracks.length + 2,
    'the length is ' + (genericTracks.length + 2)
  );

  ttcl.setCues_(genericTracks.concat([{id: '100'}]));
  assert.equal(
    ttcl.length, genericTracks.length + 1,
    'the length is ' + (genericTracks.length + 1)
  );
  ttcl.setCues_(genericTracks);
  assert.equal(ttcl.length, genericTracks.length, 'the length is ' + genericTracks.length);
});

QUnit.test('can access items by index', function(assert) {
  const ttcl = new TextTrackCueList(genericTracks);
  const length = ttcl.length;

  assert.expect(length);

  for (let i = 0; i < length; i++) {
    assert.equal(ttcl[i].id, String(i + 1), 'the id of a track matches the index + 1');
  }
});

QUnit.test('can access new items by index', function(assert) {
  const ttcl = new TextTrackCueList(genericTracks);

  ttcl.setCues_(genericTracks.concat([{id: '100'}]));

  assert.equal(ttcl[3].id, '100', 'id of item at index 3 is 100');
  ttcl.setCues_(genericTracks.concat([{id: '100'}, {id: '101'}]));
  assert.equal(ttcl[4].id, '101', 'id of item at index 4 is 101');
});

QUnit.test('cannot access removed items by index', function(assert) {
  const ttcl = new TextTrackCueList(genericTracks);

  ttcl.setCues_(genericTracks.concat([{id: '100'}, {id: '101'}]));
  assert.equal(ttcl[3].id, '100', 'id of item at index 3 is 100');
  assert.equal(ttcl[4].id, '101', 'id of item at index 4 is 101');

  ttcl.setCues_(genericTracks);

  assert.ok(!ttcl[3], 'nothing at index 3');
  assert.ok(!ttcl[4], 'nothing at index 4');
});

QUnit.test('new item available at old index', function(assert) {
  const ttcl = new TextTrackCueList(genericTracks);

  ttcl.setCues_(genericTracks.concat([{id: '100'}]));
  assert.equal(ttcl[3].id, '100', 'id of item at index 3 is 100');

  ttcl.setCues_(genericTracks);
  assert.ok(!ttcl[3], 'nothing at index 3');

  ttcl.setCues_(genericTracks.concat([{id: '101'}]));
  assert.equal(ttcl[3].id, '101', 'id of new item at index 3 is now 101');
});