Skip to content

Commit

Permalink
Unit tests for cps asset transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
karinathomasbbc committed Feb 7, 2025
1 parent 43ccf11 commit 4c9beff
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 60 deletions.
175 changes: 117 additions & 58 deletions src/app/routes/homePage/getInitialData/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { Agent } from 'undici';
import KyrgyzHomeFixture from '#data/kyrgyz/homePage/index.json';
import ukChinaSimpCpsHomepage from '#data/ukchina/homePage/simp.json';
import * as fetchPageData from '#app/routes/utils/fetchPageData';
import { HOME_PAGE } from '#app/routes/utils/pageTypes';
import { CPS_ASSET, HOME_PAGE } from '#app/routes/utils/pageTypes';
import {
VISUAL_PROMINENCE,
VISUAL_STYLE,
} from '#app/models/types/curationData';
import getInitialData from '.';

process.env.BFF_PATH = 'https://mock-bff-path';
Expand All @@ -14,24 +19,27 @@ const mockGetAgent = jest.fn(() => Promise.resolve(agent));

jest.mock('../../../../server/utilities/getAgent', () => jest.fn(mockGetAgent));

describe('Home Page - BFF Fetching', () => {
const fetchDataSpy = jest.spyOn(fetchPageData, 'default');

describe('Home Page - Get Initial Data', () => {
const originalEnvironment = process.env.SIMORGH_APP_ENV;

beforeEach(() => {
fetchDataSpy.mockImplementation(() =>
Promise.resolve({
status: 200,
json: KyrgyzHomeFixture,
}),
);
});

afterEach(() => {
process.env.SIMORGH_APP_ENV = originalEnvironment;
jest.clearAllMocks();
});

it('should request local fixture data when the app env is "local"', async () => {
process.env.SIMORGH_APP_ENV = 'local';
const fetchDataSpy = jest.spyOn(fetchPageData, 'default');

fetchDataSpy.mockImplementation(() =>
Promise.resolve({
status: 200,
json: JSON.stringify(KyrgyzHomeFixture),
}),
);

await getInitialData({
path: '/kyrgyz',
Expand All @@ -47,59 +55,110 @@ describe('Home Page - BFF Fetching', () => {
});
});

it('should request BFF data when the app env is "test"', async () => {
process.env.SIMORGH_APP_ENV = 'test';

const fetchDataSpy = jest.spyOn(fetchPageData, 'default');
fetchDataSpy.mockImplementation(() =>
Promise.resolve({
status: 200,
json: JSON.stringify(KyrgyzHomeFixture),
}),
);

await getInitialData({
path: '/kyrgyz',
service: 'kyrgyz',
pageType: 'home',
getAgent: mockGetAgent,
it.each`
environment | tipoID
${'test'} | ${'cm7682qz7v1t'}
${'live'} | ${'crg7kj2e52nt'}
`(
'should request data from BFF for pageType = home when the app env is $environment',
async ({ environment, tipoID }) => {
process.env.SIMORGH_APP_ENV = environment;

await getInitialData({
path: '/kyrgyz',
service: 'kyrgyz',
pageType: 'home',
getAgent: mockGetAgent,
});

expect(fetchDataSpy).toHaveBeenCalledWith({
path: `https://mock-bff-path/?id=${tipoID}&service=kyrgyz&pageType=home&serviceEnv=${environment}`,
agent,
optHeaders: {
'ctx-service-env': environment,
},
pageType: HOME_PAGE,
});
},
);

describe('ukchina', () => {
beforeEach(() => {
fetchDataSpy.mockImplementation(() =>
Promise.resolve({
status: 200,
json: ukChinaSimpCpsHomepage,
}),
);
});

expect(fetchDataSpy).toHaveBeenCalledWith({
path: 'https://mock-bff-path/?id=cm7682qz7v1t&service=kyrgyz&pageType=home&serviceEnv=test',
agent,
optHeaders: {
'ctx-service-env': 'test',
it.each(['test', 'live'])(
'should request data from BFF for pageType = cpsAsset on %s environment',
async environment => {
process.env.SIMORGH_APP_ENV = environment;

await getInitialData({
path: '/ukchina/simp',
service: 'ukchina',
variant: 'simp',
pageType: 'home',
getAgent: mockGetAgent,
});

expect(fetchDataSpy).toHaveBeenCalledWith({
path: `https://mock-bff-path/?id=ukchina%2Fsimp%2Ffront_page&service=ukchina&pageType=cpsAsset&variant=simp&serviceEnv=${environment}`,
agent,
optHeaders: {
'ctx-service-env': environment,
},
pageType: CPS_ASSET,
});
},
pageType: HOME_PAGE,
});
});

it('should request BFF data when the app env is "live"', async () => {
process.env.SIMORGH_APP_ENV = 'live';

const fetchDataSpy = jest.spyOn(fetchPageData, 'default');
fetchDataSpy.mockImplementation(() =>
Promise.resolve({
status: 200,
json: JSON.stringify(KyrgyzHomeFixture),
}),
);

await getInitialData({
path: '/kyrgyz',
service: 'kyrgyz',
pageType: 'home',
getAgent: mockGetAgent,
});

expect(fetchDataSpy).toHaveBeenCalledWith({
path: 'https://mock-bff-path/?id=crg7kj2e52nt&service=kyrgyz&pageType=home&serviceEnv=live',
agent,
optHeaders: {
'ctx-service-env': 'live',
},
pageType: HOME_PAGE,
it('should transform data for cps asset into homepage data', async () => {
const { pageData } = await getInitialData({
path: '/ukchina/simp',
service: 'ukchina',
variant: 'simp',
pageType: 'home',
getAgent: mockGetAgent,
});

['title', 'curations', 'metadata'].forEach(property =>
expect(pageData).toHaveProperty(property),
);

expect(pageData?.curations).toHaveLength(8);
expect(pageData?.curations).toStrictEqual(
expect.arrayContaining([
expect.objectContaining({
curationId: 'av-stories-now',
curationType: 'vivo-stream',
position: 3,
title: '图辑视频',
visualStyle: VISUAL_STYLE.NONE,
visualProminence: VISUAL_PROMINENCE.NORMAL,
summaries: expect.arrayContaining([
{
description:
'为吸引年轻人移居,美国各地的小城市正在推出不同的刺激政策,有些地方的奖励高达一万五千美元。',
duration: 'PT2M53S',
id: 'urn:bbc:ares::asset:ukchina/simp/media-57554668',
imageAlt:
'莱康特·李和伴侣决定从华盛顿特区搬到俄克拉荷马的塔尔萨试试运气',
imageUrl:
'https://ichef.bbci.co.uk/ace/ws/{width}/cpsprodpb/CF69/production/_118979035_p09lw6n1.jpg.webp',
isLive: false,
lastPublished: '2021-06-21T10:31:27.000Z',
link: 'https://www.bbc.com/ukchina/simp/media-57554668',
title: '为什么美国千禧一代正在离开大城市',
type: 'video',
},
]),
}),
]),
);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MostReadData } from '#app/components/MostRead/types';
import { Curation, Summary } from '#app/models/types/curationData';
import { HOME_PAGE } from '#app/routes/utils/pageTypes';

type CpsArticle = {
metadata: {
Expand Down Expand Up @@ -117,6 +118,7 @@ export default (cpsAsset: CpsPageData) => {
return {
data: {
title,
pageType: HOME_PAGE,
curations,
metadata: {
atiAnalytics: {
Expand All @@ -126,8 +128,6 @@ export default (cpsAsset: CpsPageData) => {
pageTitle: title,
},
},
activePage: 1,
pageCount: 1,
},
};
};

0 comments on commit 4c9beff

Please sign in to comment.