Skip to content

Commit

Permalink
updates tests
Browse files Browse the repository at this point in the history
  • Loading branch information
karimMourra committed Dec 6, 2022
1 parent b706c3b commit f5fb7b6
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 152 deletions.
2 changes: 1 addition & 1 deletion modules/jwplayerVideoProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
AUTOSTART_BLOCKED, PLAY_ATTEMPT_FAILED, CONTENT_LOADED, PLAY, PAUSE, BUFFER, TIME, SEEK_START, SEEK_END, MUTE, VOLUME,
RENDITION_UPDATE, ERROR, COMPLETE, PLAYLIST_COMPLETE, FULLSCREEN, PLAYER_RESIZE, VIEWABLE, CAST
} from '../libraries/video/constants/events.js';
import { PLAYBACK_MODE } from '../libraries/video/constants/enums.js';
import { PLAYBACK_MODE } from '../libraries/video/constants/constants.js';
import stateFactory from '../libraries/video/shared/state.js';
import { JWPLAYER_VENDOR } from '../libraries/video/constants/vendorCodes.js';
import { getEventHandler } from '../libraries/video/shared/eventHandler.js';
Expand Down
2 changes: 1 addition & 1 deletion modules/videojsVideoProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { VIDEO_JS_VENDOR } from '../libraries/video/constants/vendorCodes.js';
import { submodule } from '../src/hook.js';
import stateFactory from '../libraries/video/shared/state.js';
import { PLAYBACK_MODE } from '../libraries/video/constants/enums.js';
import { PLAYBACK_MODE } from '../libraries/video/constants/constants.js';
import { getEventHandler } from '../libraries/video/shared/eventHandler.js';

