From b198bf3c4b26445a9f6382c6693f9fd07ce4a4bc Mon Sep 17 00:00:00 2001 From: alex-slobodian Date: Mon, 5 Aug 2024 15:18:06 +0300 Subject: [PATCH] Add test for useCertificateDeleteDialog hook --- .../useCertificateDeleteDialog.test.tsx | 57 +++++++++++++++++++ utils/testing/index.ts | 10 +++- 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 src/dialogs/certificate-delete-dialog/useCertificateDeleteDialog.test.tsx diff --git a/src/dialogs/certificate-delete-dialog/useCertificateDeleteDialog.test.tsx b/src/dialogs/certificate-delete-dialog/useCertificateDeleteDialog.test.tsx new file mode 100644 index 00000000..4adac917 --- /dev/null +++ b/src/dialogs/certificate-delete-dialog/useCertificateDeleteDialog.test.tsx @@ -0,0 +1,57 @@ +import { describe, it, renderHook, act, expect } from "@testing"; +import { vi } from "vitest"; +import { useCertificateDeleteDialog } from "./useCertificateDeleteDialog"; + +import type { FortifyAPI } from "@peculiar/fortify-client-core"; + +vi.mock("@peculiar/react-components", async () => { + const actual = await vi.importActual("@peculiar/react-components"); + return { + ...actual, + useToast: () => ({ + addToast: vi.fn(), + }), + }; +}); + +describe("useCertificateDeleteDialog", () => { + it("Should initialize", async () => { + const mockFortifyClient: Partial = { + removeCertificateById: vi.fn().mockResolvedValue({}), + }; + const handleSuccess = vi.fn(); + const { result } = renderHook(() => + useCertificateDeleteDialog({ + onSuccess: handleSuccess, + fortifyClient: mockFortifyClient as FortifyAPI, + }) + ); + + expect(result.current.dialog).toBeInstanceOf(Function); + expect(result.current.open).toBeInstanceOf(Function); + + const certificateIndex = "1"; + const providerId = "2"; + + act(() => { + result.current.open({ + certificateIndex, + providerId, + label: "Message", + }); + }); + + await act(async () => { + const DialogComponent = result.current.dialog(); + + DialogComponent && + (await DialogComponent.props.onDeleteClick(certificateIndex)); + }); + + expect(handleSuccess).toHaveBeenCalledWith(providerId); + expect(mockFortifyClient.removeCertificateById).toHaveBeenCalledWith( + providerId, + certificateIndex + ); + }); +}); diff --git a/utils/testing/index.ts b/utils/testing/index.ts index 23ad4f6d..fe89720b 100644 --- a/utils/testing/index.ts +++ b/utils/testing/index.ts @@ -1,3 +1,11 @@ export { describe, it, expect, vi, afterEach } from "vitest"; -export { render, screen, cleanup, fireEvent } from "@testing-library/react"; +export { + render, + screen, + cleanup, + fireEvent, + renderHook, + act, + waitFor, +} from "@testing-library/react"; export { userEvent } from "@testing-library/user-event";