Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing flaky adapter unit tests #6706

Merged
merged 5 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions modules/prebidmanagerAnalyticsAdapter.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {ajaxBuilder} from '../src/ajax.js';
import adapter from '../src/AnalyticsAdapter.js';
import adapterManager from '../src/adapterManager.js';
import { getStorageManager } from '../src/storageManager.js';

/**
* prebidmanagerAnalyticsAdapter.js - analytics adapter for prebidmanager
*/
export const storage = getStorageManager(undefined, 'prebidmanager');
const DEFAULT_EVENT_URL = 'https://endpoint.prebidmanager.com/endpoint'
const analyticsType = 'endpoint';
const analyticsName = 'Prebid Manager Analytics: ';
Expand Down Expand Up @@ -82,14 +84,14 @@ function collectUtmTagData() {
});
if (newUtm === false) {
utmTags.forEach(function (utmKey) {
let itemValue = localStorage.getItem(`pm_${utmKey}`);
let itemValue = storage.getDataFromLocalStorage(`pm_${utmKey}`);
if (itemValue && itemValue.length !== 0) {
pmUtmTags[utmKey] = itemValue;
}
});
} else {
utmTags.forEach(function (utmKey) {
localStorage.setItem(`pm_${utmKey}`, pmUtmTags[utmKey]);
storage.setDataInLocalStorage(`pm_${utmKey}`, pmUtmTags[utmKey]);
});
}
} catch (e) {
Expand Down
3 changes: 2 additions & 1 deletion modules/sspBCBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as utils from '../src/utils.js';
import { ajax } from '../src/ajax.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER } from '../src/mediaTypes.js';
import strIncludes from 'core-js-pure/features/string/includes.js';

const BIDDER_CODE = 'sspBC';
const BIDDER_URL = 'https://ssp.wp.pl/bidder/';
Expand Down Expand Up @@ -339,7 +340,7 @@ const spec = {
site.slot = slotid || site.slot;
}

if (bidRequest && site.id && !site.id.includes('bidid')) {
if (bidRequest && site.id && !strIncludes(site.id, 'bidid')) {
// found a matching request; add this bid

// store site data for future notification
Expand Down
3 changes: 2 additions & 1 deletion modules/userId/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ import { module, hook } from '../../src/hook.js';
import { createEidsArray, buildEidPermissions } from './eids.js';
import { getCoreStorageManager } from '../../src/storageManager.js';
import {getPrebidInternal} from '../../src/utils.js';
import includes from 'core-js-pure/features/array/includes.js';

const MODULE_NAME = 'User ID';
const COOKIE = 'cookie';
Expand Down Expand Up @@ -454,7 +455,7 @@ function getCombinedSubmoduleIdsForBidder(submodules, bidder) {
return {};
}
return submodules
.filter(i => !i.config.bidders || !utils.isArray(i.config.bidders) || i.config.bidders.includes(bidder))
.filter(i => !i.config.bidders || !utils.isArray(i.config.bidders) || includes(i.config.bidders, bidder))
.filter(i => utils.isPlainObject(i.idObj) && Object.keys(i.idObj).length)
.reduce((carry, i) => {
Object.keys(i.idObj).forEach(key => {
Expand Down
3 changes: 2 additions & 1 deletion modules/yieldmoBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BANNER, VIDEO } from '../src/mediaTypes.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { Renderer } from '../src/Renderer.js';
import includes from 'core-js-pure/features/array/includes';
import find from 'core-js-pure/features/array/find.js';

const BIDDER_CODE = 'yieldmo';
const CURRENCY = 'USD';
Expand Down Expand Up @@ -223,7 +224,7 @@ function createNewBannerBid(response) {
* @param bidRequest server request
*/
function createNewVideoBid(response, bidRequest) {
const imp = (utils.deepAccess(bidRequest, 'data.imp') || []).find(imp => imp.id === response.impid);
const imp = find((utils.deepAccess(bidRequest, 'data.imp') || []), imp => imp.id === response.impid);

let result = {
requestId: imp.id,
Expand Down
23 changes: 12 additions & 11 deletions test/spec/modules/prebidmanagerAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import prebidmanagerAnalytics from 'modules/prebidmanagerAnalyticsAdapter.js';
import prebidmanagerAnalytics, {
storage
} from 'modules/prebidmanagerAnalyticsAdapter.js';
import {expect} from 'chai';
import {server} from 'test/mocks/xhr.js';
import * as utils from 'src/utils.js';
Expand Down Expand Up @@ -105,19 +107,18 @@ describe('Prebid Manager Analytics Adapter', function () {
});

describe('build utm tag data', function () {
let getDataFromLocalStorageStub;
beforeEach(function () {
localStorage.setItem('pm_utm_source', 'utm_source');
localStorage.setItem('pm_utm_medium', 'utm_medium');
localStorage.setItem('pm_utm_campaign', 'utm_camp');
localStorage.setItem('pm_utm_term', '');
localStorage.setItem('pm_utm_content', '');
getDataFromLocalStorageStub = sinon.stub(storage, 'getDataFromLocalStorage');
getDataFromLocalStorageStub.withArgs('pm_utm_source').returns('utm_source');
getDataFromLocalStorageStub.withArgs('pm_utm_medium').returns('utm_medium');
getDataFromLocalStorageStub.withArgs('pm_utm_campaign').returns('utm_camp');
getDataFromLocalStorageStub.withArgs('pm_utm_term').returns('');
getDataFromLocalStorageStub.withArgs('pm_utm_content').returns('');
getDataFromLocalStorageStub.withArgs('pm_utm_source').returns('utm_source');
});
afterEach(function () {
localStorage.removeItem('pm_utm_source');
localStorage.removeItem('pm_utm_medium');
localStorage.removeItem('pm_utm_campaign');
localStorage.removeItem('pm_utm_term');
localStorage.removeItem('pm_utm_content');
getDataFromLocalStorageStub.restore();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ty so much for fixing this one! without a doubt the source of much committer frustration recently!

prebidmanagerAnalytics.disableAnalytics()
});
it('should build utm data from local storage', function () {
Expand Down
30 changes: 17 additions & 13 deletions test/spec/modules/sharethroughBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,20 @@ const setUserAgent = (uaString) => {
};

describe('sharethrough internal spec', function() {
let windowSpy, windowTopSpy;

let windowStub, windowTopStub;
let stubbedReturn = [{
appendChild: () => undefined
}]
beforeEach(function() {
windowSpy = sinon.spy(window.document, 'getElementsByTagName');
windowTopSpy = sinon.spy(window.top.document, 'getElementsByTagName');
windowStub = sinon.stub(window.document, 'getElementsByTagName');
windowTopStub = sinon.stub(window.top.document, 'getElementsByTagName');
windowStub.withArgs('body').returns(stubbedReturn);
windowTopStub.withArgs('body').returns(stubbedReturn);
});

afterEach(function() {
windowSpy.restore();
windowTopSpy.restore();
windowStub.restore();
windowTopStub.restore();
window.STR = undefined;
window.top.STR = undefined;
});
Expand All @@ -194,29 +198,29 @@ describe('sharethrough internal spec', function() {

it('appends sfp.js to the safeframe', function() {
sharethroughInternal.handleIframe();
expect(windowSpy.calledOnce).to.be.true;
expect(windowStub.calledOnce).to.be.true;
});

it('does not append anything if sfp.js is already loaded in the safeframe', function() {
window.STR = { Tag: true };
sharethroughInternal.handleIframe();
expect(windowSpy.notCalled).to.be.true;
expect(windowTopSpy.notCalled).to.be.true;
expect(windowStub.notCalled).to.be.true;
expect(windowTopStub.notCalled).to.be.true;
});
});

describe('we are able to bust out of the iframe', function() {
it('appends sfp.js to window.top', function() {
sharethroughInternal.handleIframe();
expect(windowSpy.calledOnce).to.be.true;
expect(windowTopSpy.calledOnce).to.be.true;
expect(windowStub.calledOnce).to.be.true;
expect(windowTopStub.calledOnce).to.be.true;
});

it('only appends sfp-set-targeting.js if sfp.js is already loaded on the page', function() {
window.top.STR = { Tag: true };
sharethroughInternal.handleIframe();
expect(windowSpy.calledOnce).to.be.true;
expect(windowTopSpy.notCalled).to.be.true;
expect(windowStub.calledOnce).to.be.true;
expect(windowTopStub.notCalled).to.be.true;
});
});
});
Expand Down