From 2c829e5cc3b1938571c381b5dc14c0ff8047799f Mon Sep 17 00:00:00 2001 From: Jaimin Panchal Date: Thu, 30 Nov 2017 11:21:42 -0500 Subject: [PATCH] bug fixed to populate userSync default values (#1897) --- src/config.js | 45 +++++++++++++++++++++++++++++----------- src/userSync.js | 10 +++++++++ test/spec/config_spec.js | 8 +++++-- 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/src/config.js b/src/config.js index 41ba9d25301..5ed83e555b2 100644 --- a/src/config.js +++ b/src/config.js @@ -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', @@ -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, @@ -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) { @@ -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); } /* @@ -246,7 +266,8 @@ export function newConfig() { return { getConfig, - setConfig + setConfig, + setDefaults }; } diff --git a/src/userSync.js b/src/userSync.js index 8fb8c04cd24..939c42c28a0 100644 --- a/src/userSync.js +++ b/src/userSync.js @@ -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. * diff --git a/test/spec/config_spec.js b/test/spec/config_spec.js index 14452987091..e99e739d630 100644 --- a/test/spec/config_spec.js +++ b/test/spec/config_spec.js @@ -6,6 +6,7 @@ const utils = require('src/utils'); let getConfig; let setConfig; +let setDefaults; describe('config API', () => { let logErrorSpy; @@ -13,6 +14,7 @@ describe('config API', () => { const config = newConfig(); getConfig = config.getConfig; setConfig = config.setConfig; + setDefaults = config.setDefaults; logErrorSpy = sinon.spy(utils, 'logError'); }); @@ -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', () => {