diff --git a/backend/src/__tests__/driver.test.ts b/backend/src/__tests__/driver.test.ts index 1aa1298..9919475 100644 --- a/backend/src/__tests__/driver.test.ts +++ b/backend/src/__tests__/driver.test.ts @@ -1,4 +1,12 @@ -import { describe, it, expect, vi, beforeEach, MockedFunction } from "vitest"; +import { + describe, + it, + expect, + vi, + beforeEach, + MockedFunction, + Mock, +} from "vitest"; import request from "supertest"; import express from "express"; import router from "../routes/driver"; @@ -12,7 +20,9 @@ type QueryResult = { // Mock the database pool with a more specific type vi.mock("../index", () => ({ pool: { - query: vi.fn() as MockedFunction<(text: string, values: unknown[]) => Promise>, + query: vi.fn() as MockedFunction< + (text: string, values: unknown[]) => Promise + >, }, })); @@ -23,12 +33,11 @@ app.use(router); describe("Drivers API", () => { beforeEach(() => { vi.clearAllMocks(); // Clear mocks before each test - }) + }); describe("POST /add", () => { it("should return 200 if driver is added successfully", async () => { - (pool.query as vi.MockedFunction<(text: string, values: unknown[]) => Promise>) - .mockResolvedValueOnce({ rows: [{ id: 1 }] }); // Mock insert query response + (pool.query as Mock).mockResolvedValueOnce({ rows: [{ id: 1 }] }); // Mock insert query response const response = await request(app).post("/add").send({ email: "test@example.com", @@ -48,8 +57,7 @@ describe("Drivers API", () => { }); it("should return 500 if there is a server error", async () => { - (pool.query as vi.MockedFunction<(text: string, values: unknown[]) => Promise>) - .mockRejectedValueOnce(new Error("Database Error")); + (pool.query as Mock).mockRejectedValueOnce(new Error("Database Error")); const response = await request(app).post("/add").send({ email: "test@example.com", @@ -70,42 +78,40 @@ describe("Drivers API", () => { describe("GET /get", () => { it("should return 200 if drivers are fetched successfully", async () => { - (pool.query as vi.MockedFunction<(text: string, values: unknown[]) => Promise>) - .mockResolvedValueOnce({ - rows: [ - { - id: 1, - first_name: "John", - last_name: "Doe" - }, - { - id: 2, - first_name: "Jane", - last_name: "Smith" - }, - ], - }); + (pool.query as Mock).mockResolvedValueOnce({ + rows: [ + { + id: 1, + first_name: "John", + last_name: "Doe", + }, + { + id: 2, + first_name: "Jane", + last_name: "Smith", + }, + ], + }); const response = await request(app).get("/get"); expect(response.status).toBe(200); expect(response.body).toEqual([ - { - id: 1, - first_name: "John", - last_name: "Doe" + { + id: 1, + first_name: "John", + last_name: "Doe", }, - { - id: 2, - first_name: "Jane", - last_name: "Smith" + { + id: 2, + first_name: "Jane", + last_name: "Smith", }, ]); }); it("should return 500 if there is an error fetching drivers", async () => { - (pool.query as vi.MockedFunction<(text: string, values: unknown[]) => Promise>) - .mockRejectedValueOnce(new Error("Database Error")); + (pool.query as Mock).mockRejectedValueOnce(new Error("Database Error")); const response = await request(app).get("/get"); @@ -116,15 +122,14 @@ describe("Drivers API", () => { describe("PATCH /update", () => { it("should return 200 if driver is updated successfully", async () => { - (pool.query as vi.MockedFunction<(text: string, values: unknown[]) => Promise>) - .mockResolvedValueOnce({ rows: [{ id: 1 }] }); // Mock successful update + (pool.query as Mock).mockResolvedValueOnce({ rows: [{ id: 1 }] }); // Mock successful update const response = await request(app).patch("/update").send({ id: 1, email: "test@example.com", first_name: "John", last_name: "Doe", - middle_name: "A", + middle_name: "A", date_of_birth: "1990-01-01", sex: "M", driver_type: "regular", @@ -133,12 +138,13 @@ describe("Drivers API", () => { }); expect(response.status).toBe(200); - expect(response.body.message).toBe("Successfully updated Driver: John Doe."); + expect(response.body.message).toBe( + "Successfully updated Driver: John Doe." + ); }); it("should return 500 if there is an error updating the driver", async () => { - (pool.query as vi.MockedFunction<(text: string, values: unknown[]) => Promise>) - .mockRejectedValueOnce(new Error("Database Error")); + (pool.query as Mock).mockRejectedValueOnce(new Error("Database Error")); const response = await request(app).patch("/update").send({ id: 1, @@ -158,41 +164,41 @@ describe("Drivers API", () => { }); }); -// describe("DELETE /delete", () => { -// it("should return 200 if driver is deleted successfully", async () => { -// (pool.query as vi.MockedFunction<(text: string, values: unknown[]) => Promise>) -// .mockResolvedValueOnce({ rowCount: 1, rows: [{ id: 1, first_name: "John", last_name: "Doe" }] }); // Mock deletion + // describe("DELETE /delete", () => { + // it("should return 200 if driver is deleted successfully", async () => { + // (pool.query as Mock) + // .mockResolvedValueOnce({ rowCount: 1, rows: [{ id: 1, first_name: "John", last_name: "Doe" }] }); // Mock deletion -// const response = await request(app).delete("/delete").send({ -// id: 1, -// }); + // const response = await request(app).delete("/delete").send({ + // id: 1, + // }); -// expect(response.status).toBe(200); -// expect(response.body.message).toBe("Driver John, John Doe has been removed."); -// }); + // expect(response.status).toBe(200); + // expect(response.body.message).toBe("Driver John, John Doe has been removed."); + // }); -// it("should return 404 if driver is not found", async () => { -// (pool.query as vi.MockedFunction<(text: string, values: unknown[]) => Promise>) -// .mockResolvedValueOnce({ rowCount: 0 }); // No driver found for deletion + // it("should return 404 if driver is not found", async () => { + // (pool.query as Mock) + // .mockResolvedValueOnce({ rowCount: 0 }); // No driver found for deletion -// const response = await request(app).delete("/delete").send({ -// id: 1, -// }); + // const response = await request(app).delete("/delete").send({ + // id: 1, + // }); -// expect(response.status).toBe(404); -// expect(response.body.title).toBe("Not Found"); -// }); + // expect(response.status).toBe(404); + // expect(response.body.title).toBe("Not Found"); + // }); -// it("should return 500 if there is a server error during deletion", async () => { -// (pool.query as vi.MockedFunction<(text: string, values: unknown[]) => Promise>) -// .mockRejectedValueOnce( Error("Database Error")); + // it("should return 500 if there is a server error during deletion", async () => { + // (pool.query as Mock) + // .mockRejectedValueOnce( Error("Database Error")); -// const response = await request(app).delete("/delete").send({ -// id: 1, -// }); + // const response = await request(app).delete("/delete").send({ + // id: 1, + // }); -// expect(response.status).toBe(500); -// expect(response.body.title).toBe("Server Error"); -// }); -// }); + // expect(response.status).toBe(500); + // expect(response.body.title).toBe("Server Error"); + // }); + // }); });