Skip to content

Commit

Permalink
Add test for useCertificateDeleteDialog hook
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandr-slobodian committed Aug 5, 2024
1 parent d085c3d commit b198bf3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<FortifyAPI> = {
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
);
});
});
10 changes: 9 additions & 1 deletion utils/testing/index.ts
Original file line number Diff line number Diff line change
@@ -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";

0 comments on commit b198bf3

Please sign in to comment.