Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"semi": true,
"singleQuote": false,
"trailingComma": "all",
"printWidth": 80
}
34 changes: 9 additions & 25 deletions backend/src/__tests__/cars.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ describe("Cars API", () => {

describe("POST /check-license", () => {
it("should return 401 if license number is not found", async () => {
(
pool.query as Mock
).mockResolvedValue({
(pool.query as Mock).mockResolvedValue({
rows: [],
});

Expand All @@ -39,14 +37,12 @@ describe("Cars API", () => {
});
expect(pool.query).toHaveBeenCalledWith(
"SELECT id FROM drivers WHERE license_number = $1",
["12345678"]
["12345678"],
);
});

it("should return 200 if license number is found", async () => {
(
pool.query as Mock
).mockResolvedValue({
(pool.query as Mock).mockResolvedValue({
rows: [{ id: 1 }],
});

Expand All @@ -61,9 +57,7 @@ describe("Cars API", () => {

describe("POST /add", () => {
it("should return 404 if driver is not found", async () => {
(
pool.query as Mock
).mockResolvedValueOnce({ rows: [] });
(pool.query as Mock).mockResolvedValueOnce({ rows: [] });

const response = await request(app).post("/add").send({
car_model: "Tesla",
Expand All @@ -82,9 +76,7 @@ describe("Cars API", () => {
});

it("should add a car and return 200", async () => {
(
pool.query as Mock
)
(pool.query as Mock)
.mockResolvedValueOnce({ rows: [{ id: 1 }] }) // Mock driver query
.mockResolvedValueOnce({ rows: [{ id: 1 }] }); // Mock car insert

Expand Down Expand Up @@ -115,9 +107,7 @@ describe("Cars API", () => {
});

it("should return 404 if no cars are found", async () => {
(
pool.query as Mock
).mockResolvedValue({ rows: [] });
(pool.query as Mock).mockResolvedValue({ rows: [] });

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

Expand All @@ -129,9 +119,7 @@ describe("Cars API", () => {
});

it("should return cars for a valid driverId", async () => {
(
pool.query as Mock
).mockResolvedValue({
(pool.query as Mock).mockResolvedValue({
rows: [
{
car_model: "Tesla",
Expand Down Expand Up @@ -172,9 +160,7 @@ describe("Cars API", () => {
});

it("should update a car and return 200", async () => {
(
pool.query as Mock
).mockResolvedValue({
(pool.query as Mock).mockResolvedValue({
rowCount: 1,
rows: [{ id: 1, car_model: "Tesla", license_plate: "ABC123" }],
});
Expand All @@ -195,9 +181,7 @@ describe("Cars API", () => {

describe("DELETE /delete", () => {
it("should delete a car and return 200", async () => {
(
pool.query as Mock
).mockResolvedValue({
(pool.query as Mock).mockResolvedValue({
rowCount: 1,
});

Expand Down
2 changes: 1 addition & 1 deletion backend/src/__tests__/driver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe("Drivers API", () => {

expect(response.status).toBe(200);
expect(response.body.message).toBe(
"Successfully updated Driver: John Doe."
"Successfully updated Driver: John Doe.",
);
});

Expand Down
6 changes: 2 additions & 4 deletions backend/src/__tests__/profile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ describe("GET /profile/get/:id", () => {
expect(response.body.violations.length).toBe(2);
expect(response.body.cars.length).toBe(2);
expect(response.body.violations[0].description).toBe(
mockViolations[0].description
);
expect(response.body.cars[0].car_model).toBe(
mockCars[0].car_model
mockViolations[0].description,
);
expect(response.body.cars[0].car_model).toBe(mockCars[0].car_model);
});

it("should return 404 if driver is not found", async () => {
Expand Down
56 changes: 35 additions & 21 deletions backend/src/__tests__/registration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ describe("Registration API", () => {
describe("GET /get", () => {
it("should fetch all registrations successfully", async () => {
const mockRegistrations: Registration[] = [
{
user_id: 1,
license_number: "123",
school_email: "[email protected]"
{
user_id: 1,
license_number: "123",
school_email: "[email protected]",
},
{
user_id: 2,
license_number: "456",
school_email: "[email protected]"
{
user_id: 2,
license_number: "456",
school_email: "[email protected]",
},
];

Expand All @@ -71,7 +71,7 @@ describe("Registration API", () => {
expect(response.body).toEqual(mockRegistrations);
expect(pool.query).toHaveBeenCalledOnce();
expect(pool.query).toHaveBeenCalledWith(
`SELECT user_id, license_number, school_email, first_name, last_name, middle_name, date_of_birth, driver_type, sex FROM registrations`
`SELECT user_id, license_number, school_email, first_name, last_name, middle_name, date_of_birth, driver_type, sex FROM registrations`,
);
});

Expand Down Expand Up @@ -133,8 +133,15 @@ describe("Registration API", () => {
expect(pool.query).toHaveBeenCalledWith(
expect.stringContaining("INSERT INTO registrations"),
expect.arrayContaining([
"12345678", "[email protected]", "John", "Doe", "A", "1990-01-01", "Student", "M",
])
"12345678",
"[email protected]",
"John",
"Doe",
"A",
"1990-01-01",
"Student",
"M",
]),
);
});

Expand Down Expand Up @@ -177,9 +184,11 @@ describe("Registration API", () => {

it("should return 404 if registration not found", async () => {
const license_number = "12345678";
(pool.query as Mock).mockResolvedValueOnce({ rows: [] }); // No matching registration
(pool.query as Mock).mockResolvedValueOnce({ rows: [] }); // No matching registration

const response = await request(app).post("/approve").send({ license_number });
const response = await request(app)
.post("/approve")
.send({ license_number });

expect(response.status).toBe(404);
expect(response.body).toEqual({
Expand All @@ -190,36 +199,41 @@ describe("Registration API", () => {

it("should approve the registration and update driver details", async () => {
const license_number = "12345678";
const mockRegistration: Registration = { school_email: "[email protected]", user_id: 1 };
const mockRegistration: Registration = {
school_email: "[email protected]",
user_id: 1,
};
const mockDriver = { email: "", id: 1 };

// Mocking registration and driver fetch queries
(pool.query as Mock)
.mockResolvedValueOnce({ rows: [mockRegistration] }) // Mock registration fetch
.mockResolvedValueOnce({ rows: [mockDriver] }); // Mock driver fetch
.mockResolvedValueOnce({ rows: [mockRegistration] }) // Mock registration fetch
.mockResolvedValueOnce({ rows: [mockDriver] }); // Mock driver fetch

// Mock pool.connect to return a mock client
const mockClient: MockClient = {
query: vi.fn().mockResolvedValueOnce({}), // Mocking successful query
query: vi.fn().mockResolvedValueOnce({}), // Mocking successful query
release: vi.fn(),
begin: vi.fn().mockResolvedValueOnce({}),
commit: vi.fn().mockResolvedValueOnce({}),
rollback: vi.fn().mockResolvedValueOnce({}),
};
(pool.connect as Mock).mockResolvedValue(mockClient);

const response = await request(app).post("/approve").send({ license_number });
const response = await request(app)
.post("/approve")
.send({ license_number });

expect(response.status).toBe(200);
expect(response.body).toEqual({
title: "Driver Updated!",
message: "Driver's email and user_id have been updated successfully.",
});

expect(pool.query).toHaveBeenCalledTimes(3); // Expect 3 queries: registration, driver, deletion
expect(pool.query).toHaveBeenCalledTimes(3); // Expect 3 queries: registration, driver, deletion
expect(pool.connect).toHaveBeenCalledOnce();
expect(mockClient.begin).toHaveBeenCalledOnce(); // Ensure transaction start
expect(mockClient.commit).toHaveBeenCalledOnce(); // Ensure transaction commit
expect(mockClient.begin).toHaveBeenCalledOnce(); // Ensure transaction start
expect(mockClient.commit).toHaveBeenCalledOnce(); // Ensure transaction commit
});
});
});
Loading
Loading