Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion dev_deps.ts
Original file line number Diff line number Diff line change
@@ -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";
57 changes: 44 additions & 13 deletions tests/utils.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -27,10 +26,29 @@ type ExtractLagoResponse<E> = 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<Response>;

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 });
}
Expand All @@ -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<Api<unknown>[T][U]>;
responseObject?: ExtractLagoDataOrError<
Expand All @@ -61,21 +79,32 @@ export async function lagoTest<
urlParams?: Record<string, string>;
},
) {
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":
Expand All @@ -96,6 +125,7 @@ export async function lagoTest<
(lagoError as ApiErrorUnprocessableEntity).error,
errorMessage,
);
assertRequest();
}
});
break;
Expand All @@ -108,6 +138,7 @@ export async function lagoTest<
) as Response;

assertEquals(response.status, 200);
assertRequest();
});
break;

Expand Down
Loading