Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JW Player Video Module: trigger error when missing div id #9407

Merged
merged 2 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions modules/jwplayerVideoProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,26 @@ export function JWPlayerProvider(config, jwplayer_, adState_, timeState_, callba

function init() {
if (!jwplayer) {
triggerSetupFailure(-1); // TODO: come up with code for player absent
triggerSetupFailure(-1); // TODO: come up with error code schema- player is absent
return;
}

playerVersion = jwplayer.version;

if (playerVersion < minimumSupportedPlayerVersion) {
triggerSetupFailure(-2); // TODO: come up with code for version not supported
triggerSetupFailure(-2); // TODO: come up with error code schema - version not supported
return;
}

if (!document.getElementById(divId)) {
triggerSetupFailure(-3); // TODO: come up with error code schema - missing div id
return;
}

player = jwplayer(divId);
if (player.getState() === undefined) {
if (!player || !player.getState) {
triggerSetupFailure(-4); // TODO: come up with error code schema - factory function failure
} else if (player.getState() === undefined) {
setupPlayer(playerConfig);
} else {
const payload = getSetupCompletePayload();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,28 @@ function getUtilsMock() {

const sharedUtils = { videoEvents };

function addDiv() {
const div = document.createElement('div');
div.setAttribute('id', 'test');
document.body.appendChild(div);
}

function removeDiv() {
const div = document.getElementById('test');
if (div) {
div.remove();
}
}

describe('JWPlayerProvider', function () {
beforeEach(() => {
addDiv();
});

afterEach(() => {
removeDiv();
});

describe('init', function () {
let config;
let adState;
Expand All @@ -75,7 +96,7 @@ describe('JWPlayerProvider', function () {
let utilsMock;

beforeEach(() => {
config = {};
config = { divId: 'test' };
adState = adStateFactory();
timeState = timeStateFactory();
callbackStorage = callbackStorageFactory();
Expand Down Expand Up @@ -104,7 +125,21 @@ describe('JWPlayerProvider', function () {
expect(payload.errorCode).to.be.equal(-2);
});

it('should instantiate the player when uninstantied', function () {
it('should trigger failure when div is missing', function () {
removeDiv();
let jwplayerMock = () => {};
const provider = JWPlayerProvider(config, jwplayerMock, adState, timeState, callbackStorage, utilsMock, sharedUtils);
const setupFailed = sinon.spy();
provider.onEvent(SETUP_FAILED, setupFailed, {});
provider.init();
expect(setupFailed.calledOnce).to.be.true;
const payload = setupFailed.args[0][1];
expect(payload.errorCode).to.be.equal(-3);
addDiv();
addDiv();
});

it('should instantiate the player when uninstantiated', function () {
const player = getPlayerMock();
config.playerConfig = {};
const setupSpy = player.setup = sinon.spy();
Expand All @@ -113,7 +148,7 @@ describe('JWPlayerProvider', function () {
expect(setupSpy.calledOnce).to.be.true;
});

it('should trigger setup complete when player is already instantied', function () {
it('should trigger setup complete when player is already instantiated', function () {
const player = getPlayerMock();
player.getState = () => 'idle';
const provider = JWPlayerProvider(config, makePlayerFactoryMock(player), adState, timeState, callbackStorage, utilsMock, sharedUtils);
Expand Down Expand Up @@ -151,7 +186,7 @@ describe('JWPlayerProvider', function () {
const test_playback_method = PLAYBACK_METHODS.CLICK_TO_PLAY;
const test_skip = 0;

const config = {};
const config = { divId: 'test' };
const player = getPlayerMock();
const utils = getUtilsMock();

Expand Down Expand Up @@ -230,9 +265,9 @@ describe('JWPlayerProvider', function () {
duration: test_duration,
playbackMode: test_playback_mode
})
}
};

const provider = JWPlayerProvider({}, makePlayerFactoryMock(player), adStateFactory(), timeState, {}, utils, sharedUtils);
const provider = JWPlayerProvider({ divId: 'test' }, makePlayerFactoryMock(player), adStateFactory(), timeState, {}, utils, sharedUtils);
provider.init();

let content = provider.getOrtbContent();
Expand Down Expand Up @@ -260,7 +295,7 @@ describe('JWPlayerProvider', function () {
it('should call playAd', function () {
const player = getPlayerMock();
const playAdSpy = player.playAd = sinon.spy();
const provider = JWPlayerProvider({}, makePlayerFactoryMock(player), {}, {}, {}, {}, sharedUtils);
const provider = JWPlayerProvider({ divId: 'test' }, makePlayerFactoryMock(player), {}, {}, {}, {}, sharedUtils);
provider.init();
provider.setAdTagUrl('tag');
expect(playAdSpy.called).to.be.true;
Expand All @@ -273,7 +308,7 @@ describe('JWPlayerProvider', function () {
it('should register event listener on player', function () {
const player = getPlayerMock();
const onSpy = player.on = sinon.spy();
const provider = JWPlayerProvider({}, makePlayerFactoryMock(player), adStateFactory(), timeStateFactory(), callbackStorageFactory(), getUtilsMock(), sharedUtils);
const provider = JWPlayerProvider({ divId: 'test' }, makePlayerFactoryMock(player), adStateFactory(), timeStateFactory(), callbackStorageFactory(), getUtilsMock(), sharedUtils);
provider.init();
const callback = () => {};
provider.onEvent(PLAY, callback, {});
Expand All @@ -285,7 +320,7 @@ describe('JWPlayerProvider', function () {
it('should remove event listener on player', function () {
const player = getPlayerMock();
const offSpy = player.off = sinon.spy();
const provider = JWPlayerProvider({}, makePlayerFactoryMock(player), adStateFactory(), timeStateFactory(), callbackStorageFactory(), utils, sharedUtils);
const provider = JWPlayerProvider({ divId: 'test' }, makePlayerFactoryMock(player), adStateFactory(), timeStateFactory(), callbackStorageFactory(), utils, sharedUtils);
provider.init();
const callback = () => {};
provider.onEvent(AD_IMPRESSION, callback, {});
Expand All @@ -301,7 +336,7 @@ describe('JWPlayerProvider', function () {
const player = getPlayerMock();
const removeSpy = player.remove = sinon.spy();
player.remove = removeSpy;
const provider = JWPlayerProvider({}, makePlayerFactoryMock(player), adStateFactory(), timeStateFactory(), callbackStorageFactory(), getUtilsMock(), sharedUtils);
const provider = JWPlayerProvider({ divId: 'test' }, makePlayerFactoryMock(player), adStateFactory(), timeStateFactory(), callbackStorageFactory(), getUtilsMock(), sharedUtils);
provider.init();
provider.destroy();
provider.destroy();
Expand Down