From 2dea9f573b672a561d3daf16467ef70f2e7be837 Mon Sep 17 00:00:00 2001 From: alex-slobodian Date: Wed, 23 Oct 2024 15:40:03 +0300 Subject: [PATCH] Add more tests --- .../useCertificateViewerDialog.test.tsx | 93 ++++++++++++++++--- 1 file changed, 80 insertions(+), 13 deletions(-) diff --git a/src/dialogs/certificate-viewer-dialog/useCertificateViewerDialog.test.tsx b/src/dialogs/certificate-viewer-dialog/useCertificateViewerDialog.test.tsx index bfc8079..cf08d48 100644 --- a/src/dialogs/certificate-viewer-dialog/useCertificateViewerDialog.test.tsx +++ b/src/dialogs/certificate-viewer-dialog/useCertificateViewerDialog.test.tsx @@ -1,8 +1,23 @@ import { renderHook, act } from "@testing"; +import { vi } from "vitest"; import { useCertificateViewerDialog } from "./useCertificateViewerDialog"; import { CertificateProps } from "../../types"; -import type { IProviderInfo } from "@peculiar/fortify-client-core"; +import type { IProviderInfo, FortifyAPI } from "@peculiar/fortify-client-core"; + +vi.mock("@peculiar/x509", async (importOriginal) => { + const originalModule = + await importOriginal(); + + return { + ...originalModule, + X509Certificate: vi.fn().mockImplementation(() => ({ + rawData: new ArrayBuffer(0), + subject: "Test Subject", + subjectName: "Rest Subject Name", + })), + }; +}); describe("useCertificateViewerDialog", () => { const providers = [ @@ -12,33 +27,85 @@ describe("useCertificateViewerDialog", () => { }, ] as IProviderInfo[]; + const certificate = { + id: "1", + label: "Certificate name", + type: "x509", + } as CertificateProps; + + const defaultProps = { + providers, + fortifyClient: null, + currentProviderId: providers[0].id, + }; + + const defaultOpenProps = { + certificate, + providerId: providers[0].id, + }; + it("Should initialize", () => { const { result } = renderHook(() => - useCertificateViewerDialog({ - providers, - fortifyClient: null, - currentProviderId: providers[0].id, - }) + useCertificateViewerDialog(defaultProps) ); 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(defaultOpenProps); + }); + + const DialogComponent = result.current.dialog(); + + expect(DialogComponent).not.toBeNull(); + expect(DialogComponent?.props.certificates).toStrictEqual([certificate]); + }); + + it("should close the dialog when the current provider is not found", () => { + const { result } = renderHook(() => + useCertificateViewerDialog(defaultProps) + ); act(() => { result.current.open({ - certificate, - providerId: providers[0].id, + ...defaultOpenProps, + providerId: "2", }); }); const DialogComponent = result.current.dialog(); + expect(DialogComponent).toBeNull(); + }); + + it("should make a chain of certificates", async () => { + const mockFortifyClient: Partial = { + getProviderById: vi.fn().mockResolvedValue({ + certStorage: { + getChain: vi + .fn() + .mockResolvedValue([ + { value: new ArrayBuffer(0) }, + { value: new ArrayBuffer(0) }, + ]), + }, + }), + }; + const { result } = renderHook(() => + useCertificateViewerDialog({ + ...defaultProps, + fortifyClient: mockFortifyClient as FortifyAPI, + }) + ); + + await act(async () => { + await result.current.open(defaultOpenProps); + }); + + const DialogComponent = result.current.dialog(); + expect(mockFortifyClient.getProviderById).toHaveBeenCalled(); expect(DialogComponent).not.toBeNull(); - expect(DialogComponent?.props.certificates).toStrictEqual([certificate]); + expect(DialogComponent?.props.certificates.length).toBe(2); }); });