Skip to content

Commit

Permalink
fix: fix error code if no valid creative found
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepolischuk committed Jun 8, 2022
1 parent b9102d8 commit e918d9c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 45 deletions.
43 changes: 18 additions & 25 deletions src/runner/helpers/__tests__/startVideoAd.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@ import VpaidAdUnit from '../../../adUnit/VpaidAdUnit';
import canPlay from '../../../adUnit/helpers/media/canPlay';
import VideoAdContainer from '../../../adContainer/VideoAdContainer';
import startVideoAd from '../startVideoAd';
import {
start,
closeLinear
} from '../../../tracker/linearEvents';
import {
adStopped,
adUserClose
} from '../../../adUnit/helpers/vpaid/api';
import {errorCodes} from '../../../tracker';
import {start, closeLinear} from '../../../tracker/linearEvents';
import {adStopped, adUserClose} from '../../../adUnit/helpers/vpaid/api';

jest.mock('../../../adUnit/createVideoAdUnit');
jest.mock('../../../adUnit/helpers/media/canPlay');
Expand Down Expand Up @@ -153,10 +148,11 @@ describe('startVideoAd', () => {
});

test('must fail if the ad chain has no creatives', async () => {
expect.assertions(1);
expect.assertions(2);
try {
await startVideoAd(wrongVastAdChain, videoAdContainer, options);
} catch (error) {
expect(error.code).toBe(errorCodes.VAST_MEDIA_FILE_NOT_FOUND);
expect(error.message).toBe('No valid creative found in the passed VAST chain');
}
});
Expand All @@ -167,7 +163,7 @@ describe('startVideoAd', () => {
const adUnit = createAdUnitMock(vastAdChain, videoAdContainer, options);

createVideoAdUnit.mockImplementation(() => {
// eslint-disable-next-line promise/always-return, promise/always-return, promise/catch-or-return, promise/prefer-await-to-then
// eslint-disable-next-line promise/always-return, promise/always-return, promise/catch-or-return, promise/prefer-await-to-then
adUnit.start = () => {
adUnit.__simulateError(adUnitError);
};
Expand All @@ -178,11 +174,7 @@ describe('startVideoAd', () => {
expect(startVideoAd(vastAdChain, videoAdContainer, options)).rejects.toBe(adUnitError);
});

[
adUserClose,
adStopped,
closeLinear
].forEach((event) => {
[adUserClose, adStopped, closeLinear].forEach((event) => {
test(`must cancel the ad unit start on '${event}' event`, async () => {
expect.assertions(1);
const adUnit = createAdUnitMock(vastAdChain, videoAdContainer, options);
Expand Down Expand Up @@ -236,7 +228,7 @@ describe('startVideoAd', () => {
const adUnit = createAdUnitMock(vastAdChain, videoAdContainer, options);

createVideoAdUnit.mockImplementation(() => {
// eslint-disable-next-line promise/always-return, promise/always-return, promise/catch-or-return, promise/prefer-await-to-then
// eslint-disable-next-line promise/always-return, promise/always-return, promise/catch-or-return, promise/prefer-await-to-then
adUnit.start = () => {
adUnit.emit(start);
};
Expand Down Expand Up @@ -274,18 +266,19 @@ describe('startVideoAd', () => {
const adUnitError = new Error('adUnit error');
const adUnit = createAdUnitMock(vastAdChain, videoAdContainer, options);

createVideoAdUnit.mockImplementationOnce(() => {
const vpaidUnit = createAdUnitMock(vastAdChain, videoAdContainer, options);
createVideoAdUnit
.mockImplementationOnce(() => {
const vpaidUnit = createAdUnitMock(vastAdChain, videoAdContainer, options);

// eslint-disable-next-line promise/always-return, promise/always-return, promise/catch-or-return, promise/prefer-await-to-then
vpaidUnit.start = () => {
vpaidUnit.__simulateError(adUnitError);
};
// eslint-disable-next-line promise/always-return, promise/always-return, promise/catch-or-return, promise/prefer-await-to-then
vpaidUnit.start = () => {
vpaidUnit.__simulateError(adUnitError);
};

return vpaidUnit;
})
return vpaidUnit;
})
.mockImplementationOnce(() => {
// eslint-disable-next-line promise/always-return, promise/always-return, promise/catch-or-return, promise/prefer-await-to-then
// eslint-disable-next-line promise/always-return, promise/always-return, promise/catch-or-return, promise/prefer-await-to-then

adUnit.start = () => {
adUnit.emit(start);
Expand Down
38 changes: 18 additions & 20 deletions src/runner/helpers/startVideoAd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ import createVideoAdUnit from '../../adUnit/createVideoAdUnit';
import VideoAdContainer from '../../adContainer/VideoAdContainer';
import {getInteractiveFiles, getMediaFiles} from '../../vastSelectors';
import canPlay from '../../adUnit/helpers/media/canPlay';
import {
start,
closeLinear
} from '../../tracker/linearEvents';
import {
adStopped,
adUserClose
} from '../../adUnit/helpers/vpaid/api';
import {errorCodes} from '../../tracker';
import {start, closeLinear} from '../../tracker/linearEvents';
import {adStopped, adUserClose} from '../../adUnit/helpers/vpaid/api';

const validate = (vastChain, videoAdContainer) => {
if (!Array.isArray(vastChain) || vastChain.length === 0) {
Expand All @@ -33,23 +28,26 @@ const hasVastCreative = (ad, videoElement) => {
return false;
};

const startAdUnit = (adUnit, {onAdReady}) => new Promise((resolve, reject) => {
const createRejectHandler = (event) => () =>
reject(new Error(`Ad unit start rejected due to event '${event}'`));
const startAdUnit = (adUnit, {onAdReady}) =>
new Promise((resolve, reject) => {
const createRejectHandler = (event) => () => reject(new Error(`Ad unit start rejected due to event '${event}'`));

adUnit.onError(reject);
adUnit.on(start, () => resolve(adUnit));
adUnit.on(adUserClose, createRejectHandler(adUserClose));
adUnit.on(closeLinear, createRejectHandler(closeLinear));
adUnit.on(adStopped, createRejectHandler(adStopped));
adUnit.onError(reject);
adUnit.on(start, () => resolve(adUnit));
adUnit.on(adUserClose, createRejectHandler(adUserClose));
adUnit.on(closeLinear, createRejectHandler(closeLinear));
adUnit.on(adStopped, createRejectHandler(adStopped));

onAdReady(adUnit);
adUnit.start();
});
onAdReady(adUnit);
adUnit.start();
});

const tryToStartVpaidAd = (vastChain, videoAdContainer, options) => {
if (!hasVpaidCreative(vastChain[0].ad)) {
throw new Error('No valid creative found in the passed VAST chain');
const error = new Error('No valid creative found in the passed VAST chain');

error.code = errorCodes.VAST_MEDIA_FILE_NOT_FOUND;
throw error;
}

const adUnit = createVideoAdUnit(vastChain, videoAdContainer, {
Expand Down

0 comments on commit e918d9c

Please sign in to comment.