|
| 1 | +/** |
| 2 | + * Tests for useAutomation – validates automation trigger |
| 3 | + * and approval/rejection operations. |
| 4 | + */ |
| 5 | +import { renderHook, act } from "@testing-library/react-native"; |
| 6 | + |
| 7 | +/* ---- Mock useClient from SDK ---- */ |
| 8 | +const mockTrigger = jest.fn(); |
| 9 | +const mockApprove = jest.fn(); |
| 10 | +const mockReject = jest.fn(); |
| 11 | + |
| 12 | +const mockClient = { |
| 13 | + automation: { trigger: mockTrigger }, |
| 14 | + workflow: { approve: mockApprove, reject: mockReject }, |
| 15 | +}; |
| 16 | + |
| 17 | +jest.mock("@objectstack/client-react", () => ({ |
| 18 | + useClient: () => mockClient, |
| 19 | +})); |
| 20 | + |
| 21 | +import { useAutomation } from "~/hooks/useAutomation"; |
| 22 | + |
| 23 | +beforeEach(() => { |
| 24 | + mockTrigger.mockReset(); |
| 25 | + mockApprove.mockReset(); |
| 26 | + mockReject.mockReset(); |
| 27 | +}); |
| 28 | + |
| 29 | +describe("useAutomation", () => { |
| 30 | + it("triggers an automation flow with payload", async () => { |
| 31 | + mockTrigger.mockResolvedValue({ |
| 32 | + executionId: "exec-1", |
| 33 | + message: "Started", |
| 34 | + data: { status: "ok" }, |
| 35 | + }); |
| 36 | + |
| 37 | + const { result } = renderHook(() => useAutomation()); |
| 38 | + |
| 39 | + let triggerResult: unknown; |
| 40 | + await act(async () => { |
| 41 | + triggerResult = await result.current.trigger("onboard-user", { |
| 42 | + userId: "123", |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + expect(mockTrigger).toHaveBeenCalledWith("onboard-user", { |
| 47 | + userId: "123", |
| 48 | + }); |
| 49 | + expect(triggerResult).toEqual({ |
| 50 | + success: true, |
| 51 | + executionId: "exec-1", |
| 52 | + message: "Started", |
| 53 | + data: { status: "ok" }, |
| 54 | + }); |
| 55 | + expect(result.current.isLoading).toBe(false); |
| 56 | + expect(result.current.error).toBeNull(); |
| 57 | + }); |
| 58 | + |
| 59 | + it("triggers without payload", async () => { |
| 60 | + mockTrigger.mockResolvedValue({}); |
| 61 | + |
| 62 | + const { result } = renderHook(() => useAutomation()); |
| 63 | + |
| 64 | + await act(async () => { |
| 65 | + await result.current.trigger("daily-report"); |
| 66 | + }); |
| 67 | + |
| 68 | + expect(mockTrigger).toHaveBeenCalledWith("daily-report", {}); |
| 69 | + }); |
| 70 | + |
| 71 | + it("handles trigger error", async () => { |
| 72 | + mockTrigger.mockRejectedValue(new Error("Flow not found")); |
| 73 | + |
| 74 | + const { result } = renderHook(() => useAutomation()); |
| 75 | + |
| 76 | + await act(async () => { |
| 77 | + await expect( |
| 78 | + result.current.trigger("nonexistent"), |
| 79 | + ).rejects.toThrow("Flow not found"); |
| 80 | + }); |
| 81 | + |
| 82 | + expect(result.current.error?.message).toBe("Flow not found"); |
| 83 | + }); |
| 84 | + |
| 85 | + it("approves a workflow step", async () => { |
| 86 | + mockApprove.mockResolvedValue({ success: true }); |
| 87 | + |
| 88 | + const { result } = renderHook(() => useAutomation()); |
| 89 | + |
| 90 | + await act(async () => { |
| 91 | + await result.current.approve("tasks", "rec-1", "LGTM"); |
| 92 | + }); |
| 93 | + |
| 94 | + expect(mockApprove).toHaveBeenCalledWith({ |
| 95 | + object: "tasks", |
| 96 | + recordId: "rec-1", |
| 97 | + comment: "LGTM", |
| 98 | + }); |
| 99 | + expect(result.current.error).toBeNull(); |
| 100 | + }); |
| 101 | + |
| 102 | + it("handles approval error", async () => { |
| 103 | + mockApprove.mockRejectedValue(new Error("Not authorized")); |
| 104 | + |
| 105 | + const { result } = renderHook(() => useAutomation()); |
| 106 | + |
| 107 | + await act(async () => { |
| 108 | + await expect( |
| 109 | + result.current.approve("tasks", "rec-1"), |
| 110 | + ).rejects.toThrow("Not authorized"); |
| 111 | + }); |
| 112 | + |
| 113 | + expect(result.current.error?.message).toBe("Not authorized"); |
| 114 | + }); |
| 115 | + |
| 116 | + it("rejects a workflow step with reason", async () => { |
| 117 | + mockReject.mockResolvedValue({ success: true }); |
| 118 | + |
| 119 | + const { result } = renderHook(() => useAutomation()); |
| 120 | + |
| 121 | + await act(async () => { |
| 122 | + await result.current.reject("tasks", "rec-1", "Needs changes", "See comments"); |
| 123 | + }); |
| 124 | + |
| 125 | + expect(mockReject).toHaveBeenCalledWith({ |
| 126 | + object: "tasks", |
| 127 | + recordId: "rec-1", |
| 128 | + reason: "Needs changes", |
| 129 | + comment: "See comments", |
| 130 | + }); |
| 131 | + expect(result.current.error).toBeNull(); |
| 132 | + }); |
| 133 | + |
| 134 | + it("handles rejection error", async () => { |
| 135 | + mockReject.mockRejectedValue(new Error("Rejection failed")); |
| 136 | + |
| 137 | + const { result } = renderHook(() => useAutomation()); |
| 138 | + |
| 139 | + await act(async () => { |
| 140 | + await expect( |
| 141 | + result.current.reject("tasks", "rec-1", "Bad"), |
| 142 | + ).rejects.toThrow("Rejection failed"); |
| 143 | + }); |
| 144 | + |
| 145 | + expect(result.current.error?.message).toBe("Rejection failed"); |
| 146 | + }); |
| 147 | +}); |
0 commit comments