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

WORLDSERVICE-79: Enable Reverb on LIVE for PIDGIN #12375

Merged
merged 17 commits into from
Feb 10, 2025
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
16 changes: 16 additions & 0 deletions src/app/components/ATIAnalytics/beacon/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ describe('beacon', () => {
true,
);
});

it('should resolve reverbParams to null when Reverb is disabled for a service', () => {
sendEventBeacon({
type: 'click',
service: 'news',
componentName: 'component',
pageIdentifier: 'pageIdentifier',
detailedPlacement: 'detailedPlacement',
useReverb: false,
});

const reverbParams = sendBeaconSpy.mock.calls[0][1];

expect(sendBeaconSpy).toHaveBeenCalledTimes(1);
expect(reverbParams).toBeNull();
});
});
});
});
24 changes: 24 additions & 0 deletions src/app/components/ATIAnalytics/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1201,5 +1201,29 @@ describe('ATI Analytics Container', () => {
},
});
});

it('should set reverbParams to null when Reverb is disabled', () => {
const mockCanonical = jest.fn().mockReturnValue('canonical-return-value');
// @ts-expect-error - we need to mock these functions to ensure tests are deterministic
canonical.default = mockCanonical;

const {
metadata: { atiAnalytics },
} = articleDataNews;

render(<ATIAnalytics atiData={atiAnalytics} />, {
...defaultRenderProps,
atiData: atiAnalytics,
isAmp: false,
pageData: articleDataNews,
pageType: ARTICLE_PAGE,
service: 'mundo',
isUK: true,
});

const { reverbParams } = mockCanonical.mock.calls[0][0];

expect(reverbParams).toBeNull();
});
});
});
3 changes: 1 addition & 2 deletions src/app/lib/analyticsUtils/sendBeacon/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import isLive from '../../utilities/isLive';
import onClient from '../../utilities/onClient';
import nodeLogger from '../../logger.node';
import { ATI_LOGGING_ERROR } from '../../logger.const';
Expand Down Expand Up @@ -107,7 +106,7 @@ const callReverb = async eventDetails => {
const sendBeacon = async (url, reverbBeaconConfig) => {
if (onClient()) {
try {
if (!isLive() && reverbBeaconConfig) {
if (reverbBeaconConfig) {
const {
params: { page, user },
eventDetails,
Expand Down
56 changes: 32 additions & 24 deletions src/app/lib/analyticsUtils/sendBeacon/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable global-require */
import loggerMock from '#testHelpers/loggerMock';
import { ATI_LOGGING_ERROR } from '#app/lib/logger.const';
import sendBeacon from './index';
import * as onClient from '../../utilities/onClient';

let fetchResponse;
let isOnClient;
Expand All @@ -17,22 +19,19 @@ window.__reverb = {
__reverbLoadedPromise: Promise.resolve(reverbMock),
};

jest.spyOn(onClient, 'default').mockImplementation(() => isOnClient);

describe('sendBeacon', () => {
beforeEach(() => {
isOnClient = true;
fetch.mockImplementation(() => fetchResponse);
jest.mock('../../utilities/onClient', () => jest.fn());
const onClient = require('../../utilities/onClient');
onClient.mockImplementation(() => isOnClient);
});

afterEach(() => {
jest.clearAllMocks();
});

it(`should fetch`, () => {
const sendBeacon = require('./index').default;

sendBeacon('https://foobar.com');

expect(fetch).toHaveBeenCalledWith('https://foobar.com', {
Expand All @@ -43,8 +42,6 @@ describe('sendBeacon', () => {
it(`should not fetch when not on client`, () => {
isOnClient = false;

const sendBeacon = require('./index').default;

sendBeacon('https://foobar.com');

expect(fetch).not.toHaveBeenCalled();
Expand All @@ -61,6 +58,10 @@ describe('sendBeacon', () => {
},
};

// Simulates reverbBeaconConfig set to null in ATIAnalytics and sendEventBeacon
// in the event useReverb resolves to 'false'
const reverbConfigWhenReverbIsDisabled = null;

const originalProcessEnv = process.env;

afterEach(() => {
Expand All @@ -73,16 +74,18 @@ describe('sendBeacon', () => {
});

it('should call Reverb viewEvent if Reverb config is passed', async () => {
const sendBeacon = require('./index').default;

await sendBeacon('https://foobar.com', reverbConfig);

expect(reverbMock.viewEvent).toHaveBeenCalledTimes(1);
});

it('should not call "fetch" if Reverb config is passed', async () => {
const sendBeacon = require('./index').default;
it('should not call Reverb viewEvent if Reverb is not enabled for a service', async () => {
karinathomasbbc marked this conversation as resolved.
Show resolved Hide resolved
await sendBeacon('https://foobar.com', null);

expect(reverbMock.viewEvent).not.toHaveBeenCalled();
});

it('should not call "fetch" if Reverb config is passed', async () => {
await sendBeacon('https://foobar.com', reverbConfig);

expect(fetch).not.toHaveBeenCalled();
Expand All @@ -95,16 +98,18 @@ describe('sendBeacon', () => {
});

it('should call Reverb viewEvent if Reverb config is passed', async () => {
const sendBeacon = require('./index').default;

await sendBeacon('https://foobar.com', reverbConfig);

expect(reverbMock.viewEvent).toHaveBeenCalledTimes(1);
});

it('should not call "fetch" if Reverb config is passed', async () => {
const sendBeacon = require('./index').default;
it('should not call Reverb viewEvent if Reverb is not enabled for a service', async () => {
await sendBeacon('https://foobar.com', null);

expect(reverbMock.viewEvent).not.toHaveBeenCalled();
});

it('should not call "fetch" if Reverb config is passed', async () => {
await sendBeacon('https://foobar.com', reverbConfig);

expect(fetch).not.toHaveBeenCalled();
Expand All @@ -116,20 +121,25 @@ describe('sendBeacon', () => {
process.env.SIMORGH_APP_ENV = 'live';
});

it('should not call Reverb viewEvent if Reverb config is passed', async () => {
const sendBeacon = require('./index').default;

it('should call Reverb viewEvent if Reverb config is passed', async () => {
await sendBeacon('https://foobar.com', reverbConfig);

expect(reverbMock.viewEvent).not.toHaveBeenCalled();
expect(reverbMock.viewEvent).toHaveBeenCalled();
});

it('should call "fetch" when Reverb config is passed', async () => {
const sendBeacon = require('./index').default;
it('should not call Reverb viewEvent if Reverb is not enabled for a service', async () => {
await sendBeacon(
'https://foobar.com',
reverbConfigWhenReverbIsDisabled,
);

expect(reverbMock.viewEvent).not.toHaveBeenCalled();
});

it('should not call "fetch" when Reverb config is passed', async () => {
await sendBeacon('https://foobar.com', reverbConfig);

expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).not.toHaveBeenCalledTimes(1);
});
});
});
Expand All @@ -143,8 +153,6 @@ describe('sendBeacon', () => {
});

it(`should send error to logger`, async () => {
const sendBeacon = require('./index').default;

await sendBeacon('https://foobar.com');

expect(loggerMock.error).toHaveBeenCalledWith(ATI_LOGGING_ERROR, {
Expand Down
Loading