Skip to content

Commit

Permalink
Rivr Thin Adapter - part 2 (#6)
Browse files Browse the repository at this point in the history
* RVR-2087 - Remove sendAuction call from ExpiringQueue

* RVR-2147 - Fix test failing on IE 11.0.0

* RVR-2087 - Remove ExpiringQueue
  • Loading branch information
AlessandroDG authored Dec 11, 2018
1 parent 1fb91d9 commit 55e0ad2
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 88 deletions.
51 changes: 1 addition & 50 deletions modules/rivrAnalyticsAdapter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {ajax} from 'src/ajax';
import adapter from 'src/AnalyticsAdapter';
import find from 'core-js/library/fn/array/find';
import CONSTANTS from 'src/constants.json';
import adaptermanager from 'src/adaptermanager';
import * as utils from 'src/utils';
Expand Down Expand Up @@ -31,61 +30,13 @@ let rivrAnalytics = Object.assign(adapter({analyticsType}), {
}
});

/**
* Expiring queue implementation. Fires callback on elapsed timeout since last last update or creation.
* @param callback
* @param ttl
* @constructor
*/
export function ExpiringQueue(sendImpressions, sendAuction, ttl, log) {
let queue = [];
let timeoutId;

this.push = (event) => {
if (event instanceof Array) {
queue.push.apply(queue, event);
} else {
queue.push(event);
}
reset();
};

this.popAll = () => {
let result = queue;
queue = [];
reset();
return result;
};
/**
* For test/debug purposes only
* @return {Array}
*/
this.peekAll = () => {
return queue;
};

this.init = reset;

function reset() {
if (timeoutId) {
clearTimeout(timeoutId);
}
timeoutId = setTimeout(() => {
sendAuction();
if (queue.length) {
sendImpressions();
}
}, ttl);
}
};

// save the base class function
rivrAnalytics.originEnableAnalytics = rivrAnalytics.enableAnalytics;

// override enableAnalytics so we can get access to the config passed in from the page
rivrAnalytics.enableAnalytics = (config) => {
if (window.rivraddon && window.rivraddon.analytics) {
window.rivraddon.analytics.enableAnalytics(config, ExpiringQueue, {utils, ajax, find});
window.rivraddon.analytics.enableAnalytics(config, {utils, ajax});
rivrAnalytics.originEnableAnalytics(config);
}
};
Expand Down
40 changes: 2 additions & 38 deletions test/spec/modules/rivrAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as utils from 'src/utils';
import analyticsAdapter from 'modules/rivrAnalyticsAdapter';
import {
ExpiringQueue,
sendAuction,
sendImpressions,
handleClickEventWithClosureScope,
createUnOptimisedParamsField,
Expand Down Expand Up @@ -87,49 +85,15 @@ describe('RIVR Analytics adapter', () => {
delete window.rivraddon;
});

it('ExpiringQueue should call sendImpression callback after expiring queue timeout is elapsed', (done) => {
const sendImpressionMock = () => {
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()).to.be.equal(200);
done();
};
const sendAuctionMock = () => {};

let queue = new ExpiringQueue(
sendImpressionMock,
sendAuctionMock,
EXPIRING_QUEUE_TIMEOUT_MOCK);

queue.push(1);

setTimeout(() => {
queue.push([2, 3]);
timer.tick(50);
}, 50);
setTimeout(() => {
queue.push([4]);
timer.tick(100);
}, 100);
timer.tick(50);
});

it('enableAnalytics - should call rivraddon enableAnalytics with the correct arguments', () => {
// adaptermanager.enableAnalytics() is called in beforeEach. If just called here it doesn't seem to work.
const firstArgument = rivraddonsEnableAnalyticsStub.getCall(0).args[0];
const secondArgument = rivraddonsEnableAnalyticsStub.getCall(0).args[1];
const thirdArgument = rivraddonsEnableAnalyticsStub.getCall(0).args[2];

expect(firstArgument.provider).to.be.equal('rivr');

expect(typeof secondArgument).to.be.equal('function');
expect(secondArgument.name).to.be.equal('ExpiringQueue');

expect(thirdArgument).to.have.property('utils');
expect(thirdArgument).to.have.property('ajax');
expect(thirdArgument).to.have.property('find');
expect(secondArgument).to.have.property('utils');
expect(secondArgument).to.have.property('ajax');
});

it('Firing an event when rivraddon context is not defined it should do nothing', () => {
Expand Down

0 comments on commit 55e0ad2

Please sign in to comment.