From fd4dfbda369f6f93c3af3cc460ce57f798fdbf3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Wo=C5=BAniak?= Date: Fri, 25 May 2018 09:12:04 +0000 Subject: [PATCH] Merged in RVR-1181-Prebid-js-unit-tests-setup (pull request #6) RVR-1181 Prebid.js Unit tests setup Approved-by: Alessandro Di Giovanni --- modules/rivrAnalyticsAdapter.js | 7 +- .../spec/modules/rivrAnalyticsAdapter_spec.js | 221 ++++++++++++++++++ 2 files changed, 224 insertions(+), 4 deletions(-) create mode 100644 test/spec/modules/rivrAnalyticsAdapter_spec.js diff --git a/modules/rivrAnalyticsAdapter.js b/modules/rivrAnalyticsAdapter.js index 35a14c588bcc..ea13d233d3e3 100644 --- a/modules/rivrAnalyticsAdapter.js +++ b/modules/rivrAnalyticsAdapter.js @@ -51,7 +51,7 @@ let rivrAnalytics = Object.assign(adapter({analyticsType}), { function sendAuction() { let auctionObject = rivrAnalytics.context.auctionObject; let req = Object.assign({}, {Auction: auctionObject}); - auctionObject = fulfillAuctionObject(); + rivrAnalytics.context.auctionObject = fulfillAuctionObject(); logInfo('sending request to analytics => ', req); ajax(`http://${rivrAnalytics.context.host}/${rivrAnalytics.context.clientID}/auctions`, () => { }, JSON.stringify(req)); @@ -83,9 +83,7 @@ function trackBidResponse(args) { function trackBidWon(args) { let auctionObject = rivrAnalytics.context.auctionObject; - let bidResponse = createBidResponse(args); let auctionImpression = createAuctionImpression(args); - auctionObject.bidResponses.push(bidResponse); auctionObject.imp.push(auctionImpression); }; @@ -255,7 +253,7 @@ function addHandlers(bannersIds) { }; function fulfillAuctionObject() { - return { + let newAuction = { id: null, timestamp: null, at: null, @@ -312,6 +310,7 @@ function fulfillAuctionObject() { }, bidResponses: [] } + return newAuction; }; /** * Expiring queue implementation. Fires callback on elapsed timeout since last last update or creation. diff --git a/test/spec/modules/rivrAnalyticsAdapter_spec.js b/test/spec/modules/rivrAnalyticsAdapter_spec.js new file mode 100644 index 000000000000..b0d3b2fd72f0 --- /dev/null +++ b/test/spec/modules/rivrAnalyticsAdapter_spec.js @@ -0,0 +1,221 @@ +import analyticsAdapter, {ExpiringQueue} from 'modules/rivrAnalyticsAdapter'; +import {expect} from 'chai'; +import adaptermanager from 'src/adaptermanager'; +import * as ajax from 'src/ajax'; +import CONSTANTS from 'src/constants.json'; + +const events = require('../../../src/events'); + +describe('', () => { + let sandbox; + + before(() => { + sandbox = sinon.sandbox.create(); + }); + + after(() => { + sandbox.restore(); + analyticsAdapter.disableAnalytics(); + }); + + describe('ExpiringQueue', () => { + let timer; + before(() => { + 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()).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); + }); + }); + + const REQUEST = { + bidderCode: 'adapter', + auctionId: '5018eb39-f900-4370-b71e-3bb5b48d324f', + bidderRequestId: '1a6fc81528d0f6', + bids: [{ + bidder: 'adapter', + params: {}, + adUnitCode: 'container-1', + transactionId: 'de90df62-7fd0-4fbc-8787-92d133a7dc06', + sizes: [[300, 250]], + bidId: '208750227436c1', + bidderRequestId: '1a6fc81528d0f6', + auctionId: '5018eb39-f900-4370-b71e-3bb5b48d324f' + }], + auctionStart: 1509369418387, + timeout: 3000, + start: 1509369418389 + }; + + const RESPONSE = { + bidderCode: 'adapter', + width: 300, + height: 250, + statusMessage: 'Bid available', + getStatusCode: () => 1, + adId: '208750227436c1', + mediaType: 'banner', + cpm: 0.015, + creativeId: 999, + ad: '', + auctionId: '5018eb39-f900-4370-b71e-3bb5b48d324f', + responseTimestamp: 1509369418832, + requestTimestamp: 1509369418389, + bidder: 'adapter', + adUnitCode: 'container-1', + timeToRespond: 443, + currency: 'EU', + size: '300x250' + }; + + describe('Analytics adapter', () => { + let ajaxStub; + let timer; + + before(() => { + ajaxStub = sandbox.stub(ajax, 'ajax'); + timer = sandbox.useFakeTimers(0); + }); + + beforeEach(() => { + sandbox.stub(events, 'getEvents').callsFake(() => { + return [] + }); + }); + + afterEach(() => { + events.getEvents.restore(); + }); + + it('should be configurable', () => { + adaptermanager.registerAnalyticsAdapter({ + code: 'rivr', + adapter: analyticsAdapter + }); + + adaptermanager.enableAnalytics({ + provider: 'rivr', + options: { + pubId: 777, + } + }); + + expect(analyticsAdapter.context).to.have.property('host', 'integrations.rivr.simplaex.net'); + expect(analyticsAdapter.context).to.have.property('pubId', 777); + }); + + it('should handle auction init event', () => { + events.emit(CONSTANTS.EVENTS.AUCTION_INIT, {auctionId: 1, config: {}, timeout: 3000}); + const auctionId = analyticsAdapter.context.auctionObject.id; + const auctionStart = analyticsAdapter.context.auctionTimeStart; + expect(auctionId).to.be.eql(1); + }); + + it('should handle bid request event', () => { + events.emit(CONSTANTS.EVENTS.BID_REQUESTED, REQUEST); + const sitePubcid = analyticsAdapter.context.auctionObject.site.publisher.id; + const appPubcid = analyticsAdapter.context.auctionObject.app.publisher.id; + expect(sitePubcid).to.be.eql(777); + expect(sitePubcid).to.be.eql(appPubcid); + }); + + it('should handle bid response event', () => { + events.emit(CONSTANTS.EVENTS.BID_RESPONSE, RESPONSE); + const ev = analyticsAdapter.context.auctionObject.bidResponses; + expect(ev).to.have.length(1); + expect(ev[0]).to.be.eql({ + timestamp: 1509369418832, + status: 1, + 'total_duration': 443, + bidderId: null, + 'bidder_name': 'adapter', + cur: 'EU', + seatid: [ + { + seat: null, + bid: [ + { + status: 1, + 'clear_price': 0.015, + attr: [], + crid: 999, + cid: null, + id: null, + adid: '208750227436c1', + adomain: [], + iurl: null + } + ] + } + ] + }); + }); + + it('should handle auction end event', () => { + timer.tick(447); + events.emit(CONSTANTS.EVENTS.AUCTION_END, RESPONSE); + const endTime = analyticsAdapter.context.auctionTimeEnd; + expect(endTime).to.be.eql(447); + }); + + it('should handle winning bid', () => { + events.emit(CONSTANTS.EVENTS.BID_WON, RESPONSE); + const ev = analyticsAdapter.context.auctionObject.imp; + expect(ev.length).to.be.eql(1); + expect(ev[0]).to.be.eql({ + tagid: 'container-1', + displaymanager: null, + displaymanagerver: null, + secure: null, + bidfloor: null, + banner: { + w: 300, + h: 250, + pos: null, + expandable: [], + api: [] + } + }); + }); + + it('sends request after timeout', () => { + let impressions = analyticsAdapter.context.auctionObject.imp; + let responses = analyticsAdapter.context.auctionObject.bidResponses; + + expect(impressions.length).to.be.eql(1); + expect(responses.length).to.be.eql(1); + expect(ajaxStub.calledOnce).to.be.equal(false); + + timer.tick(4500); + + let impressionss = analyticsAdapter.context.auctionObject.imp; + let responsess = analyticsAdapter.context.auctionObject.bidResponses; + + expect(ajaxStub.calledOnce).to.be.equal(true); + expect(impressionss.length).to.be.eql(0); + expect(responsess.length).to.be.eql(0); + }); + }); +});