Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiodxa committed Nov 27, 2024
1 parent 4815859 commit 851d935
Showing 1 changed file with 11 additions and 53 deletions.
64 changes: 11 additions & 53 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { beforeEach, expect, mock, test } from "bun:test";
import { FormStrategy, FormStrategyVerifyParams } from ".";
import { FormStrategy } from ".";

let verify = mock();

beforeEach(() => {
verify.mockReset();
});

test("should have the name of the strategy", () => {
test("has the name of the strategy", () => {
let strategy = new FormStrategy(verify);
expect(strategy.name).toBe("form");
});

test("should pass to the verify callback a FormData object", async () => {
test("pass to the verify function a FormData object", async () => {
let body = new FormData();
body.set("email", "[email protected]");

Expand All @@ -25,10 +25,12 @@ test("should pass to the verify callback a FormData object", async () => {
expect(verify).toBeCalledWith({ form: body, request });
});

test("should return what the verify callback returned", async () => {
verify.mockImplementationOnce(async ({ form }: FormStrategyVerifyParams) => {
return form.get("email");
});
test("returns what the verify function returned", async () => {
verify.mockImplementationOnce(
async ({ form }: FormStrategy.VerifyOptions) => {
return form.get("email");
},
);

let body = new FormData();
body.set("email", "[email protected]");
Expand All @@ -42,7 +44,7 @@ test("should return what the verify callback returned", async () => {
expect(user).toBe("[email protected]");
});

test("should pass error as cause on failure", async () => {
test("thrown error passthrough the strategy", async () => {
verify.mockImplementationOnce(() => {
throw new TypeError("Invalid email address");
});
Expand All @@ -54,51 +56,7 @@ test("should pass error as cause on failure", async () => {

let strategy = new FormStrategy(verify);

let result = await strategy.authenticate(request).catch((error) => error);

expect(result).toEqual(new Error("Invalid email address"));
expect((result as Error).cause).toEqual(
expect(strategy.authenticate(request)).rejects.toThrow(
new TypeError("Invalid email address"),
);
});

test("should pass generate error from string on failure", async () => {
verify.mockImplementationOnce(() => {
throw "Invalid email address";
});

let body = new FormData();
body.set("email", "[email protected]");

let request = new Request("http://example.com/test", {
body,
method: "POST",
});

let strategy = new FormStrategy(verify);

let result = await strategy.authenticate(request).catch((error) => error);

expect(result).toEqual(new Error("Invalid email address"));
expect((result as Error).cause).toEqual(new Error("Invalid email address"));
});

test("should create Unknown error if thrown value is not Error or string", async () => {
verify.mockImplementationOnce(() => {
throw { message: "Invalid email address" };
});

let body = new FormData();
body.set("email", "[email protected]");

let request = new Request("http://.../test", { body, method: "POST" });

let strategy = new FormStrategy(verify);

let result = await strategy.authenticate(request).catch((error) => error);

expect(result).toEqual(new Error("Unknown error"));
expect((result as Error).cause).toEqual(
new Error(JSON.stringify({ message: "Invalid email address" }, null, 2)),
);
});

0 comments on commit 851d935

Please sign in to comment.