Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TRA-15269] La quantité refusée (champ quantityRefused) est désormais obligatoire sur les BSDD #3823

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 99 additions & 42 deletions back/src/forms/__tests__/validation.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,7 @@ describe("receivedInfosSchema", () => {
const receivedInfo: ReceivedFormInput = {
wasteAcceptationStatus: "ACCEPTED",
quantityReceived: 12.5,
quantityRefused: 0,
wasteRefusalReason: "",
receivedBy: "Jim",
receivedAt: new Date("2020-01-17T10:12:00+0100"),
Expand All @@ -1014,7 +1015,8 @@ describe("receivedInfosSchema", () => {
describe("waste is refused", () => {
const receivedInfo: ReceivedFormInput = {
wasteAcceptationStatus: "REFUSED",
quantityReceived: 0,
quantityReceived: 10,
quantityRefused: 10,
wasteRefusalReason: "non conformity",
receivedBy: "Joe",
receivedAt: new Date("2020-01-17T10:12:00+0100"),
Expand Down Expand Up @@ -1042,6 +1044,7 @@ describe("receivedInfosSchema", () => {
const receivedInfo: ReceivedFormInput = {
wasteAcceptationStatus: "PARTIALLY_REFUSED",
quantityReceived: 11,
quantityRefused: 6,
wasteRefusalReason: "mixed waste",
receivedBy: "Bill",
receivedAt: new Date("2020-01-17T10:12:00+0100"),
Expand All @@ -1064,13 +1067,18 @@ describe("receivedInfosSchema", () => {
);
});

it("should be invalid when quantity received is 0", async () => {
const validateFn = () =>
receivedInfoSchema.validate({ ...receivedInfo, quantityReceived: 0 });
await expect(validateFn()).rejects.toThrow(
"Réception : le poids doit être supérieur à 0 lorsque le déchet est accepté ou accepté partiellement"
);
});
// TODO: can no longer be tested because error on quantityRefused pops first
// it("should be invalid when quantity received is 0", async () => {
// const validateFn = () =>
// receivedInfoSchema.validate({
// ...receivedInfo,
// quantityReceived: 0,
// quantityRefused: 0
// });
// await expect(validateFn()).rejects.toThrow(
// "Réception : le poids doit être supérieur à 0 lorsque le déchet est accepté ou accepté partiellement"
// );
// });
});

describe("quantityRefused", () => {
Expand All @@ -1080,7 +1088,7 @@ describe("receivedInfosSchema", () => {
signedAt: new Date("2020-01-17T10:12:00+0100")
};

it("quantityRefused is not mandatory", async () => {
it("quantityRefused is required if wasteAcceptationStatus + quantityReceived (PARTIALLY_REFUSED)", async () => {
// Given
const update = {
...form,
Expand All @@ -1089,6 +1097,60 @@ describe("receivedInfosSchema", () => {
quantityReceived: 10
};

// When
const validateFn = () => receivedInfoSchema.validate(update);

// Then
await expect(validateFn()).rejects.toThrow(
"La quantité refusée (quantityRefused) est requise"
);
});

it("quantityRefused is required if wasteAcceptationStatus + quantityReceived (ACCEPTED)", async () => {
// Given
const update = {
...form,
wasteAcceptationStatus: "ACCEPTED",
quantityReceived: 10
};

// When
const validateFn = () => receivedInfoSchema.validate(update);

// Then
await expect(validateFn()).rejects.toThrow(
"La quantité refusée (quantityRefused) est requise"
);
});

it("quantityRefused is required if wasteAcceptationStatus + quantityReceived (REFUSED)", async () => {
// Given
const update = {
...form,
wasteAcceptationStatus: "REFUSED",
wasteRefusalReason: "Because",
quantityReceived: 10
};

// When
const validateFn = () => receivedInfoSchema.validate(update);

// Then
await expect(validateFn()).rejects.toThrow(
"La quantité refusée (quantityRefused) est requise"
);
});

it("quantityRefused can be 0 if waste is REFUSED and quantityReceived = 0", async () => {
// Given
const update = {
...form,
wasteAcceptationStatus: "REFUSED",
wasteRefusalReason: "Because",
quantityReceived: 0,
quantityRefused: 0
};

// When
const isValid = await receivedInfoSchema.validate(update);

Expand All @@ -1112,24 +1174,21 @@ describe("receivedInfosSchema", () => {
);
});

it.each([null, undefined, 0])(
"waste is ACCEPTED > quantityReceived = 10 > quantityRefused can be %p",
async quantityRefused => {
// Given
const update = {
...form,
wasteAcceptationStatus: "ACCEPTED",
quantityReceived: 10,
quantityRefused
};
it("waste is ACCEPTED > quantityReceived = 10 > quantityRefused must equal 0", async () => {
// Given
const update = {
...form,
wasteAcceptationStatus: "ACCEPTED",
quantityReceived: 10,
quantityRefused: 0
};

// When
const isValid = await receivedInfoSchema.validate(update);
// When
const isValid = await receivedInfoSchema.validate(update);

// Then
expect(isValid).toBeTruthy();
}
);
// Then
expect(isValid).toBeTruthy();
});

it.each([5, 10, 15])(
"waste is ACCEPTED > quantityReceived = 10 > quantityRefused can NOT be %p",
Expand All @@ -1152,25 +1211,22 @@ describe("receivedInfosSchema", () => {
}
);

it.each([null, undefined, 10])(
"waste is REFUSED > quantityReceived = 10 > quantityRefused can be %p",
async quantityRefused => {
// Given
const update = {
...form,
wasteAcceptationStatus: "REFUSED",
wasteRefusalReason: "Reason",
quantityReceived: 10,
quantityRefused
};
it("waste is REFUSED > quantityReceived = 10 > quantityRefused must be quantityReceived", async () => {
// Given
const update = {
...form,
wasteAcceptationStatus: "REFUSED",
wasteRefusalReason: "Reason",
quantityReceived: 10,
quantityRefused: 10
};

// When
const isValid = await receivedInfoSchema.validate(update);
// When
const isValid = await receivedInfoSchema.validate(update);

// Then
expect(isValid).toBeTruthy();
}
);
// Then
expect(isValid).toBeTruthy();
});

it.each([0, 3, 15])(
"waste is REFUSED > quantityReceived = 10 > quantityRefused can NOT be %p",
Expand Down Expand Up @@ -2214,6 +2270,7 @@ describe("processedInfoSchema", () => {
const receivedInfo: ReceivedFormInput = {
wasteAcceptationStatus: "ACCEPTED",
quantityReceived: 12.5,
quantityRefused: 0,
wasteRefusalReason: "",
receivedBy: "Jim",
receivedAt: new Date("2020-01-17T10:12:00+0100"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const createAndAcceptForm = async (createOpt, acceptOpt) => {
signedBy: "Bill",
wasteAcceptationStatus: "ACCEPTED",
quantityReceived: 11,
quantityRefused: 0,
...acceptOpt
}
}
Expand Down Expand Up @@ -141,7 +142,8 @@ describe("Test Form reception", () => {
signedAt: "2019-01-17T10:22:00+0100",
signedBy: "Bill",
wasteAcceptationStatus: "ACCEPTED",
quantityReceived: 11
quantityReceived: 11,
quantityRefused: 0
}
}
});
Expand Down Expand Up @@ -290,7 +292,8 @@ describe("Test Form reception", () => {
signedAt: "2019-01-17T10:22:00+0100",
wasteAcceptationStatus: "REFUSED",
wasteRefusalReason: "Lorem ipsum",
quantityReceived: 0
quantityReceived: 0,
quantityRefused: 0
}
}
});
Expand Down Expand Up @@ -387,7 +390,8 @@ describe("Test Form reception", () => {
signedAt: "2019-01-17T10:22:00+0100",
wasteAcceptationStatus: "PARTIALLY_REFUSED",
wasteRefusalReason: "Dolor sit amet",
quantityReceived: 12.5
quantityReceived: 12.5,
quantityRefused: 7
}
}
});
Expand All @@ -399,6 +403,7 @@ describe("Test Form reception", () => {
expect(frm.signedBy).toBe("Carol");
expect(frm.wasteRefusalReason).toBe("Dolor sit amet");
expect(frm.quantityReceived?.toNumber()).toBe(12.5);
expect(frm.quantityRefused?.toNumber()).toBe(7);

// A StatusLog object is created
const logs = await prisma.statusLog.findMany({
Expand Down Expand Up @@ -455,7 +460,8 @@ describe("Test Form reception", () => {
signedAt: format(signedAt, f),
signedBy: "Bill",
wasteAcceptationStatus: WasteAcceptationStatus.ACCEPTED,
quantityReceived: 11
quantityReceived: 11,
quantityRefused: 0
}
}
});
Expand Down Expand Up @@ -540,7 +546,8 @@ describe("Test Form reception", () => {
wasteRefusalReason: "Parce que",
signedAt: "2019-01-18" as any,
signedBy: "John",
quantityReceived: 0
quantityReceived: 0,
quantityRefused: 0
}
}
}
Expand Down Expand Up @@ -702,6 +709,7 @@ describe("Test Form reception", () => {
acceptedInfo: {
wasteAcceptationStatus: "ACCEPTED",
quantityReceived: 1,
quantityRefused: 0,
signedAt: new Date("2022-01-01").toISOString() as any,
signedBy: "Collecteur annexe 1"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ describe("Test Form reception", () => {
receivedAt: "2019-01-17T10:22:00+0100",
signedAt: "2019-01-17T10:22:00+0100",
wasteAcceptationStatus: "ACCEPTED",
quantityReceived: 11
quantityReceived: 11,
quantityRefused: 0
}
}
});
Expand Down Expand Up @@ -221,7 +222,8 @@ describe("Test Form reception", () => {
receivedAt: "2019-01-17T10:22:00+0100",
signedAt: "2019-01-17T10:22:00+0100",
wasteAcceptationStatus: "ACCEPTED",
quantityReceived: 0
quantityReceived: 0,
quantityRefused: 0
}
}
});
Expand Down Expand Up @@ -377,7 +379,8 @@ describe("Test Form reception", () => {
signedAt: "2019-01-17T10:22:00+0100",
wasteAcceptationStatus: "REFUSED",
wasteRefusalReason: "Lorem ipsum",
quantityReceived: 0
quantityReceived: 0,
quantityRefused: 0
}
}
});
Expand Down Expand Up @@ -423,7 +426,8 @@ describe("Test Form reception", () => {
signedAt: "2019-01-17T10:22:00+0100",
wasteAcceptationStatus: "PARTIALLY_REFUSED",
wasteRefusalReason: "Dolor sit amet",
quantityReceived: 12.5
quantityReceived: 12.5,
quantityRefused: 7
}
}
});
Expand All @@ -435,6 +439,7 @@ describe("Test Form reception", () => {
expect(frm.receivedBy).toBe("Carol");
expect(frm.wasteRefusalReason).toBe("Dolor sit amet");
expect(frm.quantityReceived?.toNumber()).toBe(12.5);
expect(frm.quantityRefused?.toNumber()).toBe(7);

// A StatusLog object is created
const logs = await prisma.statusLog.findMany({
Expand Down Expand Up @@ -566,7 +571,8 @@ describe("Test Form reception", () => {
receivedBy: "Bill",
receivedAt: "2019-01-17T10:22:00+0100",
wasteAcceptationStatus: "ACCEPTED",
quantityReceived: 11
quantityReceived: 11,
quantityRefused: 0
}
}
});
Expand Down Expand Up @@ -635,7 +641,8 @@ describe("Test Form reception", () => {
receivedBy: "Bill",
receivedAt: "2019-01-17T10:22:00+0100",
wasteAcceptationStatus: "ACCEPTED",
quantityReceived: 11
quantityReceived: 11,
quantityRefused: 0
}
}
});
Expand Down Expand Up @@ -794,7 +801,8 @@ describe("Test Form reception", () => {
wasteRefusalReason: "Parce que",
receivedAt: "2019-01-18" as any,
receivedBy: "John",
quantityReceived: 0
quantityReceived: 0,
quantityRefused: 0
}
}
}
Expand Down Expand Up @@ -885,7 +893,8 @@ describe("Test Form reception", () => {
receivedAt: "2019-01-17T10:22:00+0100",
signedAt: "2019-01-17T10:22:00+0100",
wasteAcceptationStatus: "ACCEPTED",
quantityReceived: 50
quantityReceived: 50,
quantityRefused: 0
}
}
});
Expand Down Expand Up @@ -921,7 +930,8 @@ describe("Test Form reception", () => {
receivedAt: "2019-01-17T10:22:00+0100",
signedAt: "2019-01-17T10:22:00+0100",
wasteAcceptationStatus: "ACCEPTED",
quantityReceived: 30
quantityReceived: 30,
quantityRefused: 0
}
}
});
Expand Down Expand Up @@ -1030,7 +1040,8 @@ describe("Test Form reception", () => {
wasteAcceptationStatus: "ACCEPTED",
receivedAt: new Date("2022-01-01").toISOString() as any,
receivedBy: "John",
quantityReceived: 1
quantityReceived: 1,
quantityRefused: 0
}
}
});
Expand Down
Loading
Loading