Skip to content

Commit

Permalink
@gkatsev cleared vttjs script handlers on dispose. Fixed tests. closes
Browse files Browse the repository at this point in the history
  • Loading branch information
gkatsev committed Mar 17, 2016
1 parent 94e899f commit dbdc411
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ CHANGELOG
=========

## HEAD (Unreleased)
_(none)_
* @gkatsev cleared vttjs script handlers on dispose. Fixed tests ([view](https://github.com/videojs/video.js/pull/3189))

--------------------

Expand Down
4 changes: 4 additions & 0 deletions src/js/tech/tech.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ class Tech extends Component {
script.onerror = () => {
this.trigger('vttjserror');
};
this.on('dispose', () => {
script.onload = null;
script.onerror = null;
});
this.el().parentNode.appendChild(script);
window['WebVTT'] = true;
}
Expand Down
1 change: 1 addition & 0 deletions test/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = function(config) {

browserify: {
debug: true,
plugin: ['proxyquireify/plugin'],
transform: [
require('babelify').configure({
sourceMapRelative: './',
Expand Down
53 changes: 38 additions & 15 deletions test/unit/tracks/text-track.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import EventTarget from '../../../src/js/event-target.js';
import TextTrack from '../../../src/js/tracks/text-track.js';
import TestHelpers from '../test-helpers.js';
import log from '../../../src/js/utils/log.js';
import proxyquireify from 'proxyquireify';

const proxyquire = proxyquireify(require);

const defaultTech = {
textTracks() {},
Expand Down Expand Up @@ -276,31 +279,42 @@ test('tracks are parsed if vttjs is loaded', function() {
};
};

let xhr;
window.xhr.onCreate = (newXhr) => xhr = newXhr;
// use proxyquire to stub xhr module because IE8s XDomainRequest usage
let xhrHandler;
let TextTrack = proxyquire('../../../src/js/tracks/text-track.js', {
xhr(options, fn) {
xhrHandler = fn;
}
});

let tt = new TextTrack({
tech: defaultTech,
src: 'http://example.com'
});

xhr.respond(200, {}, 'WebVTT\n');
xhrHandler(null, {}, 'WEBVTT\n');

ok(parserCreated, 'WebVTT is loaded, so we can just parse');

clock.restore();
window.WebVTT = oldVTT;
TextTrack = proxyquire('../../../src/js/tracks/text-track.js', {});
});

test('tracks are parsed once vttjs is loaded', function() {
const clock = sinon.useFakeTimers();
const oldVTT = window.WebVTT;
let parserCreated = false;

window.WebVTT = true;
// use proxyquire to stub xhr module because IE8s XDomainRequest usage
let xhrHandler;
let TextTrack = proxyquire('../../../src/js/tracks/text-track.js', {
xhr(options, fn) {
xhrHandler = fn;
}
});

let xhr;
window.xhr.onCreate = (newXhr) => xhr = newXhr;
window.WebVTT = true;

let testTech = new EventTarget();
testTech.textTracks = () => {};
Expand All @@ -311,7 +325,7 @@ test('tracks are parsed once vttjs is loaded', function() {
src: 'http://example.com'
});

xhr.respond(200, {}, 'WebVTT\n');
xhrHandler(null, {}, 'WEBVTT\n');

ok(!parserCreated, 'WebVTT is not loaded, do not try to parse yet');

Expand All @@ -336,18 +350,28 @@ test('tracks are parsed once vttjs is loaded', function() {

clock.restore();
window.WebVTT = oldVTT;
TextTrack = proxyquire('../../../src/js/tracks/text-track.js', {});
});

test('stops processing if vttjs loading errored out', function() {
const clock = sinon.useFakeTimers();
sinon.stub(log, 'error');
const oldVTT = window.WebVTT;
let parserCreated = false;

window.WebVTT = true;

let xhr;
window.xhr.onCreate = (newXhr) => xhr = newXhr;
// use proxyquire to stub xhr module because IE8s XDomainRequest usage
let xhrHandler;
let errorMsg;
let TextTrack = proxyquire('../../../src/js/tracks/text-track.js', {
xhr(options, fn) {
xhrHandler = fn;
},
'../utils/log.js': {
error(msg) {
errorMsg = msg;
}
}
});

let testTech = new EventTarget();
testTech.textTracks = () => {};
Expand All @@ -361,20 +385,19 @@ test('stops processing if vttjs loading errored out', function() {
src: 'http://example.com'
});

xhr.respond(200, {}, 'WebVTT\n');
xhrHandler(null, {}, 'WEBVTT\n');

ok(!parserCreated, 'WebVTT is not loaded, do not try to parse yet');

testTech.trigger('vttjserror');
let errorSpyCall = log.error.getCall(0);
let offSpyCall = testTech.off.getCall(0);

ok(errorSpyCall.calledWithMatch('vttjs failed to load, stopping trying to process'),
ok(/^vttjs failed to load, stopping trying to process/.test(errorMsg),
'vttjs failed to load, so, we logged an error');
ok(!parserCreated, 'WebVTT is not loaded, do not try to parse yet');
ok(offSpyCall, 'tech.off was called');

clock.restore();
log.error.restore();
window.WebVTT = oldVTT;
TextTrack = proxyquire('../../../src/js/tracks/text-track.js', {});
});

0 comments on commit dbdc411

Please sign in to comment.