Skip to content

Commit

Permalink
Merge pull request #12324 from bbc/WSTEAMA-1237-release-reverb-pidgin…
Browse files Browse the repository at this point in the history
…-TEST

WSTEAMA-1237: Launch Reverb tracking on TEST for Pidgin
  • Loading branch information
alex-magana authored Jan 22, 2025
2 parents 5866586 + 7cc34eb commit 81f36a7
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 19 deletions.
15 changes: 14 additions & 1 deletion src/app/hooks/useClickTrackerHandler/index.test.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable jsx-a11y/click-events-have-key-events */
/* eslint-disable jsx-a11y/no-static-element-interactions */
import React from 'react';
import React, { createContext } from 'react';
import userEvent from '@testing-library/user-event';
import { waitFor } from '@testing-library/dom';
import { STORY_PAGE } from '#app/routes/utils/pageTypes';
Expand All @@ -13,6 +13,8 @@ import {
act,
fireEvent,
} from '../../components/react-testing-library-with-providers';
import * as serviceContextModule from '../../contexts/ServiceContext';

import pidginData from './fixtureData/tori-51745682.json';
import useClickTrackerHandler from '.';

Expand Down Expand Up @@ -89,6 +91,17 @@ beforeEach(() => {
assign: jest.fn(),
...rest,
};

jest.replaceProperty(
serviceContextModule,
'ServiceContext',
createContext({
atiAnalyticsProducerId: '70',
atiAnalyticsProducerName: 'PIDGIN',
service: 'pidgin',
useReverb: false,
}),
);
});

afterEach(() => {
Expand Down
20 changes: 16 additions & 4 deletions src/app/hooks/useViewTracker/index.test.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */

import React from 'react';
import React, { createContext } from 'react';
import {
renderHook,
act,
Expand All @@ -11,7 +11,8 @@ import { RequestContextProvider } from '#contexts/RequestContext';
import { ToggleContextProvider } from '#contexts/ToggleContext';
import { STORY_PAGE } from '#app/routes/utils/pageTypes';
import OPTIMIZELY_CONFIG from '#lib/config/optimizely';
import { ServiceContextProvider } from '../../contexts/ServiceContext';
import * as serviceContextModule from '../../contexts/ServiceContext';

import useViewTracker from '.';

import fixtureData from './fixtureData.json';
Expand Down Expand Up @@ -65,6 +66,17 @@ beforeEach(() => {
jest.useFakeTimers();
console.error = jest.fn();
global.IntersectionObserver = IntersectionObserver;

jest.replaceProperty(
serviceContextModule,
'ServiceContext',
createContext({
atiAnalyticsProducerId: '70',
atiAnalyticsProducerName: 'PIDGIN',
service: 'pidgin',
useReverb: false,
}),
);
});

afterEach(() => {
Expand Down Expand Up @@ -98,13 +110,13 @@ const wrapper = ({ pageData, atiData, children, toggles = defaultToggles }) => (
service="pidgin"
pathname="/pidgin/tori-51745682"
>
<ServiceContextProvider service="pidgin">
<serviceContextModule.ServiceContextProvider service="pidgin">
<ToggleContextProvider toggles={toggles}>
<EventTrackingContextProvider data={pageData} atiData={atiData}>
{children}
</EventTrackingContextProvider>
</ToggleContextProvider>
</ServiceContextProvider>
</serviceContextModule.ServiceContextProvider>
</RequestContextProvider>
);

Expand Down
3 changes: 2 additions & 1 deletion src/app/lib/analyticsUtils/sendBeacon/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
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 @@ -106,7 +107,7 @@ const callReverb = async eventDetails => {
const sendBeacon = async (url, reverbBeaconConfig) => {
if (onClient()) {
try {
if (reverbBeaconConfig) {
if (!isLive() && reverbBeaconConfig) {
const {
params: { page, user },
eventDetails,
Expand Down
78 changes: 65 additions & 13 deletions src/app/lib/analyticsUtils/sendBeacon/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('sendBeacon', () => {
});

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

it(`should fetch`, () => {
Expand Down Expand Up @@ -61,20 +61,76 @@ describe('sendBeacon', () => {
},
};

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

await sendBeacon('https://foobar.com', reverbConfig);
afterEach(() => {
process.env = originalProcessEnv;
});

describe('LOCAL', () => {
beforeEach(() => {
process.env.SIMORGH_APP_ENV = 'local';
});

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);
});

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

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

expect(fetch).not.toHaveBeenCalled();
});
});

it('should not call "fetch" if Reverb config is passed', async () => {
const sendBeacon = require('./index').default;
describe('TEST', () => {
beforeEach(() => {
process.env.SIMORGH_APP_ENV = 'test';
});

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;

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

expect(fetch).not.toHaveBeenCalled();
expect(fetch).not.toHaveBeenCalled();
});
});

describe('LIVE', () => {
beforeEach(() => {
process.env.SIMORGH_APP_ENV = 'live';
});

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

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

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

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

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

expect(fetch).toHaveBeenCalledTimes(1);
});
});
});

Expand All @@ -86,10 +142,6 @@ describe('sendBeacon', () => {
fetchResponse = Promise.reject(error);
});

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

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

Expand Down
1 change: 1 addition & 0 deletions src/app/lib/config/services/pidgin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const service: DefaultServiceConfig = {
atiAnalyticsAppName: 'news-pidgin',
atiAnalyticsProducerId: '70',
atiAnalyticsProducerName: 'PIDGIN',
useReverb: true,
chartbeatDomain: 'pidgin.bbc.co.uk',
brandName: 'BBC News Pidgin',
product: 'BBC News',
Expand Down
4 changes: 4 additions & 0 deletions src/server/Document/component.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ describe('Document Component', () => {
// uses values set in the .env file in lieu of mocked values
dotenv.config();

beforeEach(() => {
process.env.SIMORGH_APP_ENV = 'local';
});

afterEach(() => {
process.env = originalProcessEnv;
});
Expand Down

0 comments on commit 81f36a7

Please sign in to comment.