|
| 1 | +import { responseFactory } from "../../fixtures/response"; |
| 2 | + |
| 3 | +const SYMBOL_NATIVE_FETCH = Symbol(); |
| 4 | +const SYMBOL_MOCKED_REQUESTS = Symbol(); |
| 5 | +// @ts-expect-error - set native reference. |
| 6 | +window[SYMBOL_NATIVE_FETCH] = window.fetch; |
| 7 | +// @ts-expect-error - set mocked requests record. |
| 8 | +window[SYMBOL_MOCKED_REQUESTS] = {}; |
| 9 | + |
| 10 | +/** |
| 11 | + * Mock fetch() function. |
| 12 | + * This is useful when using Jest tests, use storybook-addon-mock for stories |
| 13 | + * (if available). |
| 14 | + */ |
| 15 | +export function addFetchMock( |
| 16 | + mockUrl: string, |
| 17 | + mockMethod: "DELETE" | "GET" | "PATCH" | "POST" | "PUT", |
| 18 | + responseData: unknown = {}, |
| 19 | +) { |
| 20 | + window.fetch = async (...args) => { |
| 21 | + const [url, requestInit] = args; |
| 22 | + const method = requestInit?.method || "GET"; |
| 23 | + |
| 24 | + // Use mock if method and url match mock. |
| 25 | + if (url === mockUrl && method === mockMethod) { |
| 26 | + // Update window[SYMBOL_MOCKED_REQUESTS] |
| 27 | + // @ts-expect-error - set mocked requests record. |
| 28 | + window[SYMBOL_MOCKED_REQUESTS][url] = [ |
| 29 | + // @ts-expect-error - get mocked requests record. |
| 30 | + ...(window[SYMBOL_MOCKED_REQUESTS][url] || []), |
| 31 | + requestInit, |
| 32 | + ]; |
| 33 | + |
| 34 | + // Return responseData |
| 35 | + return responseFactory({ json: async () => responseData }); |
| 36 | + } |
| 37 | + |
| 38 | + // We have mocks active (so testing environment) but are making live |
| 39 | + // requests. This is probably unwanted. |
| 40 | + // NOTE: Use e2e test to test actual integration. |
| 41 | + console.warn(method, url, "unmocked!"); |
| 42 | + |
| 43 | + // @ts-expect-error - get native reference. |
| 44 | + return window[SYMBOL_NATIVE_FETCH](...args); |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Restores original fetch function. |
| 50 | + */ |
| 51 | +export function restoreNativeFetch() { |
| 52 | + // @ts-expect-error - check native implementation |
| 53 | + if (window[SYMBOL_NATIVE_FETCH]) { |
| 54 | + // @ts-expect-error - set native implementation |
| 55 | + window.fetch = window[SYMBOL_NATIVE_FETCH]; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Returns calls to mocked URL. |
| 61 | + * @param mockUrl |
| 62 | + * @param mockMethod |
| 63 | + */ |
| 64 | +export function getMockedRequests( |
| 65 | + mockUrl: string, |
| 66 | + mockMethod: "DELETE" | "GET" | "PATCH" | "POST" | "PUT", |
| 67 | +): RequestInit[] { |
| 68 | + const records: RequestInit[] = |
| 69 | + // @ts-expect-error - get mocked requests records. |
| 70 | + window[SYMBOL_MOCKED_REQUESTS][mockUrl] || []; |
| 71 | + |
| 72 | + return records.filter( |
| 73 | + (requestInit: RequestInit) => (requestInit.method || "GET") === mockMethod, |
| 74 | + ); |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Returns last call to mocked URL. |
| 79 | + * @param mockUrl |
| 80 | + * @param mockMethod |
| 81 | + */ |
| 82 | +export function getLastMockedRequest( |
| 83 | + mockUrl: string, |
| 84 | + mockMethod: "DELETE" | "GET" | "PATCH" | "POST" | "PUT", |
| 85 | +): RequestInit | undefined { |
| 86 | + const mockedRequests = getMockedRequests(mockUrl, mockMethod); |
| 87 | + return mockedRequests[mockedRequests.length - 1]; |
| 88 | +} |
0 commit comments