Skip to content

Commit

Permalink
fix: test
Browse files Browse the repository at this point in the history
  • Loading branch information
rufiange committed Sep 23, 2024
1 parent 3eb270e commit 06956ff
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
6 changes: 3 additions & 3 deletions modules/contxtfulBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER, NATIVE, VIDEO } from '../src/mediaTypes.js';
import { _each, buildUrl, isStr, isEmptyStr, logInfo, logError } from '../src/utils.js';
import { ajax, sendBeacon } from '../src/ajax.js';
import * as ajax from '../src/ajax.js';
import { config as pbjsConfig } from '../src/config.js';
import {
isBidRequestValid,
Expand Down Expand Up @@ -179,11 +179,11 @@ const logEvent = (eventType, data, options = {}) => {
});

// Try sending the beacon
if (sendBeacon(eventUrl, JSON.stringify(payload))) {
if (ajax.sendBeacon(eventUrl, JSON.stringify(payload))) {
logInfo(BIDDER_CODE, `[${eventType}] Beacon sent with payload: ${JSON.stringify(data)}`);
} else {
// Fallback to using ajax
ajax(eventUrl, null, JSON.stringify(payload), {
ajax.ajax(eventUrl, null, JSON.stringify(payload), {
method: 'POST',
contentType: 'application/json',
withCredentials: true,
Expand Down
41 changes: 21 additions & 20 deletions test/spec/modules/contxtfulBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,23 +406,23 @@ describe('contxtful bid adapter', function () {
contxtful: {customer: CUSTOMER, version: VERSION, 'sampling': {'onTimeout': 0.0}},
});

const beaconStub = sandbox.stub(navigator, 'sendBeacon').returns(true);
const fetchStub = sandbox.stub(window, 'fetch').returns(true);
const beaconStub = sandbox.stub(ajax, 'sendBeacon').returns(true);
const ajaxStub = sandbox.stub(ajax, 'ajax');
expect(spec.onTimeout({'customData': 'customvalue'})).to.not.throw;
expect(beaconStub.called).to.be.false;
expect(fetchStub.called).to.be.false;
expect(ajaxStub.called).to.be.false;
});

it('will always call server if sampling is 1 with sendBeacon available', () => {
config.setConfig({
contxtful: {customer: CUSTOMER, version: VERSION, 'sampling': {'onTimeout': 1.0}},
});

const beaconStub = sandbox.stub(navigator, 'sendBeacon').returns(true);
const fetchStub = sandbox.stub(window, 'fetch').returns(true);
const beaconStub = sandbox.stub(ajax, 'sendBeacon').returns(true);
const ajaxStub = sandbox.stub(ajax, 'ajax');
expect(spec.onTimeout({'customData': 'customvalue'})).to.not.throw;
expect(beaconStub.called).to.be.true;
expect(fetchStub.called).to.be.false;
expect(ajaxStub.called).to.be.false;
});

it('will always call server if sampling is 1 with sendBeacon not available', () => {
Expand All @@ -431,9 +431,10 @@ describe('contxtful bid adapter', function () {
});

const ajaxStub = sandbox.stub(ajax, 'ajax');
const beaconStub = sandbox.stub(navigator, 'sendBeacon').value(undefined);
const beaconStub = sandbox.stub(ajax, 'sendBeacon').returns(false);
expect(spec.onTimeout({'customData': 'customvalue'})).to.not.throw;
expect(beaconStub.called).to.be.false;
expect(beaconStub.called).to.be.true;
expect(beaconStub.returned(false)).to.be.true;
expect(ajaxStub.calledOnce).to.be.true;
});
});
Expand All @@ -445,10 +446,10 @@ describe('contxtful bid adapter', function () {
});

const ajaxStub = sandbox.stub(ajax, 'ajax');
const beaconStub = sandbox.stub(navigator, 'sendBeacon').value(undefined);
const beaconStub = sandbox.stub(ajax, 'sendBeacon').returns(false);
spec.onBidderError({'customData': 'customvalue'});
expect(ajaxStub.calledOnce).to.equal(true);
expect(beaconStub.calledOnce).to.equal(false);
expect(ajaxStub.calledOnce).to.be.true;
expect(beaconStub.returned(false)).to.be.true;
});
});

Expand All @@ -459,10 +460,10 @@ describe('contxtful bid adapter', function () {
});

const ajaxStub = sandbox.stub(ajax, 'ajax');
const beaconStub = sandbox.stub(navigator, 'sendBeacon').value(undefined);
const beaconStub = sandbox.stub(ajax, 'sendBeacon').returns(false);
spec.onBidWon({'customData': 'customvalue'});
expect(ajaxStub.calledOnce).to.equal(true);
expect(beaconStub.calledOnce).to.equal(false);
expect(ajaxStub.calledOnce).to.be.true;
expect(beaconStub.returned(false)).to.be.true;
});
});

Expand All @@ -472,10 +473,10 @@ describe('contxtful bid adapter', function () {
contxtful: {customer: CUSTOMER, version: VERSION},
});
const ajaxStub = sandbox.stub(ajax, 'ajax');
const beaconStub = sandbox.stub(navigator, 'sendBeacon').value(undefined);
const beaconStub = sandbox.stub(ajax, 'sendBeacon').returns(false);
spec.onBidBillable({'customData': 'customvalue'});
expect(ajaxStub.calledOnce).to.equal(true);
expect(beaconStub.calledOnce).to.equal(false);
expect(ajaxStub.calledOnce).to.be.true;
expect(beaconStub.returned(false)).to.be.true;
});
});

Expand All @@ -485,10 +486,10 @@ describe('contxtful bid adapter', function () {
contxtful: {customer: CUSTOMER, version: VERSION},
});
const ajaxStub = sandbox.stub(ajax, 'ajax');
const beaconStub = sandbox.stub(navigator, 'sendBeacon').value(undefined);
const beaconStub = sandbox.stub(ajax, 'sendBeacon').returns(false);
spec.onAdRenderSucceeded({'customData': 'customvalue'});
expect(ajaxStub.calledOnce).to.equal(true);
expect(beaconStub.calledOnce).to.equal(false);
expect(ajaxStub.calledOnce).to.be.true;
expect(beaconStub.returned(false)).to.be.true;
});
});
});
Expand Down

0 comments on commit 06956ff

Please sign in to comment.