generated from Kan-A-Pesh/express-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: added get daily challenges route and tests
- Loading branch information
Showing
5 changed files
with
146 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,24 @@ | ||
import ChallengesController from "@/controllers/challenges"; | ||
import Status from "@/models/status"; | ||
import { NextFunction, Request, Response } from "express"; | ||
|
||
/** | ||
* Handles the /daily/challenges route. | ||
* Retrieves the daily challenges information. | ||
* Sends a response with a status of 200 and the challenges information. | ||
*/ | ||
// export default async function Route_Daily_Challenges_Get(req: Request, res: Response, next: NextFunction) { | ||
// const challenges = null | ||
// } | ||
export default async function Route_DailyChallenges_Get(req: Request, res: Response, next: NextFunction) { | ||
const challenges = await ChallengesController.getDailyChallenges(); | ||
|
||
if (!challenges) { | ||
return Status.send(req, next, { | ||
status: 404, | ||
error: "errors.notFound" | ||
}) | ||
} | ||
|
||
return Status.send(req, next, { | ||
status: 200, | ||
data: challenges | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import { Router } from "express"; | ||
import Route_DailyChallenges_Get from "./challenges"; | ||
import Route_Daily_Read from "./read"; | ||
|
||
const dailyRouter = Router(); | ||
|
||
dailyRouter.get("/", Route_Daily_Read); | ||
// dailyRouter.get("/challenges", Route_Daily_Challenges_Get); | ||
dailyRouter.get("/challenges", Route_DailyChallenges_Get); | ||
|
||
export default dailyRouter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import createApp from "@/app"; | ||
import globals from "@/env/env"; | ||
import { toDateString } from "@/utils/date"; | ||
import { get, post } from "../utils"; | ||
|
||
const app = createApp("e2e-daily"); | ||
|
||
const testGlobals = { | ||
clubId: 0, | ||
}; | ||
|
||
describe("Daily challenges", () => { | ||
|
||
test("should create a daily club and challenge", async () => { | ||
const resClub = await post( | ||
app, | ||
"/admin/clubs", | ||
{ | ||
name: "daily club", | ||
avatarUrl: "https://placehold.co/400", | ||
description: "description", | ||
dailyDate: toDateString() // Today | ||
}, | ||
{ | ||
"X-ADMIN-KEY": globals.env.ADMIN_TOKEN | ||
} | ||
); | ||
|
||
testGlobals.clubId = resClub.body.response[0].data.id ?? "invalid"; | ||
|
||
const res = await post( | ||
app, | ||
"/admin/challenges", | ||
{ | ||
clubId: testGlobals.clubId, | ||
score: 100, | ||
name: "daily challenge" | ||
}, | ||
{ | ||
"X-ADMIN-KEY": globals.env.ADMIN_TOKEN | ||
} | ||
); | ||
|
||
expect(res.body).toStrictEqual({ | ||
masterStatus: 201, | ||
sentAt: expect.any(Number), | ||
response: [ | ||
{ | ||
status: 201, | ||
success: true, | ||
data: { | ||
id: expect.any(Number), | ||
score: 100, | ||
name: "daily challenge", | ||
clubId: testGlobals.clubId | ||
} | ||
} | ||
] | ||
}); | ||
|
||
}); | ||
|
||
|
||
test("should get daily club", async () => { | ||
const res = await get(app, "/daily"); | ||
|
||
expect(res.body).toStrictEqual({ | ||
masterStatus: 200, | ||
sentAt: expect.any(Number), | ||
response: [ | ||
{ | ||
status: 200, | ||
success: true, | ||
data: { | ||
avatarUrl: "https://placehold.co/400", | ||
name: "daily club", | ||
description: "description" | ||
} | ||
} | ||
] | ||
}); | ||
|
||
}); | ||
|
||
test("should get daily challenges", async () => { | ||
const res = await get(app, "/daily/challenges"); | ||
|
||
expect(res.body).toStrictEqual({ | ||
masterStatus: 200, | ||
sentAt: expect.any(Number), | ||
response: [ | ||
{ | ||
status: 200, | ||
success: true, | ||
data: expect.arrayContaining([ | ||
{ | ||
id: expect.any(Number), | ||
name: expect.any(String), | ||
score: expect.any(Number) | ||
} | ||
]) | ||
} | ||
] | ||
}); | ||
}); | ||
|
||
}); |