diff --git a/app/challenges-platform/index.ts b/app/challenges-platform/index.ts index ff4dd23..1bf1e39 100644 --- a/app/challenges-platform/index.ts +++ b/app/challenges-platform/index.ts @@ -3,6 +3,7 @@ export { ParticipantsService, ReviewsService, SubmissionService, + AccesibleChallengesService, } from "./services"; import { Transformer } from "./models"; import { transformers } from "../../config/challenges-platform/transformers"; diff --git a/test/challenges-platform/factories/accessible-challenge-factory.ts b/test/challenges-platform/factories/accessible-challenge-factory.ts new file mode 100644 index 0000000..3c7d146 --- /dev/null +++ b/test/challenges-platform/factories/accessible-challenge-factory.ts @@ -0,0 +1,32 @@ +import { challengeFactory } from "./challenge-factory"; +import { participantFactory } from "./participant-factory"; +import { + Challenge, + Participant, +} from "../../../app/challenges-platform/models"; +import { AccesibleChallengesService } from "../../../app/challenges-platform"; +import { db } from "../../../db"; +import { accessibleChallenges } from "../../../db/schema"; + +export const accessibleChallengeFactory = async ({ + challenge, + participant, + }: { + challenge?: Challenge; + participant?: Participant; + } = {}): Promise => { + const c = challenge || (await challengeFactory()); + const p = participant || (await participantFactory()); + + const insertResult = await db + .insert(accessibleChallenges) + .values({ + challengeId: c.id, + participantId: p.id, + }) + .returning(); + + const result = await AccesibleChallengesService.count(c, p); + if (!result.ok) fail("Expected result to be Ok"); + return result.val; + }; \ No newline at end of file diff --git a/test/challenges-platform/services/accessible-challenges-service.test.ts b/test/challenges-platform/services/accessible-challenges-service.test.ts new file mode 100644 index 0000000..4f819eb --- /dev/null +++ b/test/challenges-platform/services/accessible-challenges-service.test.ts @@ -0,0 +1,36 @@ +import { challengeFactory } from "../factories/challenge-factory"; +import { participantFactory } from "../factories/participant-factory"; +import { accessibleChallengeFactory } from "../factories/accessible-challenge-factory"; +import { AccesibleChallengesService } from "../../../app/challenges-platform"; + +describe("AccessibleChallengesService", () => { + describe("count", () => { + describe("when the challenge is not accessible", () => { + it("returns 0", async () => { + const challenge = await challengeFactory(); + const participant = await participantFactory(); + + const result = await AccesibleChallengesService.count(challenge, participant); + + if (!result.ok) fail("Expected result to be Ok"); + expect(result.val).toBe(0); + }); + }); + describe("when the challenge is accessible", () => { + it("returns 1", async () => { + const challenge = await challengeFactory(); + const participant = await participantFactory(); + + const insert = await accessibleChallengeFactory({ + challenge, + participant + }); + + const result = await AccesibleChallengesService.count(challenge, participant); + + if (!result.ok) fail("Expected result to be Ok"); + expect(result.val).toBe(1); + }); + }); + }) +}); \ No newline at end of file