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 RTD Module : fallback to lone player on page #11186

Merged
merged 4 commits into from
Jul 3, 2024
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
32 changes: 26 additions & 6 deletions modules/jwplayerRtdProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import {submodule} from '../src/hook.js';
import {config} from '../src/config.js';
import {ajaxBuilder} from '../src/ajax.js';
import {deepAccess, logError} from '../src/utils.js';
import { deepAccess, logError, logWarn } from '../src/utils.js'
import {find} from '../src/polyfill.js';
import {getGlobal} from '../src/prebidGlobal.js';

Expand Down Expand Up @@ -429,17 +429,37 @@ export function addTargetingToBid(bid, targeting) {
bid.rtd = Object.assign({}, rtd, jwRtd);
}

function getPlayer(playerDivId) {
export function getPlayer(playerDivId) {
const jwplayer = window.jwplayer;
if (!jwplayer) {
logError(SUBMODULE_NAME + '.js was not found on page');
return;
}

const player = jwplayer(playerDivId);
if (!player || !player.getPlaylist) {
logError('player ID did not match any players');
let player = jwplayer(playerDivId);
if (player && player.getPlaylist) {
return player;
}

const playerOnPageCount = document.getElementsByClassName('jwplayer').length;
if (playerOnPageCount === 0) {
logError('No JWPlayer instances have been detected on the page');
return;
}
return player;

let errorMessage = `player Div ID ${playerDivId} did not match any players.`;

// If there are multiple instances on the page, we cannot guess which one should be targeted.
if (playerOnPageCount > 1) {
logError(errorMessage);
return;
}

player = jwplayer();
if (player && player.getPlaylist) {
logWarn(`${errorMessage} Targeting player Div ID ${player.id} instead`);
return player;
}

logError(errorMessage);
}
61 changes: 61 additions & 0 deletions test/spec/modules/jwplayerRtdProvider_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getVatFromCache,
getVatFromPlayer,
setOverrides,
getPlayer,
jwplayerSubmodule
} from 'modules/jwplayerRtdProvider.js';
import {server} from 'test/mocks/xhr.js';
Expand Down Expand Up @@ -1603,6 +1604,66 @@ describe('jwplayerRtdProvider', function() {
});
});

describe('Player detection', function () {
const playerInstanceMock = {
getPlaylist: () => [],
getPlaylistItem: () => ({})
};

beforeEach(function () {
window.jwplayer = sinon.stub();
});

afterEach(function () {
delete window.jwplayer;
});

it('should fail if jwplayer global does not exist', function () {
delete window.jwplayer;
expect(getPlayer('divId')).to.be.undefined;
});

it('should return the player instance for the specified div id', function () {
window.jwplayer.returns(playerInstanceMock);
const player = getPlayer('divId');
expect(player).to.deep.equal(playerInstanceMock);
});

it('should request a player when the div id does not match a player on the page and only 1 player is in the DOM', function () {
const playerDomElement = document.createElement('div');
playerDomElement.className = 'jwplayer';
document.body.appendChild(playerDomElement);

window.jwplayer.withArgs('invalidDivId').returns(undefined);
window.jwplayer.returns(playerInstanceMock);

const playerInstance = getPlayer('invalidDivId');

expect(playerInstance).to.deep.equal(playerInstanceMock);

document.body.removeChild(playerDomElement);
});

it('should fail when the div id does not match a player on the page, and multiple players are instantiated', function () {
const firstPlayerDomElement = document.createElement('div');
const secondPlayerDomElement = document.createElement('div');
firstPlayerDomElement.className = 'jwplayer';
secondPlayerDomElement.className = 'jwplayer';
document.body.appendChild(firstPlayerDomElement);
document.body.appendChild(secondPlayerDomElement);

window.jwplayer.withArgs('invalidDivId').returns(undefined);
window.jwplayer.returns(playerInstanceMock);

const playerInstance = getPlayer('invalidDivId');

expect(playerInstance).to.be.undefined;

document.body.removeChild(firstPlayerDomElement);
document.body.removeChild(secondPlayerDomElement);
});
});

describe('jwplayerSubmodule', function () {
it('successfully instantiates', function () {
expect(jwplayerSubmodule.init()).to.equal(true);
Expand Down