Skip to content

Commit

Permalink
Rename functions and add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewlane committed Sep 5, 2017
1 parent 22e2234 commit 111d829
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/adaptermanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function getBids({bidderCode, requestId, bidderRequestId, adUnits}) {
}

if (adUnit.mediaTypes) {
if (utils.mediaTypesValid(adUnit.mediaTypes)) {
if (utils.isValidMediaTypes(adUnit.mediaTypes)) {
bid = Object.assign({}, bid, { mediaTypes: adUnit.mediaTypes });
} else {
utils.logError(`mediaTypes is not correctly configured`);
Expand Down
4 changes: 2 additions & 2 deletions src/bidmanager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { uniques, flatten, adUnitsFilter, getBidderRequest } from './utils';
import { getPriceBucketString } from './cpmBucketManager';
import { NATIVE_KEYS, nativeBidIsValid } from './native';
import { videoBidIsValid } from './video';
import { isValidVideoBid } from './video';
import { getCacheUrl, store } from './videoCache';
import { Renderer } from 'src/Renderer';
import { config } from 'src/config';
Expand Down Expand Up @@ -114,7 +114,7 @@ exports.addBidResponse = function (adUnitCode, bid) {
utils.logError(errorMessage('Native bid missing some required properties.'));
return false;
}
if (bid.mediaType === 'video' && !videoBidIsValid(bid)) {
if (bid.mediaType === 'video' && !isValidVideoBid(bid)) {
utils.logError(errorMessage(`Video bid does not have required vastUrl or renderer property`));
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -719,10 +719,10 @@ export function getDefinedParams(object, params) {

/**
* Validates an adunit's `mediaTypes` parameter
* @param{MediaTypes} mediaTypes mediaTypes parameter to validate
* @returns{boolean} If object is valie
* @param {MediaTypes} mediaTypes mediaTypes parameter to validate
* @return {boolean} If object is valid
*/
export function mediaTypesValid(mediaTypes) {
export function isValidMediaTypes(mediaTypes) {
const SUPPORTED_MEDIA_TYPES = ['banner', 'native', 'video'];
const SUPPORTED_STREAM_TYPES = ['instream', 'outstream'];

Expand Down
11 changes: 9 additions & 2 deletions src/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ const nonVideoBidder = bid => !videoAdapters.includes(bid.bidder);
export const hasNonVideoBidder = adUnit =>
adUnit.bids.filter(nonVideoBidder).length;

/*
/**
* @typedef {object} VideoBid
* @property {string} adId id of the bid
*/

/**
* Validate that the assets required for video context are present on the bid
* @param {VideoBid} bid video bid to validate
* @return {boolean} If object is valid
*/
export function videoBidIsValid(bid) {
export function isValidVideoBid(bid) {
const bidRequest = getBidRequest(bid.adId);
const context =
bidRequest && deepAccess(bidRequest, 'mediaTypes.video.context');
Expand Down
70 changes: 70 additions & 0 deletions test/spec/video_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { isValidVideoBid } from 'src/video';
const utils = require('src/utils');

describe('video.js', () => {
it('validates valid instream bids', () => {
sinon.stub(utils, 'getBidRequest', () => ({
bidder: 'appnexusAst',
mediaTypes: {
video: { context: 'instream' },
},
}));

const valid = isValidVideoBid({
vastUrl: 'http://www.example.com/vastUrl'
});

expect(valid).to.be(true);

utils.getBidRequest.restore();
});

it('catches invalid instream bids', () => {
sinon.stub(utils, 'getBidRequest', () => ({
bidder: 'appnexusAst',
mediaTypes: {
video: { context: 'instream' },
},
}));

const valid = isValidVideoBid({});

expect(valid).to.be(false);

utils.getBidRequest.restore();
});

it('validates valid outstream bids', () => {
sinon.stub(utils, 'getBidRequest', () => ({
bidder: 'appnexusAst',
mediaTypes: {
video: { context: 'outstream' },
},
renderer: {
url: 'render.url',
render: () => true,
}
}));

const valid = isValidVideoBid({});

expect(valid).to.be(true);

utils.getBidRequest.restore();
});

it('catches invalid outstream bids', () => {
sinon.stub(utils, 'getBidRequest', () => ({
bidder: 'appnexusAst',
mediaTypes: {
video: { context: 'outstream' },
},
}));

const valid = isValidVideoBid({});

expect(valid).to.be(false);

utils.getBidRequest.restore();
});
});

0 comments on commit 111d829

Please sign in to comment.