Skip to content

Commit

Permalink
Fix deleting attribute value with tests (#5299)
Browse files Browse the repository at this point in the history
* Fix deleting attribute value with tests

* Add changelog
  • Loading branch information
poulch authored Dec 5, 2024
1 parent 63e7d28 commit 3cbda5e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-countries-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---

Update variant file attribute value no more cause error
56 changes: 55 additions & 1 deletion src/attributes/utils/handlers.test.ts
Original file line number Diff line number Diff line change
@@ -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<AttributeInputData, string[]> = [
Expand Down Expand Up @@ -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<null, File>;

const attributes = [
{
attribute: {
id: "attr-1",
inputType: AttributeInputTypeEnum.FILE,
},
values: [
{
id: "val-1",
},
],
},
] as Array<ProductFragment["attributes"][0]>;

// Act
const result = await handleDeleteMultipleAttributeValues(
attributesWithNewFileValue,
attributes,
deleteAttributeValue,
);

// Assert
expect(result).toEqual(["val-1"]);
expect(deleteAttributeValue).toHaveBeenCalledTimes(1);
});
});
21 changes: 14 additions & 7 deletions src/attributes/utils/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,16 +353,22 @@ export const handleUploadMultipleFiles = async (

export const handleDeleteMultipleAttributeValues = async (
attributesWithNewFileValue: FormsetData<null, File>,
attributes: Array<
| PageSelectedAttributeFragment
| ProductFragment["attributes"][0]
| NonNullable<ProductVariantDetailsQuery["productVariant"]>["nonSelectionAttributes"][0]
>,
attributes:
| Array<
| PageSelectedAttributeFragment
| ProductFragment["attributes"][0]
| NonNullable<ProductVariantDetailsQuery["productVariant"]>["nonSelectionAttributes"][0]
>
| undefined,
deleteAttributeValue: (
variables: AttributeValueDeleteMutationVariables,
) => Promise<FetchResult<AttributeValueDeleteMutation>>,
) =>
Promise.all(
) => {
if (!attributes) {
return [];
}

return Promise.all(
attributes.map(existingAttribute => {
const fileValueUnused = isFileValueUnused(attributesWithNewFileValue, existingAttribute);

Expand All @@ -376,3 +382,4 @@ export const handleDeleteMultipleAttributeValues = async (
return undefined;
}),
);
};

0 comments on commit 3cbda5e

Please sign in to comment.