Files
Alfred/tests/web/use-confirm-action.test.ts

54 lines
1.6 KiB
TypeScript

import { XProvider } from "@ant-design/x";
import { act, renderHook } from "@testing-library/react";
import { App } from "antd";
import { describe, expect, it } from "bun:test";
import { createElement } from "react";
import { useConfirmAction } from "../../src/web/shared/hooks/useConfirmAction";
function createWrapper() {
return ({ children }: { children: React.ReactNode }) =>
createElement(XProvider, null, createElement(App, null, children));
}
describe("useConfirmAction", () => {
it("calls action successfully and resolves", async () => {
let resolved = false;
const action = () => {
resolved = true;
};
const { result } = renderHook(() => useConfirmAction(), { wrapper: createWrapper() });
await act(() => result.current.confirmAction(action, "成功"));
expect(resolved).toBe(true);
});
it("handles action error without throwing", async () => {
const action = () => {
throw new Error("失败");
};
const { result } = renderHook(() => useConfirmAction(), { wrapper: createWrapper() });
await act(() => result.current.confirmAction(action, "成功"));
expect(resolved).toBe(true);
});
it("handles action error without throwing", async () => {
const action = () => {
throw new Error("失败");
};
const { result } = renderHook(() => useConfirmAction(), { wrapper: createWrapper() });
let threw = false;
try {
await act(async () => {
await result.current.confirmAction(action, "成功");
});
} catch {
threw = true;
}
expect(threw).toBe(false);
});
});