Skip to content

Commit

Permalink
add mockFetch globally for all stories
Browse files Browse the repository at this point in the history
  • Loading branch information
marjisound committed Oct 15, 2024
1 parent f4e6acd commit 4dedd9f
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 5 deletions.
3 changes: 3 additions & 0 deletions dotcom-rendering/.storybook/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { resets } from '@guardian/source/foundations';

import { Lazy } from '../src/components/Lazy';
import { Picture } from '../src/components/Picture';
import { mockFetch } from '../src/lib/mockRESTCalls';
import { setABTests } from '../src/lib/useAB';
import { ConfigContextDecorator } from './decorators/configContextDecorator';
import { Preview } from '@storybook/react';
Expand All @@ -21,6 +22,8 @@ import {
Lazy.disabled = isChromatic();
Picture.disableLazyLoading = isChromatic();

global.fetch = mockFetch;

// Fix the date to prevent false negatives
MockDate.set('Sat Jan 1 2022 12:00:00 GMT+0000 (Greenwich Mean Time)');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { mockFetch } from '../lib/mockRESTCalls';
import { GroupedNewslettersList } from './GroupedNewsletterList';
import { ManyNewsletterSignUp } from './ManyNewsletterSignUp.importable';

export const Default = () => {
global.fetch = mockFetch;
return (
<>
<GroupedNewslettersList
Expand Down
2 changes: 0 additions & 2 deletions dotcom-rendering/src/layouts/DecideLayout.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { embedIframe } from '../client/embedIframe';
import { decideFormat } from '../lib/decideFormat';
import type { ArticleFormat } from '../lib/format';
import { getCurrentPillar } from '../lib/layoutHelpers';
import { mockFetch } from '../lib/mockRESTCalls';
import { extractNAV } from '../model/extract-nav';
import type { ArticleDeprecated } from '../types/article';
import { DecideLayout, type Props as DecideLayoutProps } from './DecideLayout';
Expand All @@ -38,7 +37,6 @@ export type HydratedLayoutDecoratorArgs = {
const HydratedLayout: Decorator<
DecideLayoutProps & HydratedLayoutDecoratorArgs
> = (Story, context) => {
global.fetch = mockFetch;
const { article } = context.args;
const format: ArticleFormat = decideFormat(article.format);
const colourScheme =
Expand Down
2 changes: 1 addition & 1 deletion dotcom-rendering/src/lib/mockRESTCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const createMockResponse = (status: number, body?: any): Promise<Response> => {
return Promise.resolve(new Response(JSON.stringify(body), { status }));
};

export const mockFetch = (
export const mockFetch: typeof global.fetch = (
input: RequestInfo | URL,
requestInit?: RequestInit,
): Promise<Response> => {
Expand Down

0 comments on commit 4dedd9f

Please sign in to comment.