Skip to content

Commit

Permalink
Merge pull request #124 from MailOnline/feat/vpaid_enabled_flag
Browse files Browse the repository at this point in the history
feat: 🎸 new vpaidEnabled opt to disable VPAID ads
  • Loading branch information
carpasse authored Dec 24, 2018
2 parents 43946e7 + 661d835 commit ea67108
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
42 changes: 38 additions & 4 deletions src/runner/__tests__/runWaterfall.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
/* eslint-disable max-nested-callbacks */
import {
vastWrapperXML,
vastInlineXML,
wrapperParsedXML,
inlineAd,
inlineParsedXML,
vastInlineXML,
vastVpaidInlineXML,
vastWrapperXML,
vpaidInlineAd,
vpaidInlineParsedXML,
wrapperAd,
inlineAd
wrapperParsedXML
} from '../../../fixtures';
import defer from '../../utils/defer';
import requestAd from '../../vastRequest/requestAd';
Expand Down Expand Up @@ -117,6 +120,37 @@ describe('runWaterfall', () => {
});

describe('after fetching Vast response', () => {
test('must throw if it gets a vpaid ad with vpaidEnabled flag set to false', async () => {
const onError = jest.fn();
const vpaidChain = [
{
ad: vpaidInlineAd,
errorCode: null,
parsedXML: vpaidInlineParsedXML,
requestTag: 'https://test.example.com/vastadtaguri',
XML: vastVpaidInlineXML
}
];

requestAd.mockReturnValue(Promise.resolve(vpaidChain));

await runWaterfall(adTag, placeholder, {
...options,
onError,
vpaidEnabled: false
});
expect(onError).toHaveBeenCalledTimes(1);

const error = onError.mock.calls[0][0];

expect(error.code).toBe(200);
expect(error.message).toBe('VPAID ads are not supported by the current player');
expect(trackError).toHaveBeenCalledWith(vpaidChain, expect.objectContaining({
errorCode: 200,
tracker: options.tracker
}));
});

test('must call onError if Vast response is undefined', async () => {
const onError = jest.fn();

Expand Down
15 changes: 15 additions & 0 deletions src/runner/runWaterfall.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@
import {trackError} from '../tracker';
import requestAd from '../vastRequest/requestAd';
import requestNextAd from '../vastRequest/requestNextAd';
import {getInteractiveFiles} from '../vastSelectors';
import isIOS from '../utils/isIOS';
import run from './run';

const isVpaid = (vastChain) => Boolean(getInteractiveFiles(vastChain[0].ad));
const validateVastChain = (vastChain, options) => {
if (!vastChain || vastChain.length === 0) {
throw new Error('Invalid VastChain');
}

const lastVastResponse = vastChain[0];

if (!options.vpaidEnabled && isVpaid(vastChain)) {
const error = new Error('VPAID ads are not supported by the current player');

error.code = 200;
lastVastResponse.errorCode = 200;
lastVastResponse.error = error;
}

if (Boolean(lastVastResponse.error)) {
throw lastVastResponse.error;
}
Expand Down Expand Up @@ -138,6 +148,8 @@ const waterfall = async (fetchVastChain, placeholder, options, isCanceled) => {
* Defaults to `false`
* @param {number} [options.timeout] - timeout number in milliseconds. If set, the video ad will time out if it doesn't start within the specified time.
* @param {TrackerFn} [options.tracker] - If provided it will be used to track the VAST events instead of the default {@link pixelTracker}.
* @param {boolean} [options.vpaidEnabled] - if false and it gets a VPAID ad, it will throw an error before starting the ad and continue down in the waterfall.
* Defaults to `true`.
* @param {Object} [options.hooks] - Optional map with hooks to configure the behaviour of the ad.
* @param {Function} [options.hooks.createSkipControl] - If provided it will be called to generate the skip control. Must return a clickable [HTMLElement](https://developer.mozilla.org/es/docs/Web/API/HTMLElement) that is detached from the DOM.
* @param {Function} [options.hooks.validateVastResponse] - If provided it will be called for each valid vast response. Must throw if there is a problem with the vast response. If the Error instance has an `errorCode` number then it will be tracked using the error macros in the Vast response. It will also call {@link runWaterfall~onError} with the thrown error.
Expand All @@ -153,8 +165,11 @@ const runWaterfall = (adTag, placeholder, options) => {
adUnit = newAdUnit;
onAdStartHandler(adUnit);
};

const opts = {
vpaidEnabled: true,
...options,
// eslint-disable-next-line sort-keys
onAdReady: callbackHandler(options.onAdReady),
onAdStart,
onError: callbackHandler(options.onError),
Expand Down

0 comments on commit ea67108

Please sign in to comment.