From 3cbda5e3c5d2c497cf3aa58acf78c9925741b4d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chy=C5=82a?= Date: Thu, 5 Dec 2024 11:16:50 +0100 Subject: [PATCH] Fix deleting attribute value with tests (#5299) * Fix deleting attribute value with tests * Add changelog --- .changeset/young-countries-glow.md | 5 +++ src/attributes/utils/handlers.test.ts | 56 ++++++++++++++++++++++++++- src/attributes/utils/handlers.ts | 21 ++++++---- 3 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 .changeset/young-countries-glow.md diff --git a/.changeset/young-countries-glow.md b/.changeset/young-countries-glow.md new file mode 100644 index 00000000000..d1e69dd5fe9 --- /dev/null +++ b/.changeset/young-countries-glow.md @@ -0,0 +1,5 @@ +--- +"saleor-dashboard": patch +--- + +Update variant file attribute value no more cause error diff --git a/src/attributes/utils/handlers.test.ts b/src/attributes/utils/handlers.test.ts index d348788547a..d968270d779 100644 --- a/src/attributes/utils/handlers.test.ts +++ b/src/attributes/utils/handlers.test.ts @@ -1,10 +1,15 @@ import { createAttributeChangeHandler, createAttributeMultiChangeHandler, + handleDeleteMultipleAttributeValues, prepareAttributesInput, } from "@dashboard/attributes/utils/handlers"; import { AttributeInput, AttributeInputData } from "@dashboard/components/Attributes"; -import { AttributeInputTypeEnum, AttributeValueDetailsFragment } from "@dashboard/graphql"; +import { + AttributeInputTypeEnum, + AttributeValueDetailsFragment, + ProductFragment, +} from "@dashboard/graphql"; import { FormsetData } from "@dashboard/hooks/useFormset"; const multipleValueAttributes: FormsetData = [ @@ -796,3 +801,52 @@ describe("createAttributeChangeHandler", () => { expect(trigger).toHaveBeenCalledTimes(1); }); }); + +describe("handleDeleteMultipleAttributeValues", () => { + it("should return empty array when no attributes", async () => { + // Arrange + const trigger = jest.fn(); + + // Act + const result = await handleDeleteMultipleAttributeValues([], undefined, trigger); + + // Assert + expect(result).toEqual([]); + expect(trigger).toHaveBeenCalledTimes(0); + }); + + it("should call deleteAttributeValue when new attribute with file match existing one", async () => { + // Arrange + const deleteAttributeValue = jest.fn(() => Promise.resolve("val-1")) as any; + const attributesWithNewFileValue = [ + { + id: "attr-1", + }, + ] as FormsetData; + + const attributes = [ + { + attribute: { + id: "attr-1", + inputType: AttributeInputTypeEnum.FILE, + }, + values: [ + { + id: "val-1", + }, + ], + }, + ] as Array; + + // Act + const result = await handleDeleteMultipleAttributeValues( + attributesWithNewFileValue, + attributes, + deleteAttributeValue, + ); + + // Assert + expect(result).toEqual(["val-1"]); + expect(deleteAttributeValue).toHaveBeenCalledTimes(1); + }); +}); diff --git a/src/attributes/utils/handlers.ts b/src/attributes/utils/handlers.ts index 6a0e9160d95..7f664cb1f66 100644 --- a/src/attributes/utils/handlers.ts +++ b/src/attributes/utils/handlers.ts @@ -353,16 +353,22 @@ export const handleUploadMultipleFiles = async ( export const handleDeleteMultipleAttributeValues = async ( attributesWithNewFileValue: FormsetData, - attributes: Array< - | PageSelectedAttributeFragment - | ProductFragment["attributes"][0] - | NonNullable["nonSelectionAttributes"][0] - >, + attributes: + | Array< + | PageSelectedAttributeFragment + | ProductFragment["attributes"][0] + | NonNullable["nonSelectionAttributes"][0] + > + | undefined, deleteAttributeValue: ( variables: AttributeValueDeleteMutationVariables, ) => Promise>, -) => - Promise.all( +) => { + if (!attributes) { + return []; + } + + return Promise.all( attributes.map(existingAttribute => { const fileValueUnused = isFileValueUnused(attributesWithNewFileValue, existingAttribute); @@ -376,3 +382,4 @@ export const handleDeleteMultipleAttributeValues = async ( return undefined; }), ); +};