Skip to content

Commit

Permalink
Add more tests for useCertificateDeleteDialog hook
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandr-slobodian committed Oct 29, 2024
1 parent 2c8db72 commit 32c2119
Showing 1 changed file with 147 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import { useCertificateDeleteDialog } from "./useCertificateDeleteDialog";
import type { IProviderInfo } from "@peculiar/fortify-client-core";
import type { FortifyAPI } from "@peculiar/fortify-client-core";

const addToastMock = vi.fn();

vi.mock("@peculiar/react-components", async () => {
const actual = await vi.importActual("@peculiar/react-components");
return {
...actual,
useToast: () => ({
addToast: vi.fn(),
addToast: addToastMock,
}),
};
});
Expand All @@ -22,10 +24,13 @@ describe("useCertificateDeleteDialog", () => {
},
] as IProviderInfo[];

it("Should initialize and call onSuccess", async () => {
const certificateIndex = "1";
const providerId = providers[0].id;
const defaultOpenProps = {
certificateIndex: "1",
providerId: providers[0].id,
label: "Certificate Name",
};

it("Should open dialog, handle onSuccess & close dialog", async () => {
const mockFortifyClient: Partial<FortifyAPI> = {
removeCertificateById: vi.fn().mockResolvedValue({}),
};
Expand All @@ -43,24 +48,152 @@ describe("useCertificateDeleteDialog", () => {
expect(result.current.open).toBeInstanceOf(Function);

act(() => {
result.current.open({
certificateIndex,
providerId,
label: "Message",
});
result.current.open(defaultOpenProps);
});

const DialogComponent = result.current.dialog();
await act(async () => {
DialogComponent &&
(await DialogComponent.props.onDeleteClick(
defaultOpenProps.certificateIndex
));
});

expect(onSuccessMock).toHaveBeenCalledWith(defaultOpenProps.providerId);
expect(mockFortifyClient.removeCertificateById).toHaveBeenCalledWith(
defaultOpenProps.providerId,
defaultOpenProps.certificateIndex
);
expect(addToastMock).toHaveBeenCalledWith(
expect.objectContaining({
message: "Certificate deleted.",
})
);

act(() => {
DialogComponent?.props.onDialogClose();
});

expect(result.current.dialog()).toBeNull();
addToastMock.mockClear();
});

it("Should close dialog if current provider is not found", async () => {
const { result, rerender } = renderHook(
(localProviders) =>
useCertificateDeleteDialog({
providers: localProviders,
onSuccess: vi.fn(),
fortifyClient: null,
}),
{ initialProps: providers }
);

act(() => {
result.current.open(defaultOpenProps);
});

rerender([
{
id: "2",
},
] as IProviderInfo[]);

expect(result.current.dialog()).toBeNull();
});

it("Should show error message if certificate deletion fails", async () => {
const mockFortifyClient: Partial<FortifyAPI> = {
removeCertificateById: vi.fn().mockImplementation(() => {
throw new Error("Error");
}),
};
const onSuccessMock = vi.fn();

const { result } = renderHook(() =>
useCertificateDeleteDialog({
providers: providers,
onSuccess: onSuccessMock,
fortifyClient: mockFortifyClient as FortifyAPI,
})
);

act(() => {
result.current.open(defaultOpenProps);
});

await act(async () => {
const DialogComponent = result.current.dialog();

DialogComponent &&
(await DialogComponent.props.onDeleteClick(certificateIndex));
(await DialogComponent.props.onDeleteClick(
defaultOpenProps.certificateIndex
));
});

expect(onSuccessMock).toHaveBeenCalledWith(providerId);
expect(mockFortifyClient.removeCertificateById).toHaveBeenCalledWith(
providerId,
certificateIndex
expect(onSuccessMock).not.toHaveBeenCalled();
expect(addToastMock).toHaveBeenCalledWith(
expect.objectContaining({
message:
"Failed to delete certificate because of error. Please try again.",
})
);
addToastMock.mockClear();
});

it("Shouldn't call if no certificate id is provided", async () => {
const mockFortifyClient: Partial<FortifyAPI> = {
removeCertificateById: vi.fn().mockResolvedValue({}),
};
const onSuccessMock = vi.fn();

const { result } = renderHook(() =>
useCertificateDeleteDialog({
providers: providers,
onSuccess: onSuccessMock,
fortifyClient: mockFortifyClient as FortifyAPI,
})
);

act(() => {
result.current.open(defaultOpenProps);
});

await act(async () => {
const DialogComponent = result.current.dialog();

DialogComponent && (await DialogComponent.props.onDeleteClick(undefined));
});

expect(onSuccessMock).not.toHaveBeenCalled();
expect(addToastMock).not.toHaveBeenCalled();
});

it("Shouldn't call if no fortifyClient is provided", async () => {
const onSuccessMock = vi.fn();

const { result } = renderHook(() =>
useCertificateDeleteDialog({
providers: providers,
onSuccess: onSuccessMock,
fortifyClient: null,
})
);

act(() => {
result.current.open(defaultOpenProps);
});

await act(async () => {
const DialogComponent = result.current.dialog();

DialogComponent &&
(await DialogComponent.props.onDeleteClick(
defaultOpenProps.certificateIndex
));
});

expect(onSuccessMock).not.toHaveBeenCalled();
expect(addToastMock).not.toHaveBeenCalled();
});
});

0 comments on commit 32c2119

Please sign in to comment.