Skip to content

Commit

Permalink
test(): Add more unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
xyzuan committed Dec 2, 2024
1 parent fe3baba commit 2fb2ed8
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bunfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[test]
coverage = true
8 changes: 8 additions & 0 deletions src/utils/mockUtils.ts
Original file line number Diff line number Diff line change
@@ -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 };
83 changes: 83 additions & 0 deletions test/auth.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down Expand Up @@ -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: "[email protected]",
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);
});
});
69 changes: 69 additions & 0 deletions test/chat.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof api>("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);
// };
// });
// });

0 comments on commit 2fb2ed8

Please sign in to comment.