Skip to content

Commit

Permalink
fix(tests): align new changes to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
mxdvl committed Sep 22, 2023
1 parent 84a4050 commit 2ac0f21
Show file tree
Hide file tree
Showing 13 changed files with 223 additions and 193 deletions.
9 changes: 4 additions & 5 deletions dotcom-rendering/scripts/deploy/build-riffraff-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ Promise.all([
copyDistServer(),
copyScripts(),
copyRiffRaff(),
])
.catch((err) => {
warn(err.stack);
process.exit(1);
});
]).catch((err) => {
warn(err.stack);
process.exit(1);
});
9 changes: 0 additions & 9 deletions dotcom-rendering/scripts/jest/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,3 @@ global.TextDecoder = TextDecoder as unknown as typeof global.TextDecoder;

// Mocks the version number used by CDK, we don't want our tests to fail every time we update our cdk dependency.
jest.mock('@guardian/cdk/lib/constants/tracking-tag');

// Mocks the useConfig hook in ConfigContext, so that we don't have to use the provider all the time
jest.mock('../../src/components/ConfigContext.tsx', () => {
const mockConfig = { renderingTarget: 'Web' };
return {
...jest.requireActual('../../src/components/ConfigContext.tsx'),
useConfig: () => mockConfig,
};
});
85 changes: 45 additions & 40 deletions dotcom-rendering/src/components/ArticleMeta.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ArticleDesign, ArticleDisplay, Pillar } from '@guardian/libs';
import { jest } from '@jest/globals';
import { render } from '@testing-library/react';
import { interactiveLegacyClasses } from '../layouts/lib/interactiveLegacyStyling';
import { ConfigProvider } from './ConfigContext';

