Handle error when editing address (#3598)

This commit is contained in:
Patryk Andrzejewski 2023-05-09 09:48:06 +02:00 committed by GitHub
parent ea29aed3d4
commit 01c24c1622
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 86 additions and 17 deletions

View file

@ -2,17 +2,16 @@ import { AddressTypeInput } from "@dashboard/customers/types";
import { AccountErrorFragment, OrderErrorFragment } from "@dashboard/graphql"; import { AccountErrorFragment, OrderErrorFragment } from "@dashboard/graphql";
import { commonMessages } from "@dashboard/intl"; import { commonMessages } from "@dashboard/intl";
import { getFormErrors } from "@dashboard/utils/errors"; import { getFormErrors } from "@dashboard/utils/errors";
import getAccountErrorMessage from "@dashboard/utils/errors/account";
import getOrderErrorMessage from "@dashboard/utils/errors/order";
import { TextField } from "@material-ui/core"; import { TextField } from "@material-ui/core";
import { makeStyles } from "@saleor/macaw-ui"; import { makeStyles } from "@saleor/macaw-ui";
import React from "react"; import React from "react";
import { IntlShape, useIntl } from "react-intl"; import { useIntl } from "react-intl";
import FormSpacer from "../FormSpacer"; import FormSpacer from "../FormSpacer";
import SingleAutocompleteSelectField, { import SingleAutocompleteSelectField, {
SingleAutocompleteChoiceType, SingleAutocompleteChoiceType,
} from "../SingleAutocompleteSelectField"; } from "../SingleAutocompleteSelectField";
import { getErrorMessage } from "./getErrorMessage";
import { useAddressValidation } from "./useAddressValidation"; import { useAddressValidation } from "./useAddressValidation";
const useStyles = makeStyles( const useStyles = makeStyles(
@ -36,17 +35,6 @@ interface AddressEditProps {
onCountryChange(event: React.ChangeEvent<any>); onCountryChange(event: React.ChangeEvent<any>);
} }
function getErrorMessage(
err: AccountErrorFragment | OrderErrorFragment,
intl: IntlShape,
): string {
if (err?.__typename === "AccountError") {
return getAccountErrorMessage(err, intl);
}
return getOrderErrorMessage(err, intl);
}
const PossibleFormFields = { const PossibleFormFields = {
CITY: "city", CITY: "city",
CITY_AREA: "cityArea", CITY_AREA: "cityArea",
@ -61,9 +49,8 @@ const PossibleFormFields = {
STREET_ADDRESS_2: "streetAddress2", STREET_ADDRESS_2: "streetAddress2",
} as const; } as const;
const formFields: Array<keyof AddressTypeInput> = Object.values( const formFields: Array<keyof AddressTypeInput> =
PossibleFormFields, Object.values(PossibleFormFields);
);
const AddressEdit: React.FC<AddressEditProps> = props => { const AddressEdit: React.FC<AddressEditProps> = props => {
const { const {

View file

@ -0,0 +1,63 @@
import { AccountErrorFragment, OrderErrorFragment } from "@dashboard/graphql";
import getAccountErrorMessage from "@dashboard/utils/errors/account";
import getOrderErrorMessage from "@dashboard/utils/errors/order";
import { IntlShape } from "react-intl";
import { getErrorMessage } from "./getErrorMessage";
jest.mock("@dashboard/utils/errors/account", () => jest.fn());
jest.mock("@dashboard/utils/errors/order", () => jest.fn());
describe("getErrorMessage", () => {
it("returns original message when it exist", () => {
// Arrange
const error = {
__typename: "AccountError",
message: "test message",
code: "INVALID",
} as AccountErrorFragment;
const intlShape = {} as IntlShape;
// Act
const message = getErrorMessage(error, intlShape);
// Assert
expect(message).toBe("test message");
});
it("returns account error message", () => {
// Arrange
const error = {
__typename: "AccountError",
message: null,
code: "INVALID",
} as AccountErrorFragment;
const intlShape = {} as IntlShape;
(getAccountErrorMessage as jest.Mock).mockReturnValue(
"account error message",
);
// Act
const message = getErrorMessage(error, intlShape);
// Assert
expect(message).toBe("account error message");
});
it("returns account error message", () => {
// Arrange
const error = {
__typename: "OrderError",
message: null,
code: "INVALID",
} as unknown as OrderErrorFragment;
const intlShape = {} as IntlShape;
(getOrderErrorMessage as jest.Mock).mockReturnValue("order error message");
// Act
const message = getErrorMessage(error, intlShape);
// Assert
expect(message).toBe("order error message");
});
});

View file

@ -0,0 +1,19 @@
import { AccountErrorFragment, OrderErrorFragment } from "@dashboard/graphql";
import getAccountErrorMessage from "@dashboard/utils/errors/account";
import getOrderErrorMessage from "@dashboard/utils/errors/order";
import { IntlShape } from "react-intl";
export function getErrorMessage(
err: AccountErrorFragment | OrderErrorFragment,
intl: IntlShape,
): string {
if (err?.message) {
return err.message;
}
if (err?.__typename === "AccountError") {
return getAccountErrorMessage(err, intl);
}
return getOrderErrorMessage(err, intl);
}