diff --git a/src/dialogs/certificate-import-dialog/useCertificateImportDialog.test.tsx b/src/dialogs/certificate-import-dialog/useCertificateImportDialog.test.tsx index 0162520e..c3235bea 100644 --- a/src/dialogs/certificate-import-dialog/useCertificateImportDialog.test.tsx +++ b/src/dialogs/certificate-import-dialog/useCertificateImportDialog.test.tsx @@ -5,12 +5,14 @@ import { useCertificateImportDialog } from "./useCertificateImportDialog"; import type { IProviderInfo, FortifyAPI } from "@peculiar/fortify-client-core"; +const mockAddToast = vi.fn(); + vi.mock("@peculiar/react-components", async () => { const actual = await vi.importActual("@peculiar/react-components"); return { ...actual, useToast: () => ({ - addToast: vi.fn(), + addToast: mockAddToast, }), }; }); @@ -87,8 +89,7 @@ describe("useCertificateImportDialog", () => { }); act(() => { - const DialogComponent = result.current.dialog(); - DialogComponent && DialogComponent.props.onTextAreaChange(certificate); + result.current.dialog()?.props.onTextAreaChange(certificate); }); await act(async () => { @@ -133,4 +134,85 @@ describe("useCertificateImportDialog", () => { it("Should upload file certificate & call onSuccess (CSR)", async () => { await testFileUploadCertificate(certificateCSRPem, "csr"); }); + + it("Should handle text area validation error", () => { + const { result } = renderHook(() => + useCertificateImportDialog({ + providers, + onSuccess: vi.fn(), + fortifyClient: mockFortifyClient as FortifyAPI, + }) + ); + + act(() => { + result.current.open(); + }); + + act(() => { + result.current.dialog()?.props.onTextAreaChange("certificate"); + }); + + act(() => { + result.current.dialog()?.props.onTextAreaBlur(); + }); + + expect(result.current.dialog()?.props.isTextAreaError).toBeTruthy(); + }); + + it("Should handle file rejection correctly", () => { + const { result } = renderHook(() => + useCertificateImportDialog({ + providers, + onSuccess: vi.fn(), + fortifyClient: mockFortifyClient as FortifyAPI, + }) + ); + + act(() => { + result.current.open(); + }); + + const errorMessage = "Error message"; + act(() => { + result.current.dialog()?.props.onDropRejected(errorMessage); + }); + + expect(mockAddToast).toHaveBeenCalledWith( + expect.objectContaining({ + message: errorMessage, + variant: "wrong", + }) + ); + }); + + it("Should handle the error when uploading an invalid certificate", async () => { + const byteArray = new TextEncoder().encode("certificate"); + const { result } = renderHook(() => + useCertificateImportDialog({ + providers, + onSuccess: vi.fn(), + fortifyClient: mockFortifyClient as FortifyAPI, + }) + ); + + act(() => { + result.current.open(); + }); + + await act(async () => { + const DialogComponent = result.current.dialog(); + if (DialogComponent) { + DialogComponent.props.onDropAccepted(byteArray.buffer, "cer", ""); + await DialogComponent.props.onImportButtonClick(); + } + }); + + expect(mockAddToast).toHaveBeenCalledWith( + expect.objectContaining({ + message: + "Certificate is invalid. Please check your data and try again.", + variant: "wrong", + }) + ); + }); });