From 9baa57298b61a6bd3e6fb797aa66fe96d01b68f4 Mon Sep 17 00:00:00 2001 From: Oleg Chendighelean Date: Tue, 21 May 2024 17:56:42 +0300 Subject: [PATCH] Fix invalid secret key error message --- .../RestoreSecretKey.test.tsx | 20 ++-- .../restoreSecretKey/RestoreSecretKey.tsx | 91 +++++++++---------- src/components/PasswordInput.tsx | 14 ++- 3 files changed, 64 insertions(+), 61 deletions(-) diff --git a/src/components/Onboarding/restoreSecretKey/RestoreSecretKey.test.tsx b/src/components/Onboarding/restoreSecretKey/RestoreSecretKey.test.tsx index 5a7f4f6183..74d0a973b0 100644 --- a/src/components/Onboarding/restoreSecretKey/RestoreSecretKey.test.tsx +++ b/src/components/Onboarding/restoreSecretKey/RestoreSecretKey.test.tsx @@ -32,7 +32,9 @@ describe("", () => { it("requires the password when the secret key is encrypted", async () => { render(fixture()); - fireEvent.change(screen.getByTestId("secret-key"), { target: { value: "edesk..." } }); + fireEvent.change(screen.getByTestId("secret-key"), { + target: { value: "edesk..." }, + }); await screen.findByTestId("password"); fireEvent.blur(screen.getByTestId("password")); @@ -75,9 +77,9 @@ describe("", () => { await act(() => user.type(screen.getByTestId("password"), password)); } - expect(screen.getByRole("button")).toBeEnabled(); + expect(screen.getByTestId("restore-continue-button")).toBeEnabled(); - await act(() => user.click(screen.getByRole("button", { name: "Continue" }))); + await act(() => user.click(screen.getByTestId("restore-continue-button"))); expect(goToStepMock).toHaveBeenCalledWith({ type: "nameAccount", @@ -96,9 +98,9 @@ describe("", () => { await act(() => user.type(screen.getByTestId("password"), "wrong password")); - expect(screen.getByRole("button")).toBeEnabled(); + expect(screen.getByTestId("restore-continue-button")).toBeEnabled(); - await act(() => user.click(screen.getByRole("button", { name: "Continue" }))); + await act(() => user.click(screen.getByTestId("restore-continue-button"))); expect(mockToast).toHaveBeenCalledWith({ description: "Key-password pair is invalid", @@ -112,9 +114,9 @@ describe("", () => { await act(() => user.type(screen.getByTestId("secret-key"), UNENCRYPTED_SECRET_KEY + "asdasd")); - expect(screen.getByRole("button")).toBeEnabled(); + expect(screen.getByTestId("restore-continue-button")).toBeEnabled(); - await act(() => user.click(screen.getByRole("button", { name: "Continue" }))); + await act(() => user.click(screen.getByTestId("restore-continue-button"))); expect(mockToast).toHaveBeenCalledWith({ description: "Invalid secret key: checksum doesn't match", @@ -128,9 +130,9 @@ describe("", () => { await act(() => user.type(screen.getByTestId("secret-key"), "something invalid")); - expect(screen.getByRole("button")).toBeEnabled(); + expect(screen.getByTestId("restore-continue-button")).toBeEnabled(); - await act(() => user.click(screen.getByRole("button", { name: "Continue" }))); + await act(() => user.click(screen.getByTestId("restore-continue-button"))); expect(mockToast).toHaveBeenCalledWith({ description: diff --git a/src/components/Onboarding/restoreSecretKey/RestoreSecretKey.tsx b/src/components/Onboarding/restoreSecretKey/RestoreSecretKey.tsx index d641e54433..88f0dc0ac6 100644 --- a/src/components/Onboarding/restoreSecretKey/RestoreSecretKey.tsx +++ b/src/components/Onboarding/restoreSecretKey/RestoreSecretKey.tsx @@ -1,19 +1,13 @@ -import { - Button, - FormControl, - FormErrorMessage, - FormLabel, - Input, - Textarea, -} from "@chakra-ui/react"; +import { Button, FormControl, FormErrorMessage, FormLabel, Textarea } from "@chakra-ui/react"; import { InMemorySigner } from "@taquito/signer"; import { useState } from "react"; -import { useForm } from "react-hook-form"; +import { FormProvider, useForm } from "react-hook-form"; import { KeyIcon } from "../../../assets/icons"; import colors from "../../../style/colors"; import { useAsyncActionHandler } from "../../../utils/hooks/useAsyncActionHandler"; import { isEncryptedSecretKeyPrefix } from "../../../utils/redux/thunks/secretKeyAccount"; +import { PasswordInput } from "../../PasswordInput"; import { ModalContentWrapper } from "../ModalContentWrapper"; import { OnboardingStep } from "../OnboardingStep"; @@ -21,14 +15,16 @@ export const RestoreSecretKey = ({ goToStep }: { goToStep: (step: OnboardingStep const [isEncrypted, setIsEncrypted] = useState(false); const { handleAsyncAction } = useAsyncActionHandler(); + const form = useForm<{ secretKey: string; password: string }>({ + mode: "onBlur", + defaultValues: { password: "" }, + }); + const { register, handleSubmit, formState: { errors, isValid }, - } = useForm<{ secretKey: string; password: string }>({ - mode: "onBlur", - defaultValues: { password: "" }, - }); + } = form; const onSubmit = async ({ secretKey: rawSecretKey, @@ -51,7 +47,7 @@ export const RestoreSecretKey = ({ goToStep }: { goToStep: (step: OnboardingStep const message = error.message || ""; // if the password doesn't match taquito throws this error - if (message.includes("Cannot read properties of null (reading 'slice')")) { + if (message.includes("Cannot read properties of null")) { throw new Error("Key-password pair is invalid"); } @@ -68,44 +64,43 @@ export const RestoreSecretKey = ({ goToStep }: { goToStep: (step: OnboardingStep icon={} title="Insert Secret Key" > -
- - Secret Key -