Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandr-slobodian committed Oct 23, 2024
1 parent 77f989f commit 2dea9f5
Showing 1 changed file with 80 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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<typeof import("@peculiar/x509")>();

return {
...originalModule,
X509Certificate: vi.fn().mockImplementation(() => ({
rawData: new ArrayBuffer(0),
subject: "Test Subject",
subjectName: "Rest Subject Name",
})),
};
});

describe("useCertificateViewerDialog", () => {
const providers = [
Expand All @@ -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<FortifyAPI> = {
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);
});
});

0 comments on commit 2dea9f5

Please sign in to comment.