Skip to content

Commit

Permalink
Merge pull request #91 from FYP-2024-IQMA/SCRUM-138-Create-backend-en…
Browse files Browse the repository at this point in the history
…dpoint-for-unit-streak

SCRUM-138-Create-backend-endpoint-for-unit-streak
  • Loading branch information
mohammadfadhli authored Oct 17, 2024
2 parents 844f924 + fcd3654 commit 20d34d8
Show file tree
Hide file tree
Showing 10 changed files with 936 additions and 217 deletions.
106 changes: 106 additions & 0 deletions backend/__tests__/controllers/accountsGamificationController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,109 @@ describe("PATCH /accounts/updatepoints", () => {
);
});
});

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

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

accountsGamificationService.updateStreaksFromLogin.mockResolvedValue(mockResponse);

const response = await request(app)
.patch(`/accounts/updatestreaksfromlogin/${mockAccounts.userID}`)
.send(mockAccount);

expect(response.status).toBe(200);
expect(response.body.statusText).toBe("Streaks Updated Successfully");
expect(accountsGamificationService.updateStreaksFromLogin).toHaveBeenCalledTimes(1);
expect(accountsGamificationService.updateStreaksFromLogin).toHaveBeenCalledWith(
mockAccount.userID,
mockAccount.lastUnitCompletionDate
);
});

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

accountsGamificationService.updateStreaksFromLogin.mockRejectedValue(mockError);

const response = await request(app)
.patch(`/accounts/updatestreaksfromlogin/${mockAccounts.userID}`)
.send(mockAccount);

expect(response.status).toBe(500);
expect(accountsGamificationService.updateStreaksFromLogin).toHaveBeenCalledTimes(1);
expect(accountsGamificationService.updateStreaksFromLogin).toHaveBeenCalledWith(
mockAccount.userID,
mockAccount.lastUnitCompletionDate
);
});
});

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

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

accountsGamificationService.updateStreaksFromUnit.mockResolvedValue(mockResponse);

const response = await request(app)
.patch(`/accounts/updatestreaksfromunit/${mockAccounts.userID}/quiz}`)
.send(mockAccount);

expect(response.status).toBe(200);
expect(response.body.statusText).toBe("Streaks Updated Successfully");
expect(accountsGamificationService.updateStreaksFromUnit).toHaveBeenCalledTimes(1);
expect(accountsGamificationService.updateStreaksFromUnit).toHaveBeenCalledWith(
mockAccount.userID,
mockAccount.lastUnitCompletionDate
);
});

it("should not update if it is not a quiz and return 204 on success", async () => {
const mockResponse = { status: 204, statusText: "OK" };

accountsGamificationService.updateStreaksFromUnit.mockResolvedValue(mockResponse);

const response = await request(app)
.patch(`/accounts/updatestreaksfromunit/${mockAccounts.userID}/lesson}`)
.send(mockAccount);

expect(response.status).toBe(200);
expect(response.body.statusText).toBe("Streaks Updated Successfully");
expect(accountsGamificationService.updateStreaksFromUnit).toHaveBeenCalledTimes(1);
expect(accountsGamificationService.updateStreaksFromUnit).toHaveBeenCalledWith(
mockAccount.userID,
mockAccount.lastUnitCompletionDate
);
});

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

accountsGamificationService.updateStreaksFromUnit.mockRejectedValue(mockError);

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

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

0 comments on commit 20d34d8

Please sign in to comment.