From 8a12d5496f41cb0c0f87b32a3b6dd71c6e80485b Mon Sep 17 00:00:00 2001 From: Patryk Andrzejewski Date: Tue, 22 Aug 2023 09:11:04 +0200 Subject: [PATCH] Validate channels only when it has no variants (#4095) * Validate channels only when it has no variants * Add test --- src/products/utils/validation.test.ts | 26 ++++++++++++++++++++++++-- src/products/utils/validation.ts | 4 +++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/products/utils/validation.test.ts b/src/products/utils/validation.test.ts index c53a0d754..b488f5cf8 100644 --- a/src/products/utils/validation.test.ts +++ b/src/products/utils/validation.test.ts @@ -28,10 +28,12 @@ describe("validateProductCreateData", () => { ]); }); - it("returns errors when there is no prices for channels", () => { + it("returns errors when there is no prices for channels in simple product", () => { // Arrange const data = { - productType: "something", + productType: { + hasVariants: false, + }, name: "something", channelListings: [ { id: "chann-1", price: "" }, @@ -60,4 +62,24 @@ describe("validateProductCreateData", () => { }, ]); }); + + it("returns errors when there is no prices for channels in product with variants", () => { + // Arrange + const data = { + productType: { + hasVariants: true, + }, + name: "something", + channelListings: [ + { id: "chann-1", price: "" }, + { id: "chann-2", price: "" }, + ], + } as unknown as ProductCreateData; + + // Act + const errors = validateProductCreateData(data); + + // Assert + expect(errors).toEqual([]); + }); }); diff --git a/src/products/utils/validation.ts b/src/products/utils/validation.ts index 1c67f2089..58978dc96 100644 --- a/src/products/utils/validation.ts +++ b/src/products/utils/validation.ts @@ -34,7 +34,9 @@ export const validateProductCreateData = (data: ProductCreateData) => { errors = [...errors, createEmptyRequiredError("name")]; } - if (data.channelListings) { + const { productType, channelListings } = data; + + if (!productType.hasVariants && channelListings) { const emptyPrices = data.channelListings .filter(channel => channel.price?.length === 0) .map(({ id }) => createEmptyRequiredError(`${id}-channel-price`));