playable
Version:
Video player based on HTML5Video
250 lines • 13.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
require("jsdom-global/register");
var chai_1 = require("chai");
var sinon = (0, tslib_1.__importStar)(require("sinon"));
var testkit_1 = (0, tslib_1.__importDefault)(require("../../../../testkit"));
var progress_1 = (0, tslib_1.__importStar)(require("./progress"));
var constants_1 = require("../../../../constants");
describe('ProgressControl', function () {
var testkit;
var control;
var engine;
var eventEmitter;
beforeEach(function () {
testkit = (0, testkit_1.default)();
testkit.registerModule('progressControl', progress_1.default);
control = testkit.getModule('progressControl');
eventEmitter = testkit.getModule('eventEmitter');
engine = testkit.getModule('engine');
});
describe('constructor', function () {
it('should create instance ', function () {
(0, chai_1.expect)(control).to.exist;
(0, chai_1.expect)(control.view).to.exist;
});
});
describe('API', function () {
it('should have method for showing whole view', function () {
(0, chai_1.expect)(control.show).to.exist;
control.show();
(0, chai_1.expect)(control.isHidden).to.be.false;
});
it('should have method for hiding whole view', function () {
(0, chai_1.expect)(control.hide).to.exist;
control.hide();
(0, chai_1.expect)(control.isHidden).to.be.true;
});
it('should have method for destroying', function () {
var spy = sinon.spy(control, '_unbindEvents');
(0, chai_1.expect)(control.destroy).to.exist;
control.destroy();
(0, chai_1.expect)(spy.called).to.be.true;
});
describe('for time indicators', function () {
var VIDEO_DURATION_TIME = 1000;
var engineGetDurationTimeStub;
beforeEach(function () {
engineGetDurationTimeStub = sinon.stub(control._engine, 'getDuration').callsFake(function () { return VIDEO_DURATION_TIME; });
});
afterEach(function () {
engineGetDurationTimeStub.restore();
});
it('should have methods for adding/deleting indicators', function () {
(0, chai_1.expect)(control.addTimeIndicator, 'addTimeIndicator').to.exist;
(0, chai_1.expect)(control.addTimeIndicators, 'addTimeIndicators').to.exist;
(0, chai_1.expect)(control.clearTimeIndicators, 'clearTimeIndicators').to.exist;
});
describe('before `METADATA_LOADED`', function () {
beforeEach(function () {
control.clearTimeIndicators();
});
it('should add one indicator', function () {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
var timeIndicatorsNode;
return (0, tslib_1.__generator)(this, function (_a) {
switch (_a.label) {
case 0:
timeIndicatorsNode = control.view._$timeIndicators;
control.addTimeIndicator(100);
(0, chai_1.expect)(control._engine.isMetadataLoaded, '`isMetadataLoaded` before add').to.equal(false);
(0, chai_1.expect)(timeIndicatorsNode.childNodes.length, 'indicator added before `METADATA_LOADED`').to.equal(0);
return [4 /*yield*/, eventEmitter.emitAsync(constants_1.VideoEvent.STATE_CHANGED, {
nextState: constants_1.EngineState.METADATA_LOADED,
})];
case 1:
_a.sent();
(0, chai_1.expect)(timeIndicatorsNode.childNodes.length, 'indicator added after `METADATA_LOADED`').to.equal(1);
return [2 /*return*/];
}
});
});
});
it('should add multiple indicators', function () {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
var timeIndicatorsNode;
return (0, tslib_1.__generator)(this, function (_a) {
switch (_a.label) {
case 0:
timeIndicatorsNode = control.view._$timeIndicators;
control.addTimeIndicators([100, 200, 300]);
(0, chai_1.expect)(control._engine.isMetadataLoaded, '`isMetadataLoaded` before add').to.equal(false);
(0, chai_1.expect)(timeIndicatorsNode.childNodes.length, 'indicator added before `METADATA_LOADED`').to.equal(0);
return [4 /*yield*/, eventEmitter.emitAsync(constants_1.VideoEvent.STATE_CHANGED, {
nextState: constants_1.EngineState.METADATA_LOADED,
})];
case 1:
_a.sent();
(0, chai_1.expect)(timeIndicatorsNode.childNodes.length, 'indicators added after `METADATA_LOADED`').to.equal(3);
return [2 /*return*/];
}
});
});
});
});
describe('after `METADATA_LOADED`', function () {
beforeEach(function () {
control.clearTimeIndicators();
Reflect.defineProperty(control._engine, 'isMetadataLoaded', (0, tslib_1.__assign)((0, tslib_1.__assign)({}, Reflect.getOwnPropertyDescriptor(engine.constructor.prototype, 'isMetadataLoaded')), { get: function () { return true; } }));
});
afterEach(function () {
Reflect.deleteProperty(engine, 'isMetadataLoaded');
});
it('should add one indicator', function () {
var timeIndicatorsNode = control.view._$timeIndicators;
(0, chai_1.expect)(timeIndicatorsNode.childNodes.length, 'empty before add').to.equal(0);
control.addTimeIndicator(100);
(0, chai_1.expect)(timeIndicatorsNode.childNodes.length, 'indicators added').to.equal(1);
});
it('should add multiple indicator', function () {
var timeIndicatorsNode = control.view._$timeIndicators;
(0, chai_1.expect)(timeIndicatorsNode.childNodes.length, 'empty before add').to.equal(0);
control.addTimeIndicators([100, 200, 300]);
(0, chai_1.expect)(timeIndicatorsNode.childNodes.length, 'indicators added').to.equal(3);
});
it('should ignore time more then video duration time', function () {
var timeIndicatorsNode = control.view._$timeIndicators;
(0, chai_1.expect)(timeIndicatorsNode.childNodes.length, 'empty before add').to.equal(0);
control.addTimeIndicator(VIDEO_DURATION_TIME + 1);
(0, chai_1.expect)(timeIndicatorsNode.childNodes.length, 'indicators added').to.equal(0);
});
it('should delete all added indicators', function () {
var timeIndicatorsNode = control.view._$timeIndicators;
control.addTimeIndicators([100, 200, 300]);
(0, chai_1.expect)(timeIndicatorsNode.childNodes.length, 'indicators added').to.equal(3);
control.clearTimeIndicators();
(0, chai_1.expect)(timeIndicatorsNode.childNodes.length, 'indicators after clear').to.equal(0);
});
});
});
});
describe('video events listeners', function () {
it('should call callback on playback state change', function () {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
var spy;
return (0, tslib_1.__generator)(this, function (_a) {
switch (_a.label) {
case 0:
spy = sinon.spy(control, '_processStateChange');
control._bindEvents();
return [4 /*yield*/, eventEmitter.emitAsync(constants_1.VideoEvent.STATE_CHANGED, {})];
case 1:
_a.sent();
(0, chai_1.expect)(spy.called).to.be.true;
return [2 /*return*/];
}
});
});
});
it('should call callback on seek', function () {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
var spyPlayed, spyBuffered;
return (0, tslib_1.__generator)(this, function (_a) {
switch (_a.label) {
case 0:
spyPlayed = sinon.spy(control, '_updatePlayedIndicator');
spyBuffered = sinon.spy(control, '_updateBufferIndicator');
control._bindEvents();
return [4 /*yield*/, eventEmitter.emitAsync(constants_1.VideoEvent.STATE_CHANGED, {
nextState: constants_1.EngineState.SEEK_IN_PROGRESS,
})];
case 1:
_a.sent();
(0, chai_1.expect)(spyPlayed.called).to.be.true;
(0, chai_1.expect)(spyBuffered.called).to.be.true;
return [2 /*return*/];
}
});
});
});
it('should call callback on duration update', function () {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
var spy;
return (0, tslib_1.__generator)(this, function (_a) {
switch (_a.label) {
case 0:
spy = sinon.spy(control, '_updateBufferIndicator');
control._bindEvents();
return [4 /*yield*/, eventEmitter.emitAsync(constants_1.VideoEvent.CHUNK_LOADED)];
case 1:
_a.sent();
(0, chai_1.expect)(spy.called).to.be.true;
return [2 /*return*/];
}
});
});
});
});
describe('internal methods', function () {
it('should toggle playback on manipulation change', function () {
var startSpy = sinon.spy(control, '_pauseVideoOnDragStart');
var stopSpy = sinon.spy(control, '_playVideoOnDragEnd');
control._startProcessingUserDrag();
(0, chai_1.expect)(startSpy.called).to.be.true;
control._stopProcessingUserDrag();
(0, chai_1.expect)(stopSpy.called).to.be.true;
startSpy.restore();
stopSpy.restore();
});
it('should toggle interval updates', function () {
var startSpy = sinon.spy(control, '_startIntervalUpdates');
control._processStateChange({ nextState: constants_1.EngineState.PLAYING });
(0, chai_1.expect)(startSpy.called).to.be.true;
var stopSpy = sinon.spy(control, '_stopIntervalUpdates');
control._processStateChange({ nextState: constants_1.EngineState.PAUSED });
(0, chai_1.expect)(stopSpy.called).to.be.true;
});
it('should start interval updates', function () {
var spy = sinon.spy(window, 'setInterval');
var stopSpy = sinon.spy(control, '_stopIntervalUpdates');
control._startIntervalUpdates();
(0, chai_1.expect)(spy.calledWith(control._updateAllIndicators, progress_1.UPDATE_PROGRESS_INTERVAL_DELAY)).to.be.true;
(0, chai_1.expect)(stopSpy.called).to.be.false;
control._startIntervalUpdates();
(0, chai_1.expect)(stopSpy.called).to.be.true;
spy.restore();
});
it('should change current time of video', function () {
var spy = sinon.stub(engine, 'seekTo');
control._onChangePlayedPercent(10);
(0, chai_1.expect)(spy.called).to.be.true;
});
it('should update view', function () {
var playedSpy = sinon.spy(control, '_setPlayed');
var bufferSpy = sinon.spy(control, '_setBuffered');
control._updatePlayedIndicator();
(0, chai_1.expect)(playedSpy.called).to.be.true;
control._updateBufferIndicator();
(0, chai_1.expect)(bufferSpy.called).to.be.true;
});
it('should trigger update of both played and buffered', function () {
var playedSpy = sinon.spy(control, '_updatePlayedIndicator');
var bufferSpy = sinon.spy(control, '_updateBufferIndicator');
control._updateAllIndicators();
(0, chai_1.expect)(playedSpy.called).to.be.true;
(0, chai_1.expect)(bufferSpy.called).to.be.true;
});
});
});
//# sourceMappingURL=progress.spec.js.map