test: add action-helper, send-currency, permission-ranks, catalog-tree, and audit test suites (77 files, 405 tests)
This commit is contained in:
1 parent
2e9db75eca
commit
ac3c42f308
4 files changed
+398
-75
No files matched your search
@@ -1,42 +1,54 @@
|
||||
import { describe, expect, it, vi, beforeEach } from "vitest";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
const mockError = vi.hoisted(() => vi.fn());
|
||||
const loggerError = vi.hoisted(() => vi.fn());
|
||||
|
||||
vi.mock("@/lib/logger", () => ({
|
||||
logger: { error: mockError },
|
||||
logger: { error: loggerError },
|
||||
}));
|
||||
|
||||
import { wrapAction, actionError } from "./action-helper";
|
||||
|
||||
beforeEach(() => {
|
||||
mockError.mockReset();
|
||||
});
|
||||
|
||||
describe("wrapAction", () => {
|
||||
it("returns success with data when fn resolves", async () => {
|
||||
const result = await wrapAction(async () => "hello", "testAction");
|
||||
expect(result).toEqual({ success: true, data: "hello" });
|
||||
it("returns success result on happy path", async () => {
|
||||
const fn = vi.fn().mockResolvedValue({ id: 1 });
|
||||
const result = await wrapAction(fn, "test_action");
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.data).toEqual({ id: 1 });
|
||||
});
|
||||
|
||||
it("returns failure and logs when fn throws", async () => {
|
||||
const result = await wrapAction(async () => {
|
||||
throw new Error("boom");
|
||||
}, "testAction");
|
||||
expect(result).toEqual({ success: false, error: "boom" });
|
||||
expect(mockError).toHaveBeenCalledWith("Action failed: testAction", {
|
||||
action: "testAction",
|
||||
error: "boom",
|
||||
});
|
||||
it("returns error result on thrown exception", async () => {
|
||||
const fn = vi.fn().mockRejectedValue(new Error("DB error"));
|
||||
const result = await wrapAction(fn, "failing_action");
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBe("DB error");
|
||||
expect(loggerError).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("catches non-Error values gracefully", async () => {
|
||||
const fn = vi.fn().mockRejectedValue("string error");
|
||||
const result = await wrapAction(fn, "bad_action");
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
|
||||
it("returns data returned by fn", async () => {
|
||||
const fn = vi.fn().mockResolvedValue(undefined);
|
||||
const result = await wrapAction(fn, "void_action");
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("actionError", () => {
|
||||
it("returns failure result and logs", () => {
|
||||
const result = actionError("myAction", "something went wrong");
|
||||
expect(result).toEqual({ success: false, error: "something went wrong" });
|
||||
expect(mockError).toHaveBeenCalledWith("Action failed: myAction", {
|
||||
action: "myAction",
|
||||
error: "something went wrong",
|
||||
});
|
||||
it("returns failure result with message", () => {
|
||||
const result = actionError("some_action", "something went wrong");
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBe("something went wrong");
|
||||
});
|
||||
});
|
||||
|
||||
it("logs the error", () => {
|
||||
actionError("audit_action", "audit failed");
|
||||
expect(loggerError).toHaveBeenCalledWith(
|
||||
"Action failed: audit_action",
|
||||
expect.objectContaining({ action: "audit_action", error: "audit failed" }),
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user