Skip to content

Commit

Permalink
Merge pull request #83 from FYP-2024-IQMA/SCRUM-137-Create-backend-en…
Browse files Browse the repository at this point in the history
…dpoint-for-points-system-leaderboard

Scrum 137 create backend endpoint for points system leaderboard
  • Loading branch information
mohammadfadhli authored Oct 11, 2024
2 parents 8d18dd0 + 8ed4de2 commit 84ddb6e
Show file tree
Hide file tree
Showing 16 changed files with 1,032 additions and 0 deletions.
149 changes: 149 additions & 0 deletions backend/__tests__/controllers/accountsGamificationController.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// accountsGamificationController.test.js

const request = require("supertest");
const express = require("express");
const accountsGamificationController = require("../../dist/controllers/accountsGamificationController");
const accountsGamificationService = require("../../dist/services/accountsGamificationService");
const accountsGamificationRouter = require("../../dist/routes/accountsGamificationRouter").default;
const supabase = require("../../dist/config/supabaseConfig");

jest.mock("../../dist/config/supabaseConfig", () => ({
from: jest.fn(),
}));

jest.mock("../../dist/services/accountsGamificationService");

beforeEach(() => {
jest.resetAllMocks();
});

const app = express();
app.use(express.json());
app.use("/accounts", accountsGamificationRouter);

describe("GET /accounts/leaderboard", () => {
it("should return 200 and the list of accounts on success", async () => {
const mockAccounts = [
{
rank: 1,
name: "test USER",
points: 150,
},
{
rank: 2,
name: "test 3",
points: 100,
},
{
rank: 3,
name: "test 4",
points: 80,
},
{
rank: 4,
name: "test 5",
points: 50,
},
{
rank: 5,
name: "test 6",
points: 20,
},
];

accountsGamificationService.getTop5Accounts.mockResolvedValue(mockAccounts);

const response = await request(app).get(`/accounts/leaderboard/1`);

expect(response.status).toBe(200);
expect(response.body).toEqual(mockAccounts);
expect(accountsGamificationService.getTop5Accounts).toHaveBeenCalledTimes(1);
});

it("should return 500 and an error message on failure", async () => {
const mockError = new Error("Database error");

accountsGamificationService.getTop5Accounts.mockRejectedValue(mockError);

const response = await request(app).get(`/accounts/leaderboard/1`);

expect(response.status).toBe(500);
expect(accountsGamificationService.getTop5Accounts).toHaveBeenCalledTimes(1);
});
});

describe("GET /accounts/gamificationdata", () => {
const mockAccounts = {
userID: "2",
points: 0,
streaks: 0,
lastUnitCompletionDate: expect.anything(),
};

it("should return 200 and the account on success", async () => {
accountsGamificationService.getGamificationData.mockResolvedValue(mockAccounts);

const response = await request(app).get(
`/accounts/gamificationdata/${mockAccounts.userID}`
);

expect(response.status).toBe(200);
expect(response.body).toEqual(mockAccounts);
expect(accountsGamificationService.getGamificationData).toHaveBeenCalledTimes(1);
});

it("should return 500 and an error message on failure", async () => {
const mockError = new Error("Database error");

accountsGamificationService.getGamificationData.mockRejectedValue(mockError);

const response = await request(app).get(
`/accounts/gamificationdata/${mockAccounts.userID}`
);

expect(response.status).toBe(500);
expect(accountsGamificationService.getGamificationData).toHaveBeenCalledTimes(1);
});
});

describe("PATCH /accounts/updatepoints", () => {
const mockAccount = {
userID: "1",
points: 100,
};

it("should update an account and return 204 on success", async () => {
const mockResponse = { status: 204, statusText: "OK" };

accountsGamificationService.updatePoints.mockResolvedValue(mockResponse);

const response = await request(app)
.patch("/accounts/updatepoints")
.send(mockAccount);

expect(response.status).toBe(200);
expect(response.body.statusText).toBe("Points Updated Successfully");
expect(accountsGamificationService.updatePoints).toHaveBeenCalledTimes(1);
expect(accountsGamificationService.updatePoints).toHaveBeenCalledWith(
mockAccount.userID,
mockAccount.points
);
});

it("should return 500 and an error message on failure", async () => {
const mockError = new Error("Database error");

accountsGamificationService.updatePoints.mockRejectedValue(mockError);

const response = await request(app)
.patch("/accounts/updatepoints")
.send(mockAccount);

expect(response.status).toBe(500);
expect(accountsGamificationService.updatePoints).toHaveBeenCalledTimes(1);
expect(accountsGamificationService.updatePoints).toHaveBeenCalledWith(
mockAccount.userID,
mockAccount.points
);
});
});
Loading

0 comments on commit 84ddb6e

Please sign in to comment.