From 131390441b601876ce1518098098e7f6117bd4bd Mon Sep 17 00:00:00 2001 From: alex-slobodian Date: Mon, 4 Nov 2024 19:52:26 +0200 Subject: [PATCH] Add more tests --- src/app.test.tsx | 122 ++++++++++++++++++ .../useCertificateViewerDialog.test.tsx | 50 +++++-- .../useProviderInfoDialog.test.tsx | 30 ++++- vitest.config.ts | 2 +- 4 files changed, 192 insertions(+), 12 deletions(-) create mode 100644 src/app.test.tsx diff --git a/src/app.test.tsx b/src/app.test.tsx new file mode 100644 index 00000000..dd3c38c9 --- /dev/null +++ b/src/app.test.tsx @@ -0,0 +1,122 @@ +import { render, screen, userEvent, waitFor } from "@testing"; + +import type { IProviderInfo } from "@peculiar/fortify-client-core"; +import { FortifyAPI } from "@peculiar/fortify-client-core"; +import { App } from "./app"; + +vi.mock("@peculiar/fortify-client-core"); + +describe("", () => { + const providersMock = [ + { + id: "provider1", + name: "Provider 1", + }, + { + id: "provider2", + name: "Provider 2", + }, + ] as IProviderInfo[]; + + const certificatesMock = [ + { + id: "1", + providerID: "provider1", + subject: { + CN: ["Certificate test 1"], + }, + subjectName: "Certificate test 1", + raw: new ArrayBuffer(1), + type: "x509", + serialNumber: "1", + notAfter: new Date("2024-01-01"), + }, + { + id: "2", + providerID: "provider1", + subject: { + CN: ["Certificate test 2"], + }, + subjectName: "Certificate test 2", + raw: new ArrayBuffer(0), + type: "x509", + serialNumber: "2", + notAfter: new Date("2024-01-02"), + }, + ]; + + const mockFortifyAPIInstance: Partial = { + challenge: vi.fn().mockResolvedValue(""), + login: vi.fn(), + getProviders: vi.fn().mockResolvedValue(providersMock), + isConnectionSupported: vi.fn().mockReturnValue(true), + isConnectionDetected: vi.fn().mockResolvedValue(true), + isConnectionDetectedAuto: vi.fn(), + connect: vi.fn(), + getProviderById: vi.fn().mockResolvedValue({ + id: providersMock[0].id, + isLoggedIn: vi.fn().mockResolvedValue(true), + reset: vi.fn(), + login: vi.fn(), + logout: vi.fn(), + }), + getCertificatesByProviderId: vi.fn().mockResolvedValue(certificatesMock), + }; + + it("Should render, show providers & certificates", async () => { + vi.mocked(FortifyAPI).mockImplementation( + () => mockFortifyAPIInstance as FortifyAPI + ); + + render(); + + await waitFor(() => { + expect(screen.getByText(/Provider 1/)).toBeInTheDocument(); + expect(screen.getByText(/Provider 2/)).toBeInTheDocument(); + expect(screen.getByText(/Certificate test 1/)).toBeInTheDocument(); + expect(screen.getByText(/Certificate test 2/)).toBeInTheDocument(); + }); + }); + + it("Should open provider info dialog", async () => { + vi.mocked(FortifyAPI).mockImplementation( + () => mockFortifyAPIInstance as FortifyAPI + ); + + render(); + + await waitFor(() => { + expect(screen.getByText(/Certificate test 1/)).toBeInTheDocument(); + }); + + userEvent.click(screen.getByRole("button", { name: /info/i })); + + await waitFor(() => { + expect(screen.getByText(/Provider 1 information/)).toBeInTheDocument(); + }); + }); + + it("Should handle search & clear search", async () => { + vi.mocked(FortifyAPI).mockImplementation( + () => mockFortifyAPIInstance as FortifyAPI + ); + + render(); + + await waitFor(() => { + expect(screen.getByText(/Certificate test 1/)).toBeInTheDocument(); + }); + + userEvent.type(screen.getByPlaceholderText("Search"), "test 1"); + + await waitFor(() => { + expect(screen.getAllByText("test 1")).toHaveLength(1); + }); + + await userEvent.click(screen.getByTestId("clear-search-button")); + + await waitFor(() => { + expect(screen.queryByText("test 1")).not.toBeInTheDocument(); + }); + }); +}); diff --git a/src/dialogs/certificate-viewer-dialog/useCertificateViewerDialog.test.tsx b/src/dialogs/certificate-viewer-dialog/useCertificateViewerDialog.test.tsx index 1f6e0cfb..3b7c0a25 100644 --- a/src/dialogs/certificate-viewer-dialog/useCertificateViewerDialog.test.tsx +++ b/src/dialogs/certificate-viewer-dialog/useCertificateViewerDialog.test.tsx @@ -12,7 +12,17 @@ describe("useCertificateViewerDialog", () => { }, ] as IProviderInfo[]; - it("Should initialize", () => { + const certificate = { + id: "1", + label: "Certificate name", + } as CertificateProps; + + const defaultOpenProps = { + certificate, + providerId: providers[0].id, + }; + + it("Should initialize, open & close dialog", () => { const { result } = renderHook(() => useCertificateViewerDialog({ providers, @@ -22,21 +32,41 @@ describe("useCertificateViewerDialog", () => { expect(result.current.dialog).toBeInstanceOf(Function); expect(result.current.open).toBeInstanceOf(Function); - const certificate = { - id: "1", - label: "Certificate name", - } as CertificateProps; - act(() => { - result.current.open({ - certificate, - providerId: providers[0].id, - }); + result.current.open(defaultOpenProps); }); const DialogComponent = result.current.dialog(); expect(DialogComponent).not.toBeNull(); expect(DialogComponent?.props.certificate).toBe(certificate); + + act(() => { + DialogComponent?.props.onClose(); + }); + + expect(result.current.dialog()).toBeNull(); + }); + + it("Should close dialog if current provider is not found", async () => { + const { result, rerender } = renderHook( + (localProviders) => + useCertificateViewerDialog({ + providers: localProviders, + }), + { initialProps: providers } + ); + + act(() => { + result.current.open(defaultOpenProps); + }); + + rerender([ + { + id: "2", + }, + ] as IProviderInfo[]); + + expect(result.current.dialog()).toBeNull(); }); }); diff --git a/src/dialogs/provider-info-dialog/useProviderInfoDialog.test.tsx b/src/dialogs/provider-info-dialog/useProviderInfoDialog.test.tsx index 91c39d33..5cb74265 100644 --- a/src/dialogs/provider-info-dialog/useProviderInfoDialog.test.tsx +++ b/src/dialogs/provider-info-dialog/useProviderInfoDialog.test.tsx @@ -11,7 +11,7 @@ describe("useProviderInfoDialog", () => { }, ] as IProviderInfo[]; - it("Should initialize", async () => { + it("Should initialize, open & close", async () => { const { result } = renderHook(() => useProviderInfoDialog({ providers, @@ -29,5 +29,33 @@ describe("useProviderInfoDialog", () => { expect(DialogComponent).not.toBeNull(); expect(DialogComponent?.props.data).toBe(providers[0]); + + act(() => { + DialogComponent?.props.onDialogClose(); + }); + + expect(result.current.dialog()).toBeNull(); + }); + + it("Should close dialog if current provider is not found", async () => { + const { result, rerender } = renderHook( + (localProviders) => + useProviderInfoDialog({ + providers: localProviders, + }), + { initialProps: providers } + ); + + act(() => { + result.current.open(providers[0]); + }); + + rerender([ + { + id: "2", + }, + ] as IProviderInfo[]); + + expect(result.current.dialog()).toBeNull(); }); }); diff --git a/vitest.config.ts b/vitest.config.ts index 0343a75c..9a04ec42 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -13,7 +13,7 @@ export default defineConfig({ exclude: [ "**/*.stories.*/**", "**/*.test.tsx", - "**/index.ts", + "**/index.{ts,tsx}", "**/types.ts", "**/*.d.ts", ],