Skip to content

Commit

Permalink
bug fixed to populate userSync default values (prebid#1897)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaiminpanchal27 authored and dluxemburg committed Jul 17, 2018
1 parent 53745bd commit f27bfc9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
45 changes: 33 additions & 12 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ const DEFAULT_BIDDER_TIMEOUT = 3000;
const DEFAULT_PUBLISHER_DOMAIN = window.location.origin;
const DEFAULT_COOKIESYNC_DELAY = 100;
const DEFAULT_ENABLE_SEND_ALL_BIDS = false;
const DEFAULT_USERSYNC = {
syncEnabled: true,
pixelEnabled: true,
syncsPerBidder: 5,
syncDelay: 3000
};

const GRANULARITY_OPTIONS = {
LOW: 'low',
Expand All @@ -44,6 +38,8 @@ const ALL_TOPICS = '*';
export function newConfig() {
let listeners = [];

let defaults = {};

let config = {
// `debug` is equivalent to legacy `pbjs.logging` property
_debug: DEFAULT_DEBUG,
Expand Down Expand Up @@ -122,10 +118,7 @@ export function newConfig() {
// calls existing function which may be moved after deprecation
set s2sConfig(val) {
$$PREBID_GLOBAL$$.setS2SConfig(val);
},

// userSync defaults
userSync: DEFAULT_USERSYNC
}
};

function hasGranularity(val) {
Expand Down Expand Up @@ -177,8 +170,35 @@ export function newConfig() {
return;
}

let topics = Object.keys(options);
let topicalConfig = {};

topics.forEach(topic => {
let option = options[topic];

if (typeof defaults[topic] === 'object' && typeof option === 'object') {
option = Object.assign({}, defaults[topic], option);
}

topicalConfig[topic] = config[topic] = option;
});

callSubscribers(topicalConfig);
}

/**
* Sets configuration defaults which setConfig values can be applied on top of
* @param {object} options
*/
function setDefaults(options) {
if (typeof defaults !== 'object') {
utils.logError('defaults must be an object');
return;
}

Object.assign(defaults, options);
// Add default values to config as well
Object.assign(config, options);
callSubscribers(options);
}

/*
Expand Down Expand Up @@ -246,7 +266,8 @@ export function newConfig() {

return {
getConfig,
setConfig
setConfig,
setDefaults
};
}

Expand Down
10 changes: 10 additions & 0 deletions src/userSync.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import * as utils from 'src/utils';
import { config } from 'src/config';

// Set userSync default values
config.setDefaults({
'userSync': {
syncEnabled: true,
pixelEnabled: true,
syncsPerBidder: 5,
syncDelay: 3000
}
});

/**
* Factory function which creates a new UserSyncPool.
*
Expand Down
8 changes: 6 additions & 2 deletions test/spec/config_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ const utils = require('src/utils');

let getConfig;
let setConfig;
let setDefaults;

describe('config API', () => {
let logErrorSpy;
beforeEach(() => {
const config = newConfig();
getConfig = config.getConfig;
setConfig = config.setConfig;
setDefaults = config.setDefaults;
logErrorSpy = sinon.spy(utils, 'logError');
});

Expand Down Expand Up @@ -86,12 +88,14 @@ describe('config API', () => {
});

it('gets default userSync config', () => {
expect(getConfig('userSync')).to.eql({
const DEFAULT_USERSYNC = {
syncEnabled: true,
pixelEnabled: true,
syncsPerBidder: 5,
syncDelay: 3000
});
};
setDefaults({'userSync': DEFAULT_USERSYNC});
expect(getConfig('userSync')).to.eql(DEFAULT_USERSYNC);
});

it('has subscribe functionality for adding listeners to config updates', () => {
Expand Down

0 comments on commit f27bfc9

Please sign in to comment.