( routes: Record<string, MockFetchResponse>, defaultResponse?: MockFetchResponse )
| 75 | * ``` |
| 76 | */ |
| 77 | export function createMultiMockFetch( |
| 78 | routes: Record<string, MockFetchResponse>, |
| 79 | defaultResponse?: MockFetchResponse |
| 80 | ) { |
| 81 | return vi.fn(async (url: string | URL | Request, _init?: RequestInit) => { |
| 82 | const urlString = url instanceof Request ? url.url : url.toString() |
| 83 | |
| 84 | // Find matching route (exact or partial match) |
| 85 | const matchedRoute = Object.keys(routes).find( |
| 86 | (route) => urlString === route || urlString.includes(route) |
| 87 | ) |
| 88 | |
| 89 | if (matchedRoute) { |
| 90 | return createMockResponse(routes[matchedRoute]) |
| 91 | } |
| 92 | |
| 93 | if (defaultResponse) { |
| 94 | return createMockResponse(defaultResponse) |
| 95 | } |
| 96 | |
| 97 | return createMockResponse({ status: 404, json: { error: 'Not Found' } }) |
| 98 | }) |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Sets up global fetch mock. |
nothing calls this directly
no test coverage detected