Skip to content

Commit

Permalink
getCpmInNewCurrency to use current value of bid.cpm and bid.currency (p…
Browse files Browse the repository at this point in the history
…rebid#3845)

* getCpmInNewCurrency to use current value of bid.cpm and bid.currency

* added a test case for boosted bid, this test fails with old code
  • Loading branch information
pm-harshad-mane authored and AdSpacesDevelopers committed Jun 7, 2019
1 parent e68fcea commit 66ff696
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
5 changes: 1 addition & 4 deletions modules/currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,9 @@ export function addBidResponseHook(fn, adUnitCode, bid) {
bid.currency = 'USD';
}

let fromCurrency = bid.currency;
let cpm = bid.cpm;

// used for analytics
bid.getCpmInNewCurrency = function(toCurrency) {
return (parseFloat(cpm) * getCurrencyConversion(fromCurrency, toCurrency)).toFixed(3);
return (parseFloat(this.cpm) * getCurrencyConversion(this.currency, toCurrency)).toFixed(3);
};

// execute immediately if the bid is already in the desired currency
Expand Down
28 changes: 28 additions & 0 deletions test/spec/modules/currency_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,34 @@ describe('currency', function () {
expect(innerBid.getCpmInNewCurrency('JPY')).to.equal('100.000');
});

it('uses rates specified in json when provided and consider boosted bid', function () {
setConfig({
adServerCurrency: 'USD',
rates: {
USD: {
JPY: 100
}
}
});

var bid = { cpm: 100, currency: 'JPY', bidder: 'rubicon' };
var innerBid;

addBidResponseHook(function(adCodeId, bid) {
innerBid = bid;
}, 'elementId', bid);

expect(innerBid.cpm).to.equal('1.0000');
expect(typeof innerBid.getCpmInNewCurrency).to.equal('function');
expect(innerBid.getCpmInNewCurrency('JPY')).to.equal('100.000');

// Boosting the bid now
innerBid.cpm *= 10;
expect(innerBid.cpm).to.equal(10.0000);
expect(typeof innerBid.getCpmInNewCurrency).to.equal('function');
expect(innerBid.getCpmInNewCurrency('JPY')).to.equal('1000.000');
});

it('uses default rates when currency file fails to load', function () {
setConfig({});

Expand Down

0 comments on commit 66ff696

Please sign in to comment.