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

Replace fetch mock with jest fetch mock #11850

Merged
merged 12 commits into from
Aug 19, 2024
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed .yarn/cache/tr46-npm-1.0.1-9547f343a4-6e80d75480.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@
"eslint-plugin-prettier": "5.1.3",
"eslint-plugin-react": "7.34.2",
"eslint-plugin-react-hooks": "4.6.2",
"fetch-mock": "9.11.0",
"glob": "10.4.1",
"husky": "9.0.11",
"inspectpack": "4.7.1",
Expand Down
39 changes: 22 additions & 17 deletions src/app/components/MostRead/Amp/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FetchMock } from 'jest-fetch-mock';

import React from 'react';
import fetchMock from 'fetch-mock';
import { render, act } from '../../react-testing-library-with-providers';
import { ServiceContextProvider } from '../../../contexts/ServiceContext';
import AmpMostRead from '.';
Expand All @@ -16,15 +17,17 @@ const MostReadAmpWithContext = ({ service }: MostReadAmpWithContextProps) => (
</ServiceContextProvider>
);

const fetchMock = fetch as FetchMock;

describe('AmpMostRead', () => {
afterEach(() => {
fetchMock.restore();
fetchMock.resetMocks();
});

// Testing is done in cypress as jest dom does not support/run web workers which run the amp-scripts.

it('should render as expected', async () => {
fetchMock.mock('localhost:7080/mundo/mostread.json', mundoMostReadResponse);
fetchMock.mockResponseOnce(JSON.stringify(mundoMostReadResponse));

const { container } = render(<MostReadAmpWithContext service="mundo" />, {
service: 'mundo',
Expand All @@ -39,9 +42,7 @@ describe('AmpMostRead', () => {
});

it('should render fallback when fetch fails to load', async () => {
fetchMock.mock('localhost:7080/mundo/mostread.json', {
throws: 'failed fetch',
});
fetchMock.mockReject(Error('failed fetch'));

const { container, getByText } = render(
<MostReadAmpWithContext service="mundo" />,
Expand All @@ -62,12 +63,14 @@ describe('AmpMostRead', () => {
});

it('should render fallback when items are empty', async () => {
fetchMock.mock('localhost:7080/mundo/mostread.json', {
generated: '2022-05-03T14:44:35.496Z',
lastRecordTimeStamp: '2022-05-03T14:42:00Z',
firstRecordTimeStamp: '2022-05-03T14:27:00Z',
items: [],
});
fetchMock.mockResponseOnce(
JSON.stringify({
generated: '2022-05-03T14:44:35.496Z',
lastRecordTimeStamp: '2022-05-03T14:42:00Z',
firstRecordTimeStamp: '2022-05-03T14:27:00Z',
items: [],
}),
);

const { container, getByText } = render(
<MostReadAmpWithContext service="mundo" />,
Expand All @@ -88,11 +91,13 @@ describe('AmpMostRead', () => {
});

it('should render fallback when items are undefined', async () => {
fetchMock.mock('localhost:7080/mundo/mostread.json', {
generated: '2022-05-03T14:44:35.496Z',
lastRecordTimeStamp: '2022-05-03T14:42:00Z',
firstRecordTimeStamp: '2022-05-03T14:27:00Z',
});
fetchMock.mockResponseOnce(
JSON.stringify({
generated: '2022-05-03T14:44:35.496Z',
lastRecordTimeStamp: '2022-05-03T14:42:00Z',
firstRecordTimeStamp: '2022-05-03T14:27:00Z',
}),
);

const { container, getByText } = render(
<MostReadAmpWithContext service="mundo" />,
Expand Down
2 changes: 0 additions & 2 deletions src/app/components/MostRead/Canonical/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import fetchMock from 'fetch-mock';
import { data as arabicMostReadData } from '../../../../../data/arabic/mostRead/index.json';
import { data as pidginMostReadData } from '../../../../../data/pidgin/mostRead/index.json';
import { data as kyrgyzMostReadData } from '../../../../../data/kyrgyz/mostRead/index.json';
Expand Down Expand Up @@ -50,7 +49,6 @@ const MostReadCanonicalWithContext = ({
describe('MostRead Canonical', () => {
beforeEach(() => {
jest.clearAllMocks();
fetchMock.restore();
});

[
Expand Down
72 changes: 40 additions & 32 deletions src/app/legacy/containers/RadioSchedule/Canonical/index.test.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import fetchMock from 'fetch-mock';
import arabicRadioScheduleData from '#data/arabic/bbc_arabic_radio/schedule.json';
import { RequestContextProvider } from '#contexts/RequestContext';
import { FRONT_PAGE } from '#app/routes/utils/pageTypes';
Expand Down Expand Up @@ -33,12 +32,15 @@ const RadioScheduleWithContext = ({ initialData, lang }) => (

describe('Canonical RadioSchedule', () => {
beforeEach(() => {
fetchMock.restore();
fetch.resetMocks();
});

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

describe('With initial data', () => {
it('renders correctly for a service', async () => {
fetchMock.mock(endpoint, arabicRadioScheduleData);
const initialData = processRadioSchedule(
arabicRadioScheduleData,
'arabic',
Expand All @@ -51,7 +53,6 @@ describe('Canonical RadioSchedule', () => {
).container;
});
expect(container).toMatchSnapshot();
expect(fetchMock.calls(endpoint).length).toBeFalsy();
});

it('contains four programs for a service with a radio schedule', async () => {
Expand All @@ -63,32 +64,38 @@ describe('Canonical RadioSchedule', () => {
let container;

await act(async () => {
container = await render(
container = render(
<RadioScheduleWithContext initialData={initialData} />,
).container;
});
expect(container.querySelectorAll('li').length).toEqual(4);
});

it('does not render when data contains less than 4 programs', async () => {
fetchMock.mock(endpoint, arabicRadioScheduleData.schedules.slice(0, 2));
const radioSchedule2Programmes = { ...arabicRadioScheduleData };
radioSchedule2Programmes.schedules =
radioSchedule2Programmes.schedules.slice(0, 2);

const initialData = processRadioSchedule(
{ schedules: arabicRadioScheduleData.schedules.slice(0, 2) },
radioSchedule2Programmes,
'arabic',
Date.now(),
);

fetch.mockResponseOnce(JSON.stringify(radioSchedule2Programmes));

let container;

await act(async () => {
container = await render(
container = render(
<RadioScheduleWithContext initialData={initialData} />,
).container;
});
expect(container).toBeEmptyDOMElement();
});

it('does not render when data contains no programs', async () => {
fetchMock.mock(endpoint, []);
fetch.mockResponseOnce(JSON.stringify([]));
const initialData = processRadioSchedule(
{ schedules: [] },
'arabic',
Expand All @@ -97,7 +104,7 @@ describe('Canonical RadioSchedule', () => {
let container;

await act(async () => {
container = await render(
container = render(
<RadioScheduleWithContext initialData={initialData} />,
).container;
});
Expand All @@ -107,34 +114,32 @@ describe('Canonical RadioSchedule', () => {

describe('Without initial data', () => {
it('renders correctly for a service with a radio schedule and page frequency URL', async () => {
fetchMock.mock(endpoint, arabicRadioScheduleData);
fetch.mockResponseOnce(JSON.stringify(arabicRadioScheduleData));
let container;

await act(async () => {
container = await render(<RadioScheduleWithContext />).container;
container = render(<RadioScheduleWithContext />).container;
});

expect(container).toMatchSnapshot();
expect(fetchMock.calls(endpoint).length).toBeTruthy();
});

it('contains four programs for a service with a radio schedule', async () => {
fetchMock.mock(endpoint, arabicRadioScheduleData);
fetch.mockResponseOnce(JSON.stringify(arabicRadioScheduleData));
let container;

await act(async () => {
container = await render(<RadioScheduleWithContext />).container;
container = render(<RadioScheduleWithContext />).container;
});
expect(container.querySelectorAll('li').length).toEqual(4);
});

it('render radio schedules container with lang code', async () => {
fetchMock.mock(endpoint, arabicRadioScheduleData);
fetch.mockResponseOnce(JSON.stringify(arabicRadioScheduleData));
let container;

await act(async () => {
container = await render(<RadioScheduleWithContext lang="fa-AF" />)
.container;
container = render(<RadioScheduleWithContext lang="fa-AF" />).container;
});
expect(container.querySelector('section')).toHaveAttribute(
'lang',
Expand All @@ -143,49 +148,52 @@ describe('Canonical RadioSchedule', () => {
});

it('does not render when data contains less than 4 programs', async () => {
fetchMock.mock(endpoint, {
schedules: arabicRadioScheduleData.schedules.slice(0, 2),
});
const radioSchedule2Programmes = { ...arabicRadioScheduleData };
radioSchedule2Programmes.schedules =
radioSchedule2Programmes.schedules.slice(0, 2);

fetch.mockResponseOnce(JSON.stringify(radioSchedule2Programmes));

let container;

await act(async () => {
container = await render(<RadioScheduleWithContext />).container;
container = render(<RadioScheduleWithContext />).container;
});
expect(container).toBeEmptyDOMElement();
});

it('does not render when data contains no programs', async () => {
fetchMock.mock(endpoint, {
schedules: [],
});
fetch.mockResponseOnce(
JSON.stringify({
schedules: [],
}),
);
let container;

await act(async () => {
container = await render(<RadioScheduleWithContext />).container;
container = render(<RadioScheduleWithContext />).container;
});
expect(container).toBeEmptyDOMElement();
});

it('does not render when data fetched returns non-ok status code', async () => {
global.console.error = jest.fn();
fetchMock.mock(endpoint, 404);
fetch.mockResponse({ status: 404 });
let container;

await act(async () => {
container = await render(<RadioScheduleWithContext />).container;
container = render(<RadioScheduleWithContext />).container;
});
expect(container).toBeEmptyDOMElement();
});

it('does not render when data fetch is rejected', async () => {
global.console.error = jest.fn();
fetchMock.mock(endpoint, {
throws: 'Server not found',
});
fetch.mockRejectOnce(Error('Server not found'));
let container;

await act(async () => {
container = await render(<RadioScheduleWithContext />).container;
container = render(<RadioScheduleWithContext />).container;
});
expect(container).toBeEmptyDOMElement();
});
Expand Down
Loading
Loading