Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 73 additions & 67 deletions backend/src/__tests__/driver.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<QueryResult>>,
query: vi.fn() as MockedFunction<
(text: string, values: unknown[]) => Promise<QueryResult>
>,
},
}));

Expand All @@ -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<QueryResult>>)
.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: "[email protected]",
Expand All @@ -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<QueryResult>>)
.mockRejectedValueOnce(new Error("Database Error"));
(pool.query as Mock).mockRejectedValueOnce(new Error("Database Error"));

const response = await request(app).post("/add").send({
email: "[email protected]",
Expand All @@ -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<QueryResult>>)
.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<QueryResult>>)
.mockRejectedValueOnce(new Error("Database Error"));
(pool.query as Mock).mockRejectedValueOnce(new Error("Database Error"));

const response = await request(app).get("/get");

Expand All @@ -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<QueryResult>>)
.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: "[email protected]",
first_name: "John",
last_name: "Doe",
middle_name: "A",
middle_name: "A",
date_of_birth: "1990-01-01",
sex: "M",
driver_type: "regular",
Expand All @@ -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<QueryResult>>)
.mockRejectedValueOnce(new Error("Database Error"));
(pool.query as Mock).mockRejectedValueOnce(new Error("Database Error"));

const response = await request(app).patch("/update").send({
id: 1,
Expand All @@ -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<QueryResult>>)
// .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<QueryResult>>)
// .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<QueryResult>>)
// .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");
// });
// });
});
Loading