Files
yara724-api/src/expert-claim/pipes/submit-expert-reply-validation.pipe.spec.ts
T

106 lines
3.3 KiB
TypeScript

import { SubmitExpertReplyV2Dto } from "../dto/expert-claim-v2.dto";
import { TypeOfDamage } from "src/Types&Enums/claim-request-management/type-of-damage.enum";
import { submitExpertReplyValidationPipe } from "./submit-expert-reply-validation.pipe";
describe("submitExpertReplyValidationPipe", () => {
const replacementInvoice = {
partId: 201,
typeOfDamage: TypeOfDamage.Change,
salary: "1000000",
totalPayment: "1000000",
factorNeeded: true,
daghi: { option: "فاقد ارزش" },
};
it("accepts replacement invoices without a numeric part quote", async () => {
const result = await submitExpertReplyValidationPipe.transform(
{ parts: [replacementInvoice] },
{ type: "body", metatype: SubmitExpertReplyV2Dto },
);
expect(result.parts[0].factorNeeded).toBe(true);
expect(result.parts[0].price).toBeUndefined();
});
it.each([{ factorNeeded: false }, { price: "factor" }, { price: "فاکتور" }])(
"continues rejecting absent priced quotes and nonnumeric price markers: %j",
async (invalid) => {
await expect(
submitExpertReplyValidationPipe.transform(
{ parts: [{ ...replacementInvoice, ...invalid }] },
{ type: "body", metatype: SubmitExpertReplyV2Dto },
),
).rejects.toMatchObject({
response: {
validationErrors: expect.arrayContaining([
expect.objectContaining({ field: "parts[0].price" }),
]),
},
});
},
);
it("converts DTO validation failures into Persian, field-addressable errors", async () => {
await expect(
submitExpertReplyValidationPipe.transform(
{ parts: [] },
{ type: "body", metatype: SubmitExpertReplyV2Dto },
),
).rejects.toMatchObject({
response: {
code: "EXPERT_REPLY_VALIDATION_ERROR",
message: "اطلاعات ارسالی پاسخ کارشناسی معتبر نیست.",
validationErrors: expect.arrayContaining([
expect.objectContaining({ field: "parts" }),
]),
},
});
});
it("accepts a reply without a description", async () => {
await expect(
submitExpertReplyValidationPipe.transform(
{
parts: [
{
partId: 201,
typeOfDamage: TypeOfDamage.Repair,
salary: "100000",
totalPayment: "100000",
factorNeeded: false,
},
],
},
{ type: "body", metatype: SubmitExpertReplyV2Dto },
),
).resolves.toBeInstanceOf(SubmitExpertReplyV2Dto);
});
it("identifies invalid nested price fields for the frontend", async () => {
await expect(
submitExpertReplyValidationPipe.transform(
{
description: "Damage assessment",
parts: [
{
partId: 201,
typeOfDamage: "repair",
salary: "not-a-number",
totalPayment: "100000",
factorNeeded: false,
},
],
},
{ type: "body", metatype: SubmitExpertReplyV2Dto },
),
).rejects.toMatchObject({
response: {
validationErrors: expect.arrayContaining([
{
field: "parts[0].salary",
message: "باید مبلغ صحیح و غیرمنفی به ریال باشد.",
},
]),
},
});
});
});