Skip to content

Commit

Permalink
Fix handle nullable attribute change (#5179)
Browse files Browse the repository at this point in the history
* Fix handle nullable attribute change

* Add changeset
  • Loading branch information
poulch authored Sep 26, 2024
1 parent c665bb8 commit 22919c2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/sour-balloons-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---

Removing not required dropdown attribue value no longer cause error
49 changes: 49 additions & 0 deletions src/attributes/utils/handlers.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
createAttributeChangeHandler,
createAttributeMultiChangeHandler,
prepareAttributesInput,
} from "@dashboard/attributes/utils/handlers";
Expand Down Expand Up @@ -223,6 +224,7 @@ describe("Multiple select change handler", () => {
expect(trigger).toHaveBeenCalledTimes(1);
});
});

describe("Sending only changed attributes", () => {
// null in expected = attribute not present in output
describe("works with reference attributes", () => {
Expand Down Expand Up @@ -747,3 +749,50 @@ describe("Sending only changed attributes", () => {
});
});
});

describe("createAttributeChangeHandler", () => {
it("should return empty array when value is empty string", () => {
// Arrange
const change = jest.fn();
const trigger = jest.fn();
const handler = createAttributeChangeHandler(change, trigger);

// Act
handler("attr-1", "");

// Assert
expect(change).toHaveBeenCalledTimes(1);
expect(change).toHaveBeenCalledWith("attr-1", []);
expect(trigger).toHaveBeenCalledTimes(1);
});

it("should return empty array when value is null", () => {
// Arrange
const change = jest.fn();
const trigger = jest.fn();
const handler = createAttributeChangeHandler(change, trigger);

// Act
handler("attr-1", null);

// Assert
expect(change).toHaveBeenCalledTimes(1);
expect(change).toHaveBeenCalledWith("attr-1", []);
expect(trigger).toHaveBeenCalledTimes(1);
});

it("should return array with value when value not null or undefined or empty string", () => {
// Arrange
const change = jest.fn();
const trigger = jest.fn();
const handler = createAttributeChangeHandler(change, trigger);

// Act
handler("attr-1", "val-1");

// Assert
expect(change).toHaveBeenCalledTimes(1);
expect(change).toHaveBeenCalledWith("attr-1", ["val-1"]);
expect(trigger).toHaveBeenCalledTimes(1);
});
});
6 changes: 3 additions & 3 deletions src/attributes/utils/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ import { getFileValuesToUploadFromAttributes, isFileValueUnused } from "./data";
export function createAttributeChangeHandler(
changeAttributeData: FormsetChange<string[]>,
triggerChange: () => void,
): FormsetChange<string> {
return (attributeId: string, value: string) => {
): FormsetChange<string | null | undefined> {
return (attributeId: string, value: string | null | undefined) => {
triggerChange();
changeAttributeData(attributeId, value === "" ? [] : [value]);
changeAttributeData(attributeId, !value ? [] : [value]);
};
}

Expand Down

0 comments on commit 22919c2

Please sign in to comment.