jest.unstable_mockModule('../../src/lib/bridgetApi', () => ({
getNotificationsClient: jest.fn(),
Expand All @@ -18,26 +19,28 @@ describe('ArticleMeta', () => {
};

const { container } = render(
<ArticleMeta
format={format}
pageId="1234"
webTitle="A title"
byline="Observer writers"
tags={[
{
id: 'lifeandstyle/series/observer-design',
type: 'Series',
title: 'Observer Design',
},
]}
primaryDateline="primary date line"
secondaryDateline="secondary date line"
isCommentable={false}
discussionApiUrl=""
shortUrlId=""
ajaxUrl=""
showShareCount={true}
/>,
<ConfigProvider value={{ renderingTarget: 'Web' }}>
<ArticleMeta
format={format}
pageId="1234"
webTitle="A title"
byline="Observer writers"
tags={[
{
id: 'lifeandstyle/series/observer-design',
type: 'Series',
title: 'Observer Design',
},
]}
primaryDateline="primary date line"
secondaryDateline="secondary date line"
isCommentable={false}
discussionApiUrl=""
shortUrlId=""
ajaxUrl=""
showShareCount={true}
/>
</ConfigProvider>,
);

expect(
Expand All @@ -58,26 +61,28 @@ describe('ArticleMeta', () => {
};

const { container } = render(
<ArticleMeta
format={format}
pageId="1234"
webTitle="A title"
byline="Observer writers"
tags={[
{
id: 'lifeandstyle/series/observer-design',
type: 'Series',
title: 'Observer Design',
},
]}
primaryDateline="primary date line"
secondaryDateline="secondary date line"
isCommentable={false}
discussionApiUrl=""
shortUrlId=""
ajaxUrl=""
showShareCount={true}
/>,
<ConfigProvider value={{ renderingTarget: 'Web' }}>
<ArticleMeta
format={format}
pageId="1234"
webTitle="A title"
byline="Observer writers"
tags={[
{
id: 'lifeandstyle/series/observer-design',
type: 'Series',
title: 'Observer Design',
},
]}
primaryDateline="primary date line"
secondaryDateline="secondary date line"
isCommentable={false}
discussionApiUrl=""
shortUrlId=""
ajaxUrl=""
showShareCount={true}
/>
</ConfigProvider>,
);

expect(
Expand Down
61 changes: 34 additions & 27 deletions dotcom-rendering/src/components/BylineLink.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { jest } from '@jest/globals';
import { render } from '@testing-library/react';
import { getContributorTagsForToken } from '../lib/byline';
import type { TagType } from '../types/tag';
import { ConfigProvider } from './ConfigContext';

jest.unstable_mockModule('../../src/lib/bridgetApi', () => ({
getNotificationsClient: jest.fn(),
Expand Down Expand Up @@ -105,15 +106,17 @@ describe('BylineLink', () => {
];

const { container } = render(
<BylineLink
byline={byline}
tags={tags}
format={{
display: ArticleDisplay.Standard,
design: ArticleDesign.Standard,
theme: Pillar.News,
}}
/>,
<ConfigProvider value={{ renderingTarget: 'Web' }}>
<BylineLink
byline={byline}
tags={tags}
format={{
display: ArticleDisplay.Standard,
design: ArticleDesign.Standard,
theme: Pillar.News,
}}
/>
</ConfigProvider>,
);

const links = container.querySelectorAll('a');
Expand All @@ -138,15 +141,17 @@ describe('BylineLink', () => {
},
];
const { container } = render(
<BylineLink
byline={byline}
tags={tags}
format={{
display: ArticleDisplay.Standard,
design: ArticleDesign.Standard,
theme: Pillar.News,
}}
/>,
<ConfigProvider value={{ renderingTarget: 'Web' }}>
<BylineLink
byline={byline}
tags={tags}
format={{
display: ArticleDisplay.Standard,
design: ArticleDesign.Standard,
theme: Pillar.News,
}}
/>
</ConfigProvider>,
);

const links = container.querySelectorAll('a');
Expand All @@ -173,15 +178,17 @@ describe('BylineLink', () => {
];

const { container } = render(
<BylineLink
byline={byline}
tags={tags}
format={{
display: ArticleDisplay.Standard,
design: ArticleDesign.Standard,
theme: Pillar.News,
}}
/>,
<ConfigProvider value={{ renderingTarget: 'Web' }}>
<BylineLink
byline={byline}
tags={tags}
format={{
display: ArticleDisplay.Standard,
design: ArticleDesign.Standard,
theme: Pillar.News,
}}
/>
</ConfigProvider>,
);

const links = container.querySelectorAll('a');
Expand Down
7 changes: 2 additions & 5 deletions dotcom-rendering/src/components/ConfigContext.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { render } from '@testing-library/react';

// This file is globally mocked in "/dotcom-rendering/scripts/jest/setup.ts"
// so we need to explicitly override this in order to test its functionality
const { useConfig, ConfigProvider } = jest.requireActual('./ConfigContext.tsx');
import { ConfigProvider, useConfig } from './ConfigContext';

const testId = 'testId';
const TestComponent = () => {
Expand All @@ -22,7 +19,7 @@ describe('ConfigContext', () => {
});

describe('with ConfigProvider', () => {
it.each(['Web', 'Apps'])(
it.each(['Web', 'Apps'] as const)(
'provides correct context through useConfig hook with renderingTarget: "%s"',
(renderingTarget) => {
const config = { renderingTarget };
Expand Down
49 changes: 27 additions & 22 deletions dotcom-rendering/src/components/Contributor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ArticleDesign, ArticleDisplay, Pillar } from '@guardian/libs';
import { jest } from '@jest/globals';
import { render } from '@testing-library/react';
import { interactiveLegacyClasses } from '../layouts/lib/interactiveLegacyStyling';
import { ConfigProvider } from './ConfigContext';

jest.unstable_mockModule('../../src/lib/bridgetApi', () => ({
getNotificationsClient: jest.fn(),
Expand All @@ -18,17 +19,19 @@ describe('Contributor', () => {
};

const { container } = render(
<Contributor
format={format}
byline="Observer writers"
tags={[
{
id: 'lifeandstyle/series/observer-design',
type: 'Series',
title: 'Observer Design',
},
]}
/>,
<ConfigProvider value={{ renderingTarget: 'Web' }}>
<Contributor
format={format}
byline="Observer writers"
tags={[
{
id: 'lifeandstyle/series/observer-design',
type: 'Series',
title: 'Observer Design',
},
]}
/>
</ConfigProvider>,
);

expect(
Expand All @@ -44,17 +47,19 @@ describe('Contributor', () => {
};

const { container } = render(
<Contributor
format={format}
byline="Observer writers"
tags={[
{
id: 'lifeandstyle/series/observer-design',
type: 'Series',
title: 'Observer Design',
},
]}
/>,
<ConfigProvider value={{ renderingTarget: 'Web' }}>
<Contributor
format={format}
byline="Observer writers"
tags={[
{
id: 'lifeandstyle/series/observer-design',
type: 'Series',
title: 'Observer Design',
},
]}
/>
</ConfigProvider>,
);

expect(
Expand Down
44 changes: 22 additions & 22 deletions dotcom-rendering/src/components/EuropeLandingModal.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { getCookie } from '@guardian/libs';
import { getModalType } from './EuropeLandingModal.importable';
import { setCookie } from '@guardian/libs';
import { jest } from '@jest/globals';

// Mocking the getCookie function
jest.mock('@guardian/libs', () => ({
// Replace with the actual path to getCookie file
getCookie: jest.fn(),
const mockGetCookie = jest.fn<typeof import('@guardian/libs').getCookie>();
jest.unstable_mockModule('@guardian/libs', () => ({
setCookie,
getCookie: mockGetCookie,
}));

const mockGetCookie = getCookie as jest.MockedFunction<typeof getCookie>;
const { getModalType } = await import('./EuropeLandingModal.importable');

describe('getModalType', () => {
beforeEach(() => {
Expand All @@ -20,45 +20,45 @@ describe('getModalType', () => {
});

it('should return "ModalSwitched" if editionCookie is INT and country is in COE', () => {
mockGetCookie.mockImplementation((arg) => {
if (arg.name === 'GU_EDITION') return 'INT';
if (arg.name === 'GU_geo_country') return 'DE'; // Assuming 'DE' (Germany) is in COE
mockGetCookie.mockImplementation(({ name }) => {
if (name === 'GU_EDITION') return 'INT';
if (name === 'GU_geo_country') return 'DE'; // Assuming 'DE' (Germany) is in COE
return null;
});
expect(getModalType()).toEqual('ModalSwitched');
});

it('should return "ModalDoYouWantToSwitch" if editionCookie is INT, country is not in COE or UK/US/AUS', () => {
mockGetCookie.mockImplementation((arg) => {
if (arg.name === 'GU_EDITION') return 'INT';
if (arg.name === 'GU_geo_country') return 'JP'; // Assuming 'JP' (Japan) is not in COE, UK, US, or AUS
mockGetCookie.mockImplementation(({ name }) => {
if (name === 'GU_EDITION') return 'INT';
if (name === 'GU_geo_country') return 'JP'; // Assuming 'JP' (Japan) is not in COE, UK, US, or AUS
return null;
});
expect(getModalType()).toEqual('ModalDoYouWantToSwitch');
});

it('should return "ModalDoYouWantToSwitch" if editionCookie is UK/US/AUS and country is in COE', () => {
mockGetCookie.mockImplementation((arg) => {
if (arg.name === 'GU_EDITION') return 'UK';
if (arg.name === 'GU_geo_country') return 'DE'; // Assuming 'DE' (Germany) is in COE
mockGetCookie.mockImplementation(({ name }) => {
if (name === 'GU_EDITION') return 'UK';
if (name === 'GU_geo_country') return 'DE'; // Assuming 'DE' (Germany) is in COE
return null;
});
expect(getModalType()).toEqual('ModalDoYouWantToSwitch');
});

it('should return "ModalSwitched" if editionCookie is not set and country is in COE', () => {
mockGetCookie.mockImplementation((arg) => {
if (arg.name === 'GU_EDITION') return null;
if (arg.name === 'GU_geo_country') return 'DE'; // Assuming 'DE' (Germany) is in COE
mockGetCookie.mockImplementation(({ name }) => {
if (name === 'GU_EDITION') return null;
if (name === 'GU_geo_country') return 'DE'; // Assuming 'DE' (Germany) is in COE
return null;
});
expect(getModalType()).toEqual('ModalSwitched');
});

it('should return "NoModal" for all other cases', () => {
mockGetCookie.mockImplementation((arg) => {
if (arg.name === 'GU_EDITION') return 'UK';
if (arg.name === 'GU_geo_country') return 'US'; // Assuming 'US' is not in COE
mockGetCookie.mockImplementation(({ name }) => {
if (name === 'GU_EDITION') return 'UK';
if (name === 'GU_geo_country') return 'US'; // Assuming 'US' is not in COE
return null;
});
expect(getModalType()).toEqual('NoModal');
Expand Down
Loading

0 comments on commit 2ac0f21

Please sign in to comment.