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 1d9120a commit 3eb270e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 32 deletions.
6 changes: 2 additions & 4 deletions modules/contxtfulBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ const getSamplingRate = (bidderConfig, eventType) => {
const logEvent = (eventType, data, options = {}) => {
const {
samplingEnabled = false,
ajaxFunc = ajax,
sendBeaconFunc = sendBeacon,
} = options;

try {
Expand Down Expand Up @@ -181,11 +179,11 @@ const logEvent = (eventType, data, options = {}) => {
});

// Try sending the beacon
if (sendBeaconFunc?.(eventUrl, JSON.stringify(payload))) {
if (sendBeacon(eventUrl, JSON.stringify(payload))) {
logInfo(BIDDER_CODE, `[${eventType}] Beacon sent with payload: ${JSON.stringify(data)}`);
} else {
// Fallback to using ajax
ajaxFunc?.(eventUrl, null, JSON.stringify(payload), {
ajax(eventUrl, null, JSON.stringify(payload), {
method: 'POST',
contentType: 'application/json',
withCredentials: true,
Expand Down
67 changes: 39 additions & 28 deletions test/spec/modules/contxtfulBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@ const RX_FROM_API = { ReceptivityState: 'Receptive', test_info: 'rx_from_engine'

describe('contxtful bid adapter', function () {
const adapter = newBidder(spec);
let sandbox, ajaxStub, beaconStub;
let sandbox;

beforeEach(function () {
sandbox = sinon.sandbox.create();
ajaxStub = sandbox.stub(ajax, 'ajax');
beaconStub = sandbox.stub(navigator, 'sendBeacon');
});

afterEach(function () {
sandbox.restore();
ajaxStub.restore();
beaconStub.restore();
});

describe('is a functions', function () {
Expand Down Expand Up @@ -405,46 +401,54 @@ describe('contxtful bid adapter', function () {
});

describe('on timeout callback', () => {
it('will never call server if sampling is 0', () => {
it('will never call server if sampling is 0 with sendBeacon available', () => {
config.setConfig({
contxtful: {customer: CUSTOMER, version: VERSION, 'sampling': {'onTimeout': 0.0}},
});

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

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

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

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

expect(spec.onBidderError({'customData': 'customvalue'}, { ajaxFunc: ajaxStub, sendBeaconFunc: beaconStub })).to.not.throw;
expect(ajaxStub.calledOnce).to.equal(false);
expect(beaconStub.calledOnce).to.equal(false);
const ajaxStub = sandbox.stub(ajax, 'ajax');
const beaconStub = sandbox.stub(navigator, 'sendBeacon').value(undefined);
expect(spec.onTimeout({'customData': 'customvalue'})).to.not.throw;
expect(beaconStub.called).to.be.false;
expect(ajaxStub.calledOnce).to.be.true;
});
});

describe('on onBidderError callback', () => {
it('will always call server if sampling is 1', () => {
config.setConfig({
contxtful: {customer: CUSTOMER, version: VERSION, 'sampling': {'onBidderError': 1.0}},
});

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

Expand All @@ -453,9 +457,12 @@ describe('contxtful bid adapter', function () {
config.setConfig({
contxtful: {customer: CUSTOMER, version: VERSION},
});
spec.onBidWon({'customData': 'customvalue'}, { ajaxFunc: ajaxStub, sendBeaconFunc: beaconStub });

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

Expand All @@ -464,9 +471,11 @@ describe('contxtful bid adapter', function () {
config.setConfig({
contxtful: {customer: CUSTOMER, version: VERSION},
});
spec.onBidBillable({'customData': 'customvalue'}, { ajaxFunc: ajaxStub, sendBeaconFunc: beaconStub });
const ajaxStub = sandbox.stub(ajax, 'ajax');
const beaconStub = sandbox.stub(navigator, 'sendBeacon').value(undefined);
spec.onBidBillable({'customData': 'customvalue'});
expect(ajaxStub.calledOnce).to.equal(true);
expect(beaconStub.calledOnce).to.equal(true);
expect(beaconStub.calledOnce).to.equal(false);
});
});

Expand All @@ -475,9 +484,11 @@ describe('contxtful bid adapter', function () {
config.setConfig({
contxtful: {customer: CUSTOMER, version: VERSION},
});
spec.onAdRenderSucceeded({'customData': 'customvalue'}, { ajaxFunc: ajaxStub, sendBeaconFunc: beaconStub });
const ajaxStub = sandbox.stub(ajax, 'ajax');
const beaconStub = sandbox.stub(navigator, 'sendBeacon').value(undefined);
spec.onAdRenderSucceeded({'customData': 'customvalue'});
expect(ajaxStub.calledOnce).to.equal(true);
expect(beaconStub.calledOnce).to.equal(true);
expect(beaconStub.calledOnce).to.equal(false);
});
});
});
Expand Down

0 comments on commit 3eb270e

Please sign in to comment.