diff --git a/modules/customer-invoices/src/web/proformas/update/entities/proforma-update-form.schema.test.ts b/modules/customer-invoices/src/web/proformas/update/entities/proforma-update-form.schema.test.ts new file mode 100644 index 00000000..969dd807 --- /dev/null +++ b/modules/customer-invoices/src/web/proformas/update/entities/proforma-update-form.schema.test.ts @@ -0,0 +1,59 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +import { buildProformaUpdateDefault } from "../utils"; + +import { ProformaUpdateFormSchema } from "./proforma-update-form.schema"; + +const buildValidForm = () => ({ + ...buildProformaUpdateDefault(), + proformaDate: "2026-09-14", + operationDate: "2026-09-14", + customerId: "customer-1", +}); + +test("acepta un borrador sin líneas", () => { + assert.equal(ProformaUpdateFormSchema.safeParse(buildValidForm()).success, true); +}); + +test("acepta una línea válida", () => { + assert.equal( + ProformaUpdateFormSchema.safeParse({ + ...buildValidForm(), + items: [ + { + id: "item-1", + position: 0, + description: "Servicio", + quantity: 1, + unitAmount: 100, + itemDiscountPercentage: null, + taxPercentage: 21, + recPercentage: null, + }, + ], + }).success, + true + ); +}); + +test("mantiene la validación de la estructura de las líneas", () => { + assert.equal( + ProformaUpdateFormSchema.safeParse({ + ...buildValidForm(), + items: [ + { + id: "item-1", + position: "0", + description: "Servicio", + quantity: 1, + unitAmount: 100, + itemDiscountPercentage: null, + taxPercentage: 21, + recPercentage: null, + }, + ], + }).success, + false + ); +}); diff --git a/modules/customer-invoices/src/web/proformas/update/entities/proforma-update-form.schema.ts b/modules/customer-invoices/src/web/proformas/update/entities/proforma-update-form.schema.ts index e0866da2..ce8c17b0 100644 --- a/modules/customer-invoices/src/web/proformas/update/entities/proforma-update-form.schema.ts +++ b/modules/customer-invoices/src/web/proformas/update/entities/proforma-update-form.schema.ts @@ -51,9 +51,7 @@ export const ProformaUpdateFormSchema = z paymentMethodId: z.string().nullable(), paymentTermId: z.string().nullable(), - items: z - .array(ProformaItemUpdateFormSchema) - .min(1, "Debe haber al menos un item en la proforma"), + items: z.array(ProformaItemUpdateFormSchema), }) .refine( (formValues) => { diff --git a/modules/customer-invoices/src/web/proformas/update/utils/build-update-proforma-by-id-params.test.ts b/modules/customer-invoices/src/web/proformas/update/utils/build-update-proforma-by-id-params.test.ts index b59fc813..e7534c17 100644 --- a/modules/customer-invoices/src/web/proformas/update/utils/build-update-proforma-by-id-params.test.ts +++ b/modules/customer-invoices/src/web/proformas/update/utils/build-update-proforma-by-id-params.test.ts @@ -81,3 +81,25 @@ test("mantiene los items vacíos y respeta su orden al construir el PUT de updat }); assert.equal(result.data.items?.[1]?.position, 1); }); + +test("omite items cuando no se han modificado", () => { + const formData = buildFormData(); + + const result = buildUpdateProformaByIdParams( + "proforma-1", + { reference: "Referencia actualizada" }, + formData + ); + + assert.equal("items" in result.data, false); +}); + +test("envía una colección vacía cuando el usuario elimina todas las líneas", () => { + const formData = buildFormData({ + items: [], + }); + + const result = buildUpdateProformaByIdParams("proforma-1", { items: [] }, formData); + + assert.deepEqual(result.data.items, []); +});