diff --git a/modules/adkernelAdnAnalyticsAdapter.js b/modules/adkernelAdnAnalyticsAdapter.js index 9402f51d1b6..ab57a84f4b0 100644 --- a/modules/adkernelAdnAnalyticsAdapter.js +++ b/modules/adkernelAdnAnalyticsAdapter.js @@ -171,6 +171,15 @@ const DIRECT = '(direct)'; const REFERRAL = '(referral)'; const ORGANIC = '(organic)'; +export let storage = { + getItem: (name) => { + return localStorage.getItem(name); + }, + setItem: (name, value) => { + localStorage.setItem(name, value); + } +}; + export function getUmtSource(pageUrl, referrer) { let prevUtm = getPreviousTrafficSource(); let currUtm = getCurrentTrafficSource(pageUrl, referrer); @@ -181,7 +190,7 @@ export function getUmtSource(pageUrl, referrer) { return actual; function getPreviousTrafficSource() { - let val = localStorage.getItem(ADKERNEL_PREBID_KEY); + let val = storage.getItem(ADKERNEL_PREBID_KEY); if (!val) { return getDirect(); } @@ -235,10 +244,10 @@ export function getUmtSource(pageUrl, referrer) { return; } let utmArgs = []; - for (let utmTagName of UTM_TAGS) { + utils._each(UTM_TAGS, (utmTagName) => { let utmValue = urlParameters[utmTagName] || ''; utmArgs.push(utmValue); - } + }); return asUtm.apply(this, utmArgs); } @@ -248,7 +257,7 @@ export function getUmtSource(pageUrl, referrer) { function storeUtm(utm) { let val = JSON.stringify(utm); - localStorage.setItem(ADKERNEL_PREBID_KEY, val); + storage.setItem(ADKERNEL_PREBID_KEY, val); } function asUtm(source, medium, campaign, term = '', content = '', c1 = '', c2 = '', c3 = '', c4 = '', c5 = '') { diff --git a/test/spec/modules/adkernelAdnAnalyticsAdapter_spec.js b/test/spec/modules/adkernelAdnAnalyticsAdapter_spec.js index e71689868f7..1a14660c6af 100644 --- a/test/spec/modules/adkernelAdnAnalyticsAdapter_spec.js +++ b/test/spec/modules/adkernelAdnAnalyticsAdapter_spec.js @@ -1,4 +1,4 @@ -import analyticsAdapter, {ExpiringQueue, getUmtSource} from 'modules/adkernelAdnAnalyticsAdapter'; +import analyticsAdapter, {ExpiringQueue, getUmtSource, storage} from 'modules/adkernelAdnAnalyticsAdapter'; import {expect} from 'chai'; import adaptermanager from 'src/adaptermanager'; import * as events from 'src/events'; @@ -48,8 +48,8 @@ describe('', () => { let stubGetItem; before(() => { - stubSetItem = sandbox.stub(localStorage, 'setItem'); - stubGetItem = sandbox.stub(localStorage, 'getItem'); + stubSetItem = sandbox.stub(storage, 'setItem'); + stubGetItem = sandbox.stub(storage, 'getItem'); }); afterEach(() => { @@ -114,27 +114,34 @@ describe('', () => { }); describe('ExpiringQueue', () => { - let start; + let timer; before(() => { - start = Date.now(); + timer = sandbox.useFakeTimers(0); }); + after(() => { + timer.restore(); + }); + it('should notify after timeout period', (done) => { let queue = new ExpiringQueue(() => { let elements = queue.popAll(); expect(elements).to.be.eql([1, 2, 3, 4]); elements = queue.popAll(); expect(elements).to.have.lengthOf(0); - expect(Date.now() - start).to.be.at.least(200).and.below(250); + expect(Date.now()).to.be.equal(200); done(); }, 100); queue.push(1); setTimeout(() => { queue.push([2, 3]); + timer.tick(50); }, 50); setTimeout(() => { queue.push([4]); + timer.tick(100); }, 100); + timer.tick(50); }); });