/*
Expand Down
193 changes: 115 additions & 78 deletions test/spec/modules/videoModule/adQueue_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,124 +12,161 @@ describe('Ad Queue Coordinator', function () {
}
};

describe('Requires Queuing', function () {
it('should be true when provider is not setup', function() {
const coordinator = AdQueueCoordinator(mockVideoCoreFactory());
const mockEventsFactory = function () {
return {
emit: sinon.spy()
};
};

describe('Before Provider Setup Complete', function () {
it('should push ad to queue', function () {
const mockVideoCore = mockVideoCoreFactory();
const mockEvents = mockEventsFactory();
const coordinator = AdQueueCoordinator(mockVideoCore, mockEvents);
coordinator.registerProvider(testId);
expect(coordinator.requiresQueueing(testId)).to.be.true;
coordinator.queueAd('testAdTag', testId, { param: {} });

expect(mockEvents.emit.calledOnce).to.be.true;
let emitArgs = mockEvents.emit.firstCall.args;
expect(emitArgs[0]).to.be.equal('videoAuctionAdLoadQueued');
expect(mockVideoCore.setAdTagUrl.called).to.be.false;
});
});

it('should be false when provider is setup', function() {
describe('After Provider Setup Complete', function () {
it('should load from ad queue', function () {
const mockVideoCore = mockVideoCoreFactory();
const mockEvents = mockEventsFactory();
let setupComplete;
mockVideoCore.onEvents = function(events, callback, id) {
if (events[0] === SETUP_COMPLETE && id === testId) {
setupComplete = callback;
}
};
const coordinator = AdQueueCoordinator(mockVideoCore);
const coordinator = AdQueueCoordinator(mockVideoCore, mockEvents);
coordinator.registerProvider(testId);
setupComplete('', { divId: testId });
coordinator.queueAd('testAdTag', testId, { param: {} });

expect(mockEvents.emit.calledOnce).to.be.true;
let emitArgs = mockEvents.emit.firstCall.args;
expect(emitArgs[0]).to.be.equal('videoAuctionAdLoadQueued');

expect(coordinator.requiresQueueing(testId)).to.be.false;
expect(mockVideoCore.offEvents.calledTwice).to.be.true;
const offSetupCompleteArgs = mockVideoCore.offEvents.firstCall.args;
expect(offSetupCompleteArgs[0]).to.deep.equal([SETUP_COMPLETE]);
expect(offSetupCompleteArgs[2]).to.equal(testId);
setupComplete('', { divId: testId });
expect(mockEvents.emit.calledTwice).to.be.true;
emitArgs = mockEvents.emit.secondCall.args;
expect(emitArgs[0]).to.be.equal('videoAuctionAdLoadAttempt');
expect(mockVideoCore.setAdTagUrl.calledOnce).to.be.true;
});
});

describe('Queue Ad', function () {
it('should push ad to queue', function () {
it('should load ads without queueing', function () {
const mockVideoCore = mockVideoCoreFactory();
const mockEvents = mockEventsFactory();
let setupComplete;
mockVideoCore.onEvents = function(events, callback, id) {
if (events[0] === SETUP_COMPLETE && id === testId) {
setupComplete = callback;
}
};
const coordinator = AdQueueCoordinator(mockVideoCore);
const coordinator = AdQueueCoordinator(mockVideoCore, mockEvents);
coordinator.registerProvider(testId);
coordinator.queueAd('testAdTag', testId, { param: {} });

setupComplete('', { divId: testId });

coordinator.queueAd('testAdTag', testId, { param: {} });
expect(mockEvents.emit.calledOnce).to.be.true;
let emitArgs = mockEvents.emit.firstCall.args;
expect(emitArgs[0]).to.be.equal('videoAuctionAdLoadAttempt');
expect(mockVideoCore.setAdTagUrl.calledOnce).to.be.true;
});
});

it('should load from queue on Ad Break End', function () {
const mockVideoCore = mockVideoCoreFactory();
let setupComplete;
let adBreakEnd;
describe('On Ad Break End', function () {
it('should load from queue', function () {
const mockVideoCore = mockVideoCoreFactory();
const mockEvents = mockEventsFactory();
let setupComplete;
let adBreakEnd;

mockVideoCore.onEvents = function(events, callback, id) {
if (events[0] === SETUP_COMPLETE && id === testId) {
setupComplete = callback;
}
mockVideoCore.onEvents = function(events, callback, id) {
if (events[0] === SETUP_COMPLETE && id === testId) {
setupComplete = callback;
}

if (events[0] === AD_BREAK_END && id === testId) {
adBreakEnd = callback;
}
};
if (events[0] === AD_BREAK_END && id === testId) {
adBreakEnd = callback;
}
};

const coordinator = AdQueueCoordinator(mockVideoCore);
coordinator.registerProvider(testId);
coordinator.queueAd('testAdTag', testId);
coordinator.queueAd('testAdTag2', testId);
coordinator.queueAd('testAdTag3', testId);
const coordinator = AdQueueCoordinator(mockVideoCore, mockEvents);
coordinator.registerProvider(testId);
coordinator.queueAd('testAdTag', testId);
coordinator.queueAd('testAdTag2', testId);
coordinator.queueAd('testAdTag3', testId);

setupComplete('', { divId: testId });
mockEvents.emit.resetHistory();

expect(mockVideoCore.setAdTagUrl.calledOnce).to.be.true;
let setAdTagArgs = mockVideoCore.setAdTagUrl.firstCall.args;
expect(setAdTagArgs[0]).to.be.equal('testAdTag');
setupComplete('', { divId: testId });

adBreakEnd('', { divId: testId });
expect(mockEvents.emit.calledOnce).to.be.true;
let emitArgs = mockEvents.emit.firstCall.args;
expect(emitArgs[0]).to.be.equal('videoAuctionAdLoadAttempt');
expect(mockVideoCore.setAdTagUrl.calledOnce).to.be.true;
let setAdTagArgs = mockVideoCore.setAdTagUrl.firstCall.args;
expect(setAdTagArgs[0]).to.be.equal('testAdTag');

expect(mockVideoCore.setAdTagUrl.calledTwice).to.be.true;
setAdTagArgs = mockVideoCore.setAdTagUrl.secondCall.args;
expect(setAdTagArgs[0]).to.be.equal('testAdTag2');
adBreakEnd('', { divId: testId });

adBreakEnd('', { divId: testId });
expect(mockEvents.emit.calledTwice).to.be.true;
emitArgs = mockEvents.emit.secondCall.args;
expect(emitArgs[0]).to.be.equal('videoAuctionAdLoadAttempt');
expect(mockVideoCore.setAdTagUrl.calledTwice).to.be.true;
setAdTagArgs = mockVideoCore.setAdTagUrl.secondCall.args;
expect(setAdTagArgs[0]).to.be.equal('testAdTag2');

expect(mockVideoCore.setAdTagUrl.calledThrice).to.be.true;
setAdTagArgs = mockVideoCore.setAdTagUrl.thirdCall.args;
expect(setAdTagArgs[0]).to.be.equal('testAdTag3');
adBreakEnd('', { divId: testId });

adBreakEnd('', { divId: testId });
expect(mockVideoCore.setAdTagUrl.calledThrice).to.be.true;
});
expect(mockEvents.emit.calledThrice).to.be.true;
emitArgs = mockEvents.emit.thirdCall.args;
expect(emitArgs[0]).to.be.equal('videoAuctionAdLoadAttempt');
expect(mockVideoCore.setAdTagUrl.calledThrice).to.be.true;
setAdTagArgs = mockVideoCore.setAdTagUrl.thirdCall.args;
expect(setAdTagArgs[0]).to.be.equal('testAdTag3');

it('should empty queue as adBreaks end', function () {
const mockVideoCore = mockVideoCoreFactory();
let setupComplete;
let adBreakEnd;
adBreakEnd('', { divId: testId });

mockVideoCore.onEvents = function(events, callback, id) {
if (events[0] === SETUP_COMPLETE && id === testId) {
setupComplete = callback;
}
expect(mockEvents.emit.calledThrice).to.be.true;
expect(mockVideoCore.setAdTagUrl.calledThrice).to.be.true;
});

if (events[0] === AD_BREAK_END && id === testId) {
adBreakEnd = callback;
}
};
it('should stop responding to AdBreakEnd when queue is empty', function () {
const mockVideoCore = mockVideoCoreFactory();
let setupComplete;
let adBreakEnd;

const coordinator = AdQueueCoordinator(mockVideoCore);
coordinator.registerProvider(testId);
coordinator.queueAd('testAdTag', testId);
coordinator.queueAd('testAdTag2', testId);
coordinator.queueAd('testAdTag3', testId);

setupComplete('', { divId: testId });
adBreakEnd('', { divId: testId });
adBreakEnd('', { divId: testId });
expect(mockVideoCore.setAdTagUrl.calledThrice).to.be.true;
expect(coordinator.requiresQueueing(testId)).to.be.true;
adBreakEnd('', { divId: testId });
expect(mockVideoCore.setAdTagUrl.calledThrice).to.be.true;
expect(coordinator.requiresQueueing(testId)).to.be.false;
adBreakEnd('', { divId: testId });
expect(mockVideoCore.setAdTagUrl.calledThrice).to.be.true;
expect(coordinator.requiresQueueing(testId)).to.be.false;
mockVideoCore.onEvents = function(events, callback, id) {
if (events[0] === SETUP_COMPLETE && id === testId) {
setupComplete = callback;
}

if (events[0] === AD_BREAK_END && id === testId) {
adBreakEnd = callback;
}
};

const coordinator = AdQueueCoordinator(mockVideoCore, mockEventsFactory());
coordinator.registerProvider(testId);
coordinator.queueAd('testAdTag', testId);
coordinator.queueAd('testAdTag2', testId);
coordinator.queueAd('testAdTag3', testId);

setupComplete('', { divId: testId });
adBreakEnd('', { divId: testId });
adBreakEnd('', { divId: testId });
expect(mockVideoCore.setAdTagUrl.calledThrice).to.be.true;
adBreakEnd('', { divId: testId });
expect(mockVideoCore.setAdTagUrl.calledThrice).to.be.true;
adBreakEnd('', { divId: testId });
expect(mockVideoCore.setAdTagUrl.calledThrice).to.be.true;
});
});
});
80 changes: 9 additions & 71 deletions test/spec/modules/videoModule/pbVideo_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ function resetTestVars() {

adQueueCoordinatorMock = {
registerProvider: sinon.spy(),
requiresQueueing: sinon.spy(),
queueAd: sinon.spy()
};

Expand Down Expand Up @@ -262,10 +261,10 @@ describe('Prebid Video', function () {
pbVideoFactory(null, getConfig, pbGlobal, pbEvents, null, gamSubmoduleFactory);
beforeBidRequestCallback(() => {}, {});
auctionEndCallback(auctionResults);
expect(videoCoreMock.setAdTagUrl.calledOnce).to.be.true;
expect(videoCoreMock.setAdTagUrl.args[0][0]).to.be.equal(expectedAdTag);
expect(videoCoreMock.setAdTagUrl.args[0][1]).to.be.equal(expectedDivId);
expect(videoCoreMock.setAdTagUrl.args[0][2]).to.have.property('adUnitCode', expectedAdUnitCode);
expect(adQueueCoordinatorMock.queueAd.calledOnce).to.be.true;
expect(adQueueCoordinatorMock.queueAd.args[0][0]).to.be.equal(expectedAdTag);
expect(adQueueCoordinatorMock.queueAd.args[0][1]).to.be.equal(expectedDivId);
expect(adQueueCoordinatorMock.queueAd.args[0][2]).to.have.property('adUnitCode', expectedAdUnitCode);
});

it('should load ad tag from highest bid when ad server is not configured', function () {
Expand All @@ -287,72 +286,11 @@ describe('Prebid Video', function () {
pbVideoFactory(null, () => ({ providers: [] }), pbGlobal, pbEvents);
beforeBidRequestCallback(() => {}, {});
auctionEndCallback(auctionResults);
expect(videoCoreMock.setAdTagUrl.calledOnce).to.be.true;
expect(videoCoreMock.setAdTagUrl.args[0][0]).to.be.equal(expectedVastUrl);
expect(videoCoreMock.setAdTagUrl.args[0][1]).to.be.equal(expectedDivId);
expect(videoCoreMock.setAdTagUrl.args[0][2]).to.have.property('adUnitCode', expectedAdUnitCode);
expect(videoCoreMock.setAdTagUrl.args[0][2]).to.have.property('adXml', expectedVastXml);
});

it('should queue ad when required', function () {
const expectedVastUrl = 'expectedVastUrl';
const expectedVastXml = 'expectedVastXml';
const pbGlobal = Object.assign({}, pbGlobalMock, {
requestBids,
getHighestCpmBids: () => [{
vastUrl: expectedVastUrl,
vastXml: expectedVastXml
}, {}, {}, {}]
});
const expectedAdUnit = {
code: expectedAdUnitCode,
video: { divId: expectedDivId }
};
const auctionResults = { adUnits: [ expectedAdUnit, {} ] };

const adQueueCoordinator = Object.assign({}, adQueueCoordinatorMock, { requiresQueueing: () => true });
const pbEventsSpy = Object.assign({}, pbEvents, { emit: sinon.spy() });

pbVideoFactory(null, () => ({ providers: [] }), pbGlobal, pbEventsSpy, null, null, null, adQueueCoordinator);
beforeBidRequestCallback(() => {}, {});
auctionEndCallback(auctionResults);

expect(videoCoreMock.setAdTagUrl.called).to.be.false;

expect(adQueueCoordinator.queueAd.called).to.be.true;
const queueAdArgs = adQueueCoordinator.queueAd.firstCall.args;
expect(queueAdArgs[0]).to.be.equal(expectedVastUrl);
expect(queueAdArgs[1]).to.be.equal(expectedDivId);

expect(pbEventsSpy.emit.calledOnce).to.be.true;
expect(pbEventsSpy.emit.firstCall.args[0]).to.be.equal('videoAuctionAdLoadQueued');
});

it('should load tag when queueing not required', function () {
const expectedVastUrl = 'expectedVastUrl';
const expectedVastXml = 'expectedVastXml';
const pbGlobal = Object.assign({}, pbGlobalMock, {
requestBids,
getHighestCpmBids: () => [{
vastUrl: expectedVastUrl,
vastXml: expectedVastXml
}, {}, {}, {}]
});
const expectedAdUnit = {
code: expectedAdUnitCode,
video: { divId: expectedDivId }
};
const auctionResults = { adUnits: [ expectedAdUnit, {} ] };
const pbEventsSpy = Object.assign({}, pbEvents, { emit: sinon.spy() });

pbVideoFactory(null, () => ({ providers: [] }), pbGlobal, pbEventsSpy);
beforeBidRequestCallback(() => {}, {});
auctionEndCallback(auctionResults);

expect(adQueueCoordinatorMock.queueAd.called).to.be.false;
expect(videoCoreMock.setAdTagUrl.called).to.be.true;
expect(pbEventsSpy.emit.calledOnce).to.be.true;
expect(pbEventsSpy.emit.firstCall.args[0]).to.be.equal('videoAuctionAdLoadAttempt');
expect(adQueueCoordinatorMock.queueAd.calledOnce).to.be.true;
expect(adQueueCoordinatorMock.queueAd.args[0][0]).to.be.equal(expectedVastUrl);
expect(adQueueCoordinatorMock.queueAd.args[0][1]).to.be.equal(expectedDivId);
expect(adQueueCoordinatorMock.queueAd.args[0][2]).to.have.property('adUnitCode', expectedAdUnitCode);
expect(adQueueCoordinatorMock.queueAd.args[0][2]).to.have.property('adXml', expectedVastXml);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
SETUP_COMPLETE, SETUP_FAILED, PLAY, AD_IMPRESSION, videoEvents
} from 'libraries/video/constants/events.js';

import { PLAYBACK_MODE } from 'libraries/video/constants/enums.js';
import { PLAYBACK_MODE } from 'libraries/video/constants/constants.js';

function getPlayerMock() {
return makePlayerFactoryMock({
Expand Down

0 comments on commit f5fb7b6

Please sign in to comment.