Skip to content

Commit

Permalink
Unit-test fix (#1927)
Browse files Browse the repository at this point in the history
  • Loading branch information
ckbo3hrk authored and matthewlane committed Dec 6, 2017
1 parent bceb74f commit 8512d83
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
17 changes: 13 additions & 4 deletions modules/adkernelAdnAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}
Expand Down Expand Up @@ -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);
}

Expand All @@ -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 = '') {
Expand Down
19 changes: 13 additions & 6 deletions test/spec/modules/adkernelAdnAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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);
});
});

Expand Down

0 comments on commit 8512d83

Please sign in to comment.