1
0
mirror of https://github.com/videojs/video.js.git synced 2025-01-06 06:50:51 +02:00
video.js/test/unit/event-target.test.js
Gary Katsevman 8c92cbfb3e
feat: async change events in TextTrackList with EventTarget#queueTrigger (#5332)
Trigger the change event on the next tick. This means that multiple changes to a track's mode will only result in a single change event on its associated TextTrackList rather than 3 events as it may be currently.

Fixes #5159
2018-07-25 17:33:30 -04:00

52 lines
1.3 KiB
JavaScript

/* eslint-env qunit */
import EventTarget from '../../src/js/event-target.js';
import sinon from 'sinon';
const { test } = QUnit;
QUnit.module('EventTarget', {
beforeEach() {
this.clock = sinon.useFakeTimers();
},
afterEach() {
this.clock.restore();
}
});
test('EventTarget queueTrigger queues the event', function(t) {
const et = new EventTarget();
let changes = 0;
const changeHandler = function() {
changes++;
};
et.on('change', changeHandler);
et.queueTrigger('change');
t.equal(changes, 0, 'EventTarget did not trigger a change event yet');
this.clock.tick(1);
t.equal(changes, 1, 'EventTarget triggered a change event once the clock ticked');
});
test('EventTarget will only trigger event once with queueTrigger', function(t) {
const et = new EventTarget();
let changes = 0;
const changeHandler = function() {
changes++;
};
et.on('change', changeHandler);
et.queueTrigger('change');
t.equal(changes, 0, 'EventTarget did not trigger a change event yet');
et.queueTrigger('change');
t.equal(changes, 0, 'EventTarget did not trigger a change event yet');
et.queueTrigger('change');
t.equal(changes, 0, 'EventTarget did not trigger a change event yet');
et.queueTrigger('change');
this.clock.tick(100);
t.equal(changes, 1, 'EventTarget *only* triggered a change event once');
});