Skip to content

Commit

Permalink
Updated specs to include localStorage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pnhegde committed Jul 23, 2024
1 parent c894dbf commit d5a2132
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions test/spec/modules/pubxaiRtdProvider_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { config } from '../../../src/config';
import * as hook from '../../../src/hook.js';
import { server } from '../../mocks/xhr.js';
import { getStorageManager } from '../../../src/storageManager.js';

const getConfig = () => ({
params: {
Expand Down Expand Up @@ -45,6 +46,7 @@ const resetGlobals = () => {
window.__pubxFloorsConfig__ = undefined;
window.__pubxFloorsApiStatus__ = undefined;
window.__pubxFloorRulesPromise__ = null;
localStorage.removeItem('pubx:dynamicFloors');
};

const fakeServer = (
Expand Down Expand Up @@ -119,7 +121,7 @@ describe('pubxaiRtdProvider', () => {
stub.restore();
});
it('createFloorsDataForAuction called once before and once after __pubxFloorRulesPromise__. Also getBidRequestData executed only once', async () => {
pubxaiSubmodule.getBidRequestData(reqBidsConfigObj, () => {});
pubxaiSubmodule.getBidRequestData(reqBidsConfigObj, () => { });
assert(priceFloors.createFloorsDataForAuction.calledOnce);
await window.__pubxFloorRulesPromise__;
assert(priceFloors.createFloorsDataForAuction.calledTwice);
Expand All @@ -129,14 +131,24 @@ describe('pubxaiRtdProvider', () => {
reqBidsConfigObj.auctionId
)
);
pubxaiSubmodule.getBidRequestData(reqBidsConfigObj, () => {});
pubxaiSubmodule.getBidRequestData(reqBidsConfigObj, () => { });
await window.__pubxFloorRulesPromise__;
assert(priceFloors.createFloorsDataForAuction.calledTwice);
});
});
describe('fetchFloorRules', () => {
const providerConfig = getConfig();
const floorsResponse = getFloorsResponse();
let storageStub;

beforeEach(() => {
storageStub = sinon.stub(getStorageManager({ moduleType: 'rtd', moduleName: 'pubxai' }), 'getDataFromLocalStorage');
});

afterEach(() => {
storageStub.restore();
});

it('success with floors response', (done) => {
const promise = fetchFloorRules(providerConfig);
fakeServer(floorsResponse);
Expand All @@ -145,6 +157,7 @@ describe('pubxaiRtdProvider', () => {
done();
});
});

it('success with no floors response', (done) => {
const promise = fetchFloorRules(providerConfig);
fakeServer(undefined);
Expand All @@ -153,6 +166,7 @@ describe('pubxaiRtdProvider', () => {
done();
});
});

it('API call error', (done) => {
const promise = fetchFloorRules(providerConfig);
fakeServer(undefined, undefined, 404);
Expand All @@ -167,6 +181,7 @@ describe('pubxaiRtdProvider', () => {
done();
});
});

it('Wrong API response', (done) => {
const promise = fetchFloorRules(providerConfig);
fakeServer('floorsResponse');
Expand All @@ -181,6 +196,25 @@ describe('pubxaiRtdProvider', () => {
done();
});
});

it('success with local data response', (done) => {
const localFloorsResponse = getFloorsResponse();
storageStub.withArgs('pubx:dynamicFloors').returns(JSON.stringify(localFloorsResponse));
const promise = fetchFloorRules({ params: {} });
promise.then((res) => {
expect(res).to.deep.equal(localFloorsResponse);
done();
});
});

it('no local data response', (done) => {
storageStub.withArgs('pubx:dynamicFloors').returns(null);
const promise = fetchFloorRules({ params: {} });
promise.then((res) => {
expect(res).to.deep.equal(null);
done();
});
});
});
describe('setPriceFloors', () => {
const providerConfig = getConfig();
Expand Down

0 comments on commit d5a2132

Please sign in to comment.