-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmocks.ts
More file actions
41 lines (35 loc) · 1.17 KB
/
mocks.ts
File metadata and controls
41 lines (35 loc) · 1.17 KB
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
35
36
37
38
39
40
41
import { type Mock, vi } from "vitest"
import type { OpenSeaClient } from "../src/client.js"
import type { OutputFormat } from "../src/output.js"
export type MockClient = {
get: Mock
post: Mock
getApiKeyPrefix: Mock
}
export type CommandTestContext = {
mockClient: MockClient
getClient: () => OpenSeaClient
getFormat: () => OutputFormat
consoleSpy: ReturnType<typeof vi.spyOn>
}
export function createCommandTestContext(): CommandTestContext {
const mockClient: MockClient = {
get: vi.fn(),
post: vi.fn(),
getApiKeyPrefix: vi.fn().mockReturnValue("test..."),
}
const getClient = () => mockClient as unknown as OpenSeaClient
const getFormat = () => "json" as OutputFormat
const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {})
return { mockClient, getClient, getFormat, consoleSpy }
}
export function mockFetchResponse(data: unknown, status = 200): void {
vi.spyOn(globalThis, "fetch").mockResolvedValue(
new Response(JSON.stringify(data), { status }),
)
}
export function mockFetchTextResponse(body: string, status = 200): void {
vi.spyOn(globalThis, "fetch").mockResolvedValue(
new Response(body, { status }),
)
}