Ghost/apps/admin-x-framework/test/utils/mockFetch.ts
Jono M 5e057dee11
Added tests to AdminX framework package (#19022)
refs https://github.com/TryGhost/Product/issues/4159

---

<!-- Leave the line below if you'd like GitHub Copilot to generate a
summary from your commit -->
<!--
copilot:summary
-->
### <samp>🤖[[deprecated]](https://githubnext.com/copilot-for-prs-sunset)
Generated by Copilot at 9e68f4d</samp>

This pull request refactors several components in the `admin-x-settings`
app to use common hooks from the `@tryghost/admin-x-framework` package,
which reduces code duplication and improves consistency. It also updates
the `package.json` file and adds unit tests for the `admin-x-framework`
package, which improves the formatting, testing, and dependency
management. Additionally, it makes some minor changes to the `hooks.ts`,
`FrameworkProvider.tsx`, and `.eslintrc.cjs` files in the
`admin-x-framework` package, which enhance the public API and the
linting configuration.
2023-11-20 11:00:51 +00:00

24 lines
783 B
TypeScript

import {MockContext, vi} from 'vitest';
const originalFetch = global.fetch;
type FetchArgs = Parameters<typeof global.fetch>;
export const withMockFetch = async (
{json = {}, headers = {}, status = 200, ok = true}: {json?: unknown; headers?: Record<string, string>; status?: number; ok?: boolean},
callback: (mock: MockContext<FetchArgs, Promise<Response>>) => void | Promise<void>
) => {
const mockFetch = vi.fn<FetchArgs, Promise<Response>>(() => Promise.resolve({
json: () => Promise.resolve(json),
headers: new Headers(headers),
status,
ok
} as Response));
global.fetch = mockFetch as any; // eslint-disable-line @typescript-eslint/no-explicit-any
await callback(mockFetch.mock);
global.fetch = originalFetch;
};