Skip to content

Commit fa82755

Browse files
committed
test(api): add unit tests for createCandidacyContestationCaducite
1 parent 0ea47f3 commit fa82755

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import { faker } from "@faker-js/faker/.";
2+
import { CertificationAuthorityContestationDecision } from "@prisma/client";
3+
import { prismaClient } from "../../../../prisma/client";
4+
import { createCandidacyHelper } from "../../../../test/helpers/entities/create-candidacy-helper";
5+
import { clearDatabase } from "../../../../test/jestClearDatabaseBeforeEachTestFile";
6+
import { createCandidacyContestationCaducite } from "./createCandidacyContestationCaducite";
7+
8+
describe("createCandidacyContestationCaducite", () => {
9+
beforeEach(async () => {
10+
await clearDatabase();
11+
});
12+
13+
describe("Input validation", () => {
14+
test("should fail when contestationReason is empty", async () => {
15+
const candidacy = await createCandidacyHelper();
16+
const futureDate = new Date();
17+
futureDate.setDate(futureDate.getDate() + 30);
18+
19+
const createContestationPromise = createCandidacyContestationCaducite({
20+
candidacyId: candidacy.id,
21+
contestationReason: "",
22+
readyForJuryEstimatedAt: futureDate,
23+
});
24+
25+
await expect(createContestationPromise).rejects.toThrow(
26+
"La raison de la contestation est obligatoire",
27+
);
28+
});
29+
30+
test("should fail when readyForJuryEstimatedAt is in the past", async () => {
31+
const candidacy = await createCandidacyHelper();
32+
const pastDate = new Date();
33+
pastDate.setDate(pastDate.getDate() - 1);
34+
35+
const createContestationPromise = createCandidacyContestationCaducite({
36+
candidacyId: candidacy.id,
37+
contestationReason: "Valid reason",
38+
readyForJuryEstimatedAt: pastDate,
39+
});
40+
41+
await expect(createContestationPromise).rejects.toThrow(
42+
"La date prévisionnelle ne peut pas être dans le passé",
43+
);
44+
});
45+
});
46+
47+
describe("Candidacy validation", () => {
48+
test("should fail when candidacy does not exist", async () => {
49+
const futureDate = new Date();
50+
futureDate.setDate(futureDate.getDate() + 30);
51+
52+
const createContestationPromise = createCandidacyContestationCaducite({
53+
candidacyId: faker.string.uuid(),
54+
contestationReason: "Valid reason",
55+
readyForJuryEstimatedAt: futureDate,
56+
});
57+
58+
await expect(createContestationPromise).rejects.toThrow(
59+
"La candidature n'a pas été trouvée",
60+
);
61+
});
62+
63+
test("should fail when a contestation is pending", async () => {
64+
const candidacy = await createCandidacyHelper();
65+
const futureDate = new Date();
66+
futureDate.setDate(futureDate.getDate() + 30);
67+
68+
await prismaClient.candidacyContestationCaducite.create({
69+
data: {
70+
candidacyId: candidacy.id,
71+
contestationReason: "Initial contestation",
72+
certificationAuthorityContestationDecision:
73+
CertificationAuthorityContestationDecision.DECISION_PENDING,
74+
},
75+
});
76+
77+
const createContestationPromise = createCandidacyContestationCaducite({
78+
candidacyId: candidacy.id,
79+
contestationReason: "Another contestation",
80+
readyForJuryEstimatedAt: futureDate,
81+
});
82+
83+
await expect(createContestationPromise).rejects.toThrow(
84+
"La caducité de la candidature a été confirmée ou est en attente de décision",
85+
);
86+
});
87+
88+
test("should fail when caducity has been confirmed", async () => {
89+
const candidacy = await createCandidacyHelper();
90+
const futureDate = new Date();
91+
futureDate.setDate(futureDate.getDate() + 30);
92+
93+
await prismaClient.candidacyContestationCaducite.create({
94+
data: {
95+
candidacyId: candidacy.id,
96+
contestationReason: "Initial contestation",
97+
certificationAuthorityContestationDecision:
98+
CertificationAuthorityContestationDecision.CADUCITE_CONFIRMED,
99+
},
100+
});
101+
102+
const createContestationPromise = createCandidacyContestationCaducite({
103+
candidacyId: candidacy.id,
104+
contestationReason: "Another contestation",
105+
readyForJuryEstimatedAt: futureDate,
106+
});
107+
108+
await expect(createContestationPromise).rejects.toThrow(
109+
"La caducité de la candidature a été confirmée ou est en attente de décision",
110+
);
111+
});
112+
});
113+
114+
describe("Successful creation", () => {
115+
test("should successfully create a contestation and update readyForJuryEstimatedAt", async () => {
116+
const candidacy = await createCandidacyHelper();
117+
const futureDate = new Date();
118+
futureDate.setDate(futureDate.getDate() + 30);
119+
120+
const result = await createCandidacyContestationCaducite({
121+
candidacyId: candidacy.id,
122+
contestationReason: "Valid contestation reason",
123+
readyForJuryEstimatedAt: futureDate,
124+
});
125+
126+
expect(result).toMatchObject({
127+
candidacyId: candidacy.id,
128+
contestationReason: "Valid contestation reason",
129+
});
130+
131+
const updatedCandidacy = await prismaClient.candidacy.findUnique({
132+
where: { id: candidacy.id },
133+
});
134+
expect(updatedCandidacy?.readyForJuryEstimatedAt?.getTime()).toBe(
135+
futureDate.getTime(),
136+
);
137+
});
138+
139+
test("should allow new contestation when previous one was invalidated", async () => {
140+
const candidacy = await createCandidacyHelper();
141+
const futureDate = new Date();
142+
futureDate.setDate(futureDate.getDate() + 30);
143+
144+
await prismaClient.candidacyContestationCaducite.create({
145+
data: {
146+
candidacyId: candidacy.id,
147+
contestationReason: "Initial contestation",
148+
certificationAuthorityContestationDecision:
149+
CertificationAuthorityContestationDecision.CADUCITE_INVALIDATED,
150+
},
151+
});
152+
153+
const result = await createCandidacyContestationCaducite({
154+
candidacyId: candidacy.id,
155+
contestationReason: "New valid contestation",
156+
readyForJuryEstimatedAt: futureDate,
157+
});
158+
159+
expect(result).toMatchObject({
160+
candidacyId: candidacy.id,
161+
contestationReason: "New valid contestation",
162+
});
163+
});
164+
});
165+
});

0 commit comments

Comments
 (0)