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 Aug 7, 2024
1 parent 2161dde commit 68a4c63
Showing 1 changed file with 85 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
};
});
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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",
})
);
});
});

0 comments on commit 68a4c63

Please sign in to comment.