File size: 860 Bytes
fd2c364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { vi } from "vitest";

type FetchMock = ReturnType<typeof vi.fn>;

export function mockJsonResponse(data: unknown, status = 200): Response {
  return new Response(JSON.stringify(data), {
    status,
    statusText: status === 200 ? "OK" : "Error",
    headers: { "content-type": "application/json" },
  });
}

export function mockBlobResponse(
  body: BodyInit,
  contentType: string,
): Response {
  return new Response(body, {
    status: 200,
    headers: { "content-type": contentType },
  });
}

export function getFetchCall(
  fetchMock: FetchMock,
  index = 0,
): [string, RequestInit] {
  const [url, init] = fetchMock.mock.calls[index] as [string, RequestInit];
  return [url, init ?? {}];
}

export function getJsonBody(init: RequestInit): Record<string, unknown> {
  return JSON.parse(String(init.body ?? "{}")) as Record<string, unknown>;
}