From e1c086816815f590ee39971ca733d188d6c3b935 Mon Sep 17 00:00:00 2001 From: Wojciech Mista Date: Tue, 19 Nov 2024 11:30:47 +0100 Subject: [PATCH] Fix Save button in password change form (#5256) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix Save button in password change form * fix type * change header placement * expect onSubmit to be called with data --------- Co-authored-by: Paweł Chyła --- .changeset/rare-mirrors-sell.md | 5 + .../StaffPasswordResetDialog.test.tsx | 59 ++++++++ .../StaffPasswordResetDialog.tsx | 131 +++++++++--------- 3 files changed, 131 insertions(+), 64 deletions(-) create mode 100644 .changeset/rare-mirrors-sell.md create mode 100644 src/staff/components/StaffPasswordResetDialog/StaffPasswordResetDialog.test.tsx diff --git a/.changeset/rare-mirrors-sell.md b/.changeset/rare-mirrors-sell.md new file mode 100644 index 00000000000..a3340a96ccd --- /dev/null +++ b/.changeset/rare-mirrors-sell.md @@ -0,0 +1,5 @@ +--- +"saleor-dashboard": patch +--- + +The "Save" button in Change Password form now submits the form data to Saleor. diff --git a/src/staff/components/StaffPasswordResetDialog/StaffPasswordResetDialog.test.tsx b/src/staff/components/StaffPasswordResetDialog/StaffPasswordResetDialog.test.tsx new file mode 100644 index 00000000000..c0d0b40228e --- /dev/null +++ b/src/staff/components/StaffPasswordResetDialog/StaffPasswordResetDialog.test.tsx @@ -0,0 +1,59 @@ +import "@testing-library/jest-dom/extend-expect"; + +import { fireEvent, render, screen } from "@testing-library/react"; +import React from "react"; + +import StaffPasswordResetDialog, { + StaffPasswordResetDialogProps, +} from "./StaffPasswordResetDialog"; + +const defaultProps = { + confirmButtonState: "default", + errors: [], + open: true, + onClose: jest.fn(), + onSubmit: jest.fn(), +} as StaffPasswordResetDialogProps; + +jest.mock("react-intl", () => ({ + useIntl: jest.fn(() => ({ + formatMessage: jest.fn(x => x.defaultMessage), + })), + defineMessages: jest.fn(x => x), + FormattedMessage: ({ defaultMessage }: { defaultMessage: any }) => <>{defaultMessage}, +})); + +describe("StaffPasswordResetDialog", () => { + it("renders the form", () => { + // Arrange & Act + render(); + + // Assert + expect(screen.getByRole("form")).toBeInTheDocument(); + }); + + it("executes onSubmit when submit button is clicked", () => { + // Arrange + const { getByTestId } = render(); + + const submitButton = screen.getByTestId("submit"); + const oldPasswordInput = getByTestId("old-password-input").querySelector("input"); + const newPasswordInput = getByTestId("new-password-input").querySelector("input"); + + // Act + fireEvent.change(oldPasswordInput!, { + target: { value: "old-password" }, + }); + fireEvent.change(newPasswordInput!, { + target: { value: "new-password" }, + }); + fireEvent.click(submitButton); + + // Assert + expect(defaultProps.onSubmit).toHaveBeenCalledTimes(1); + expect(defaultProps.onSubmit).toHaveBeenCalledWith({ + oldPassword: "old-password", + newPassword: "new-password", + }); + }); +}); diff --git a/src/staff/components/StaffPasswordResetDialog/StaffPasswordResetDialog.tsx b/src/staff/components/StaffPasswordResetDialog/StaffPasswordResetDialog.tsx index b0de8e889bd..9e5fdcff1c7 100644 --- a/src/staff/components/StaffPasswordResetDialog/StaffPasswordResetDialog.tsx +++ b/src/staff/components/StaffPasswordResetDialog/StaffPasswordResetDialog.tsx @@ -11,6 +11,7 @@ import { DialogProps } from "@dashboard/types"; import { getFormErrors } from "@dashboard/utils/errors"; import getAccountErrorMessage from "@dashboard/utils/errors/account"; import { TextField } from "@material-ui/core"; +import { Box } from "@saleor/macaw-ui-next"; import React from "react"; import { FormattedMessage, useIntl } from "react-intl"; @@ -41,73 +42,75 @@ const StaffPasswordResetDialog: React.FC = ({ return ( -
- {({ change, data }) => ( - - - - + + + + - + + {({ change, data }) => ( + + - + - - - - - - - - )} - + + + + + + + + )} + +
); };