From 2fb2ed88ea3a4a22a7515d36bb300c96e444a8a9 Mon Sep 17 00:00:00 2001 From: xyzuan Date: Mon, 2 Dec 2024 14:27:09 +0700 Subject: [PATCH] test(): Add more unit test --- bunfig.toml | 2 + src/utils/mockUtils.ts | 8 ++++ test/auth.test.ts | 83 ++++++++++++++++++++++++++++++++++++++++++ test/chat.test.ts | 69 +++++++++++++++++++++++++++++++++++ 4 files changed, 162 insertions(+) create mode 100644 bunfig.toml create mode 100644 src/utils/mockUtils.ts create mode 100644 test/chat.test.ts diff --git a/bunfig.toml b/bunfig.toml new file mode 100644 index 0000000..8df14d2 --- /dev/null +++ b/bunfig.toml @@ -0,0 +1,2 @@ +[test] +coverage = true \ No newline at end of file diff --git a/src/utils/mockUtils.ts b/src/utils/mockUtils.ts new file mode 100644 index 0000000..a6f699c --- /dev/null +++ b/src/utils/mockUtils.ts @@ -0,0 +1,8 @@ +const generateRandomEmail = () => { + const randomString = Math.random().toString(36).substring(2, 12); // Random alphanumeric string + const domains = ["example.com", "test.com", "email.com", "mock.com"]; + const randomDomain = domains[Math.floor(Math.random() * domains.length)]; + return `${randomString}@${randomDomain}`; +}; + +export { generateRandomEmail }; diff --git a/test/auth.test.ts b/test/auth.test.ts index 89e76e8..f1d9909 100644 --- a/test/auth.test.ts +++ b/test/auth.test.ts @@ -1,5 +1,6 @@ import { describe, expect, it } from "bun:test"; import { api } from "../src"; +import { generateRandomEmail } from "@utils/mockUtils"; describe("/v2/login Auth Sign In", () => { it("If Email / Password cant be null", async () => { @@ -61,3 +62,85 @@ describe("/v2/login Auth Sign In", () => { expect(response.body.message).toBe("Password is invalid."); }); }); + +describe("/v2/signup Auth Sign Up", () => { + it("should return 201 if user can register with valid data", async () => { + const response = await api + .handle( + new Request("http://localhost:3121/v2/auth/signup", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + email: generateRandomEmail(), + password: "password123", + name: "Random User", + }), + }) + ) + .then(async (res) => ({ + status: res.status, + body: await res.json(), + })); + + expect(response.status).toBe(201); + }); + + it("should return 422 if email or password is missing", async () => { + const response = await api + .handle( + new Request("http://localhost:3121/v2/auth/signup", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + name: "Test User", + }), + }) + ) + .then(async (res) => ({ + status: res.status, + body: await res.json(), + })); + + expect(response.status).toBe(422); + }); + + it("should return 422 if password is missing", async () => { + const response = await api + .handle( + new Request("http://localhost:3121/v2/auth/signup", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + email: "test@example.com", + name: "Test User", + }), + }) + ) + .then(async (res) => ({ + status: res.status, + body: await res.json(), + })); + + expect(response.status).toBe(422); + }); + + it("should return 422 if email is missing", async () => { + const response = await api + .handle( + new Request("http://localhost:3121/v2/auth/signup", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + password: "password123", + name: "Test User", + }), + }) + ) + .then(async (res) => ({ + status: res.status, + body: await res.json(), + })); + + expect(response.status).toBe(422); + }); +}); diff --git a/test/chat.test.ts b/test/chat.test.ts new file mode 100644 index 0000000..c0a226e --- /dev/null +++ b/test/chat.test.ts @@ -0,0 +1,69 @@ +import { describe, expect, it } from "bun:test"; +import { api } from "../src"; + +describe("Chat Modules", () => { + it("Show chat item, chat information & user information", async () => { + const response = await api + .handle(new Request("http://localhost:3121/v2/message")) + .then(async (res) => await res.json()); + + expect(response).toHaveProperty("status", 200); + expect(response).toHaveProperty("data"); + expect(Array.isArray(response.data)).toBe(true); + + if (response.data.length > 0) { + const message = response.data[0]; + + expect(message).toHaveProperty("id"); + expect(message).toHaveProperty("user"); + expect(message).toHaveProperty("mentionedBy"); + expect(message).toHaveProperty("mentionedTo"); + expect(message).toHaveProperty("createdAt"); + + expect(message.user).toHaveProperty("id"); + expect(message.user).toHaveProperty("name"); + expect(message.user).toHaveProperty("email"); + expect(message.user).toHaveProperty("iconUrl"); + expect(message.user).toHaveProperty("isAdmin"); + + if (message?.mentionedTo?.length > 0) { + const mentioned = message.mentionedTo[0]; + expect(mentioned).toHaveProperty("id"); + expect(mentioned).toHaveProperty("message"); + expect(mentioned).toHaveProperty("user"); + expect(mentioned.user).toHaveProperty("name"); + } + } + }); +}); + +// it("Should create a message and receive it via WebSocket", async (done) => { +// const ws = treaty("localhost:3000"); +// const chat = api.subscribe() + +// ws.onopen = () => { +// ws.send( +// JSON.stringify({ +// action: "CREATE", +// message: "Hello, world!", +// }) +// ); +// }; + +// ws.onmessage = (event) => { +// const data = JSON.parse(event.data); + +// expect(data).toHaveProperty("id"); +// expect(data).toHaveProperty("message", "Hello, world!"); +// expect(data).toHaveProperty("user"); + +// ws.close(); +// done(); +// }; + +// ws.onerror = (err) => { +// ws.close(); +// done(err); +// }; +// }); +// });