diff --git a/dev_deps.ts b/dev_deps.ts index 3a00c34..8f97861 100644 --- a/dev_deps.ts +++ b/dev_deps.ts @@ -1,2 +1 @@ export { assertEquals } from "https://deno.land/std@0.196.0/assert/mod.ts"; -export * as mf from "https://deno.land/x/mock_fetch@0.3.0/mod.ts"; diff --git a/tests/utils.ts b/tests/utils.ts index f006108..3e1b8d8 100644 --- a/tests/utils.ts +++ b/tests/utils.ts @@ -1,6 +1,5 @@ // deno-lint-ignore-file no-explicit-any ban-types import { assertEquals } from "../dev_deps.ts"; -import { mf } from "../dev_deps.ts"; import { Client, getLagoError } from "../mod.ts"; import type { Api, @@ -27,10 +26,29 @@ type ExtractLagoResponse = E extends ( const errorMessage = "Lago Error" as const; -export function setupMockClient(route: string, handler: mf.MatchHandler) { - const { fetch, mock } = mf.sandbox(); +type LagoRoute = `${"POST" | "GET" | "PUT" | "DELETE"}@/api/v1/${string}`; +type MatchHandler = (request: Request) => Response | Promise; - mock(route, handler); +export function createMockFetch(route: LagoRoute, handler: MatchHandler) { + const [method, path] = route.split("@") as [string, string]; + let request: Request | undefined; + + const fetch = (async (input: RequestInfo | URL, init?: RequestInit) => { + request = input instanceof Request ? input : new Request(input, init); + + return await handler(request); + }) as typeof globalThis.fetch; + + return { + fetch, + expectedMethod: method, + expectedPath: path, + getRequest: () => request, + }; +} + +export function setupMockClient(route: LagoRoute, handler: MatchHandler) { + const { fetch } = createMockFetch(route, handler); return Client("api_key", { customFetch: fetch }); } @@ -51,7 +69,7 @@ export async function lagoTest< }: { testType: "error" | "200"; t: Deno.TestContext; - route: `${"POST" | "GET" | "PUT" | "DELETE"}@/api/v1/${string}`; + route: LagoRoute; clientPath: [T, U]; inputParams: ExtractLagoInput[T][U]>; responseObject?: ExtractLagoDataOrError< @@ -61,21 +79,32 @@ export async function lagoTest< urlParams?: Record; }, ) { - const client = setupMockClient( + const { fetch, getRequest, expectedMethod, expectedPath } = createMockFetch( route, - (_req) => { - if (urlParams) { - const urlSearchParams = new URLSearchParams(new URL(_req.url).search); - Object.entries(urlParams).forEach(([key, value]) => { - assertEquals(urlSearchParams.get(key), value); - }); - } + () => { return new Response( responseObject ? JSON.stringify(responseObject) : null, { status }, ); }, ); + const client = Client("api_key", { customFetch: fetch }); + + const assertRequest = () => { + const request = getRequest(); + if (!request) throw new Error("Expected a request to be sent"); + + const url = new URL(request.url); + assertEquals(request.method, expectedMethod); + assertEquals(url.pathname, expectedPath); + + if (urlParams) { + const urlSearchParams = new URLSearchParams(url.search); + Object.entries(urlParams).forEach(([key, value]) => { + assertEquals(urlSearchParams.get(key), value); + }); + } + }; switch (testType) { case "error": @@ -96,6 +125,7 @@ export async function lagoTest< (lagoError as ApiErrorUnprocessableEntity).error, errorMessage, ); + assertRequest(); } }); break; @@ -108,6 +138,7 @@ export async function lagoTest< ) as Response; assertEquals(response.status, 200); + assertRequest(); }); break;