Skip to content

Commit

Permalink
Delete more libs (#1707)
Browse files Browse the repository at this point in the history
* Inline breakpointNameToAttribute

* Inline isAdsOff so user-prefs can be deleted

* Delete user-prefs

* Delete cookie.ts

* Inline and remove manage-ad-free-cookie

* Delete contributions-utilities

* Delete beacon.js

* Delete mvt-cookie

* Delete libs jest mocks
  • Loading branch information
arelra authored Dec 20, 2024
1 parent c4b27bb commit 1273e6d
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 373 deletions.
10 changes: 9 additions & 1 deletion src/define/Advert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
import type { AdSize, SizeMapping, SlotName } from '../core/ad-sizes';
import { concatSizeMappings } from '../core/create-ad-slot';
import type { Breakpoint } from '../core/lib/breakpoint';
import { breakpointNameToAttribute } from '../lib/breakpoint-name-to-attribute';
import type { HeaderBiddingSize } from '../lib/header-bidding/prebid-types';
import fastdom from '../utils/fastdom-promise';
import { buildGoogletagSizeMapping, defineSlot } from './define-slot';
Expand Down Expand Up @@ -38,6 +37,15 @@ const stringToTuple = (size: string): [number, number] => {
const createSizeMapping = (attr: string): AdSize[] =>
attr.split('|').map((size) => createAdSize(...stringToTuple(size)));

/**
* Convert a breakpoint name to a form suitable for use as an attribute
* Regex matches a lowercase letter followed by an uppercase letter
*
* e.g. `mobileLandscape` => `mobile-landscape`
*/
const breakpointNameToAttribute = (breakpointName: string): string =>
breakpointName.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();

/**
* Extract the ad sizes from the breakpoint data attributes of an ad slot
*
Expand Down
4 changes: 0 additions & 4 deletions src/display/lazy-load.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ jest.mock('display/request-bids', () => ({
requestBidsForAd: jest.fn(),
}));

jest.mock('config', () => ({
get: jest.fn(() => false),
}));

jest.mock('define/Advert', () => jest.fn(() => ({ advert: jest.fn() })));

jest.mock('lib/dfp/get-advert-by-id');
Expand Down
7 changes: 3 additions & 4 deletions src/init/consented/prepare-googletag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ jest.mock('lib/identity/api', () => ({
getGoogleTagId: jest.fn().mockResolvedValue('test-id-string'),
getUrl: jest.fn(),
}));
jest.mock('lib/analytics/beacon', () => void {});

jest.mock('lib/detect/detect-breakpoint', () => ({
getCurrentBreakpoint: jest.fn(),
hasCrossedBreakpoint: jest.fn(),
}));

jest.mock('display/display-lazy-ads', () => ({
displayLazyAds: jest.fn(),
}));
Expand Down Expand Up @@ -109,15 +109,14 @@ jest.mock('@guardian/libs', () => {
},
};
});

jest.mock(
'lodash-es/once',
() =>
<T>(fn: (...args: unknown[]) => T) =>
fn,
);
jest.mock('lib/analytics/beacon', () => ({
fire: jest.fn(),
}));

jest.mock('display/load-advert', () => ({
loadAdvert: jest.fn(),
}));
Expand Down
17 changes: 0 additions & 17 deletions src/lib/__mocks__/config.js

This file was deleted.

Empty file removed src/lib/analytics/beacon.js
Empty file.
76 changes: 0 additions & 76 deletions src/lib/analytics/mvt-cookie.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/lib/breakpoint-name-to-attribute.ts

This file was deleted.

26 changes: 9 additions & 17 deletions src/lib/commercial-features.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
import { commercialFeatures } from './commercial-features';
import { setCookie, storage } from '@guardian/libs';
import type { CommercialFeaturesConstructor } from './commercial-features';
import { commercialFeatures } from './commercial-features';
import { getCurrentBreakpoint as getCurrentBreakpoint_ } from './detect/detect-breakpoint';
import { isUserLoggedInOktaRefactor as isUserLoggedInOktaRefactor_ } from './identity/api';
import { adFreeDataIsPresent as adFreeDataIsPresent_ } from './manage-ad-free-cookie';
import userPrefs from './user-prefs';

const getCurrentBreakpoint = getCurrentBreakpoint_ as jest.MockedFunction<
typeof getCurrentBreakpoint_
>;

const adFreeDataIsPresent = adFreeDataIsPresent_ as jest.MockedFunction<
typeof adFreeDataIsPresent_
>;

const isUserLoggedInOktaRefactor =
isUserLoggedInOktaRefactor_ as jest.MockedFunction<
typeof isUserLoggedInOktaRefactor_
>;

jest.mock('./manage-ad-free-cookie', () => ({
adFreeDataIsPresent: jest.fn(),
}));

const CommercialFeatures =
commercialFeatures.constructor as CommercialFeaturesConstructor;

Expand Down Expand Up @@ -77,7 +68,9 @@ describe('Commercial features', () => {

window.location.hash = '';

userPrefs.removeSwitch('adverts');
storage.local.remove(`gu.prefs.switch.adverts`);

setCookie({ name: 'GU_AF1', value: '' });

getCurrentBreakpoint.mockReturnValue('desktop');
isUserLoggedInOktaRefactor.mockResolvedValue(true);
Expand Down Expand Up @@ -203,9 +196,8 @@ describe('Commercial features', () => {
});

describe('Article body adverts under ad-free', () => {
// LOL grammar
it('are disabled', () => {
adFreeDataIsPresent.mockReturnValue(true);
setCookie({ name: 'GU_AF1', value: '10' });
const features = new CommercialFeatures();
expect(features.articleBodyAdverts).toBe(false);
});
Expand Down Expand Up @@ -233,7 +225,7 @@ describe('Commercial features', () => {

describe('High-relevance commercial component under ad-free', () => {
beforeEach(() => {
adFreeDataIsPresent.mockReturnValue(true);
setCookie({ name: 'GU_AF1', value: '10' });
});

it('Does not run on fronts', () => {
Expand Down Expand Up @@ -292,7 +284,7 @@ describe('Commercial features', () => {

describe('Third party tags under ad-free', () => {
beforeEach(() => {
adFreeDataIsPresent.mockReturnValue(true);
setCookie({ name: 'GU_AF1', value: '10' });
});

it('Does not run by default', () => {
Expand Down Expand Up @@ -373,7 +365,7 @@ describe('Commercial features', () => {
describe('Comment adverts under ad-free', () => {
beforeEach(() => {
window.guardian.config.page.commentable = true;
adFreeDataIsPresent.mockReturnValue(true);
setCookie({ name: 'GU_AF1', value: '10' });
});

it('Does not display when page has comments', () => {
Expand Down
23 changes: 19 additions & 4 deletions src/lib/commercial-features.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { getCookie, log } from '@guardian/libs';
import { getCookie, log, storage } from '@guardian/libs';
import { getCurrentBreakpoint } from './detect/detect-breakpoint';
import { adFreeDataIsPresent } from './manage-ad-free-cookie';
import userPrefs from './user-prefs';

/**
* Log the reason why adverts are disabled
Expand All @@ -28,6 +26,20 @@ function adsDisabledLogger(
}
}

/**
* Ad free cookie helpers
*/
const AD_FREE_USER_COOKIE = 'GU_AF1';

const getAdFreeCookie = (): string | null =>
getCookie({ name: AD_FREE_USER_COOKIE });

const adFreeDataIsPresent = (): boolean => {
const cookieVal = getAdFreeCookie();
if (!cookieVal) return false;
return !Number.isNaN(parseInt(cookieVal, 10));
};

/**
* Determine whether current browser is a version of Internet Explorer
*/
Expand All @@ -43,6 +55,9 @@ const isDigitalSubscriber = (): boolean =>
const isAdFreeUser = (): boolean =>
isDigitalSubscriber() || adFreeDataIsPresent();

const isUserPrefsAdsOff = (): boolean =>
storage.local.get(`gu.prefs.switch.adverts`) === false;

// Having a constructor means we can easily re-instantiate the object in a test
class CommercialFeatures {
shouldLoadGoogletag: boolean;
Expand All @@ -63,7 +78,7 @@ class CommercialFeatures {
const noadsUrl = /[#&]noads(&.*)?$/.test(window.location.hash);
const forceAdFree = /[#&]noadsaf(&.*)?$/.test(window.location.hash);
const forceAds = /[?&]forceads(&.*)?$/.test(window.location.search);
const externalAdvertising = !noadsUrl && !userPrefs.isOff('adverts');
const externalAdvertising = !noadsUrl && !isUserPrefsAdsOff();
const sensitiveContent =
window.guardian.config.page.shouldHideAdverts ||
window.guardian.config.page.section === 'childrens-books-site';
Expand Down
5 changes: 0 additions & 5 deletions src/lib/contributions-utilities.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/lib/cookie.ts

This file was deleted.

30 changes: 0 additions & 30 deletions src/lib/manage-ad-free-cookie.ts

This file was deleted.

Loading

0 comments on commit 1273e6d

Please sign in to comment.