Skip to content

Commit

Permalink
Fix clearing area input cause error (#5297)
Browse files Browse the repository at this point in the history
* Fix cannot read properly of undefined reading area

* Add changeset
  • Loading branch information
poulch authored Dec 5, 2024
1 parent c3e02b9 commit 8807dee
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-apples-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---

Removing value from area input in address form no more cause error
76 changes: 76 additions & 0 deletions src/components/AddressEdit/useAddressValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe("useAddressValidation", () => {
});
expect(current.isFieldAllowed("country")).toBeFalsy();
});

it("loads validation rules when country is provided", () => {
// Arrange
(useAddressValidationRulesQuery as jest.Mock).mockReturnValue({
Expand Down Expand Up @@ -62,4 +63,79 @@ describe("useAddressValidation", () => {
expect(current.isFieldAllowed("country")).toBeTruthy();
expect(current.isFieldAllowed("countryArea")).toBeFalsy();
});

it("getDisplayValue should return display value for area code when valid", () => {
// Arrange
(useAddressValidationRulesQuery as jest.Mock).mockReturnValue({
data: {
addressValidationRules: {
countryAreaChoices: [
{ raw: "AL", verbose: "Alabama" },
{ raw: "AN", verbose: "Ancona" },
],
allowedFields: ["country"],
},
},
loading: false,
});

const {
result: { current },
} = renderHook(() => useAddressValidation("US"));

const displayValue = current.getDisplayValue("AL");

// Assert
expect(displayValue).toEqual("Alabama");
});

it("getDisplayValue should return value when area code invalid", () => {
// Arrange
(useAddressValidationRulesQuery as jest.Mock).mockReturnValue({
data: {
addressValidationRules: {
countryAreaChoices: [
{ raw: "AL", verbose: "Alabama" },
{ raw: "AN", verbose: "Ancona" },
],
allowedFields: ["country"],
},
},
loading: false,
});

const {
result: { current },
} = renderHook(() => useAddressValidation("US"));

const displayValue = current.getDisplayValue("XX");

// Assert
expect(displayValue).toEqual("XX");
});

it("getDisplayValue should return empty string when value is null", () => {
// Arrange
(useAddressValidationRulesQuery as jest.Mock).mockReturnValue({
data: {
addressValidationRules: {
countryAreaChoices: [
{ raw: "AL", verbose: "Alabama" },
{ raw: "AN", verbose: "Ancona" },
],
allowedFields: ["country"],
},
},
loading: false,
});

const {
result: { current },
} = renderHook(() => useAddressValidation("US"));

const displayValue = current.getDisplayValue(null);

// Assert
expect(displayValue).toEqual("");
});
});
12 changes: 10 additions & 2 deletions src/components/AddressEdit/useAddressValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,19 @@ const useAllowedFields = (data: AddressValidationRulesQuery) => {
};
const useDisplayValues = (areas: AreaChoices[]) => {
const isProvinceCode = (code: string) => code.length === 2 && code.toLocaleUpperCase() === code;
const getDisplayValue = (value: string) => {
const getDisplayValue = (value: string | null | undefined) => {
if (!value) {
return "";
}

if (isProvinceCode(value)) {
const area = areas.find(area => area.raw === value);

return area.value;
if (area) {
return area.value;
}

return value;
}

return value;
Expand Down

0 comments on commit 8807dee

Please sign in to comment.