Skip to content

Commit

Permalink
test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Juanito98 committed Nov 5, 2024
1 parent 9a0718a commit dbacd5b
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 54 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 0 additions & 33 deletions src/__tests__/api/scheduleMentoria.test.ts

This file was deleted.

26 changes: 12 additions & 14 deletions src/__tests__/api/upsertParticipation.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { cascadeDelete } from "prisma-cascade-delete";
import { describe, it, expect, beforeEach, beforeAll } from "vitest";
import { mockEmailer } from "./mocks/emailer";
import {
Expand All @@ -13,6 +14,8 @@ import { prisma } from "@/lib/prisma";
import { hashPassword } from "@/lib/hashPassword";
import { toISOStringReg } from "@/lib/validators/date";
import {
cleanParticipation,
insertAndCheckSuccessfullyDummyParticipation,
validMailingAddressInput,
validOfmi,
validUserInput,
Expand Down Expand Up @@ -44,16 +47,7 @@ beforeAll(async () => {
});

beforeEach(async () => {
// Remove contestant participation of dummy email
await prisma.contestantParticipation.deleteMany({
where: {
Participation: { every: { user: { UserAuth: { email: dummyEmail } } } },
},
});
// Remove participation of dummy email
await prisma.participation.deleteMany({
where: { user: { UserAuth: { email: dummyEmail } } },
});
await cleanParticipation(dummyEmail);
// Remover contestant participation
mockEmailer.resetMock();
});
Expand Down Expand Up @@ -109,11 +103,15 @@ describe("/api/ofmi/registerParticipation API Endpoint", () => {
expect(participationModel).not.toBeNull();
});

it("should register volunteer", async () => {
await insertAndCheckSuccessfullyDummyParticipation(dummyEmail, "VOLUNTEER");
});

it("should update", async () => {
const { req, res } = mockRequestResponse({ body: validRequest });
await upsertParticipationHandler(req, res);
expect(res.statusCode).toBe(201);
expect(res.getHeaders()).toEqual({ "content-type": "application/json" });
const res = await insertAndCheckSuccessfullyDummyParticipation(
dummyEmail,
"CONTESTANT",
);
const participation = res._getJSONData()["participation"];

const newFirstName = "Other Name";
Expand Down
92 changes: 91 additions & 1 deletion src/__tests__/api/upsertParticipationUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { JSONValue } from "@/types/json";
import {
createMocks,
RequestMethod,
createRequest,
createResponse,
} from "node-mocks-http";
import type { NextApiRequest, NextApiResponse } from "next";
import { expect } from "vitest";
import { ParticipationRole } from "@prisma/client";
import upsertParticipationHandler from "@/pages/api/ofmi/upsertParticipation";
import { prisma } from "@/lib/prisma";

type ApiRequest = NextApiRequest & ReturnType<typeof createRequest>;
type APiResponse = NextApiResponse & ReturnType<typeof createResponse>;

export const validOfmi = {
edition: 1,
Expand Down Expand Up @@ -44,3 +56,81 @@ export const validUserParticipationInput = {
schoolCountry: "MEX",
schoolState: "Aguascalientes",
};

export const validVolunteerParticipationInput = {
role: ParticipationRole.VOLUNTEER,
educationalLinkageOptIn: true,
fundraisingOptIn: true,
communityOptIn: true,
trainerOptIn: true,
problemSetterOptIn: true,
mentorOptIn: true,
};

export function mockRequestResponse({
method = "POST",
body,
}: {
method?: RequestMethod;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
body: any;
}): {
req: ApiRequest;
res: APiResponse;
} {
const { req, res } = createMocks<ApiRequest, APiResponse>({
method,
headers: {
"Content-Type": "application/json",
},
body: body,
});
return { req, res };
}

export async function cleanParticipation(email: string): Promise<void> {
// Remove contestant participation of dummy email
await prisma.contestantParticipation.deleteMany({
where: {
Participation: { every: { user: { UserAuth: { email } } } },
},
});

// Remove volunteer participation
await prisma.volunteerParticipation.deleteMany({
where: {
Participation: { every: { user: { UserAuth: { email } } } },
},
});

// Remove participation of dummy email
await prisma.participation.deleteMany({
where: { user: { UserAuth: { email } } },
});

// Remove user
await prisma.user.deleteMany({
where: { UserAuth: { email } },
});
}

export async function insertAndCheckSuccessfullyDummyParticipation(
email: string,
role: ParticipationRole,
): Promise<APiResponse> {
const validRequest = {
ofmiEdition: validOfmi.edition,
user: validUserInput(email),
userParticipation:
role === "CONTESTANT"
? validUserParticipationInput
: validVolunteerParticipationInput,
};
const { req, res } = mockRequestResponse({ body: validRequest });
await upsertParticipationHandler(req, res);

console.log(res._getJSONData());
expect(res.statusCode).toBe(201);
expect(res.getHeaders()).toEqual({ "content-type": "application/json" });
return res;
}

0 comments on commit dbacd5b

Please sign in to comment.