feat: add extensive test suite
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import { describe, expect, mock, test } from "bun:test";
|
||||
import { createAuthHandlers } from "./handler";
|
||||
|
||||
describe("auth route factory", () => {
|
||||
test("creates both handlers from the provided auth instance", async () => {
|
||||
const auth = { name: "auth" };
|
||||
const get = mock(async (_request: Request) => new Response("GET"));
|
||||
const post = mock(async (_request: Request) => new Response("POST"));
|
||||
const adapter = mock((_auth: typeof auth) => ({ GET: get, POST: post }));
|
||||
const handlers = createAuthHandlers(adapter, auth);
|
||||
expect(adapter).toHaveBeenCalledWith(auth);
|
||||
expect(
|
||||
await (await handlers.GET(new Request("https://buzz.test"))).text(),
|
||||
).toBe("GET");
|
||||
expect(
|
||||
await (
|
||||
await handlers.POST(
|
||||
new Request("https://buzz.test", { method: "POST" }),
|
||||
)
|
||||
).text(),
|
||||
).toBe("POST");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,8 @@
|
||||
type AuthHandler = (request: Request) => Response | Promise<Response>;
|
||||
|
||||
export function createAuthHandlers<TAuth>(
|
||||
toHandler: (auth: TAuth) => { GET: AuthHandler; POST: AuthHandler },
|
||||
auth: TAuth,
|
||||
) {
|
||||
return toHandler(auth);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { toNextJsHandler } from "better-auth/next-js";
|
||||
import { auth } from "@/lib/auth"; // path to your auth file
|
||||
import { createAuthHandlers } from "./handler";
|
||||
|
||||
export const { POST, GET } = toNextJsHandler(auth);
|
||||
export const { POST, GET } = createAuthHandlers(toNextJsHandler, auth);
|
||||
|
||||
Reference in New Issue
Block a user