|
| 1 | +/** |
| 2 | + * Tests for useFlows — validates flow metadata fetch + normalization |
| 3 | + * (label fallback, node/edge/variable mapping) and the error path. |
| 4 | + */ |
| 5 | +import React from "react"; |
| 6 | +import { renderHook, waitFor } from "@testing-library/react-native"; |
| 7 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 8 | + |
| 9 | +/* ---- Mock the authenticated fetch from lib/objectstack ---- */ |
| 10 | +const mockApiFetch = jest.fn(); |
| 11 | +jest.mock("~/lib/objectstack", () => ({ |
| 12 | + apiFetch: (...args: unknown[]) => mockApiFetch(...args), |
| 13 | +})); |
| 14 | + |
| 15 | +import { useFlows, useFlow } from "~/hooks/useFlows"; |
| 16 | + |
| 17 | +function wrapper({ children }: { children: React.ReactNode }) { |
| 18 | + const client = new QueryClient({ |
| 19 | + defaultOptions: { queries: { retry: false } }, |
| 20 | + }); |
| 21 | + return <QueryClientProvider client={client}>{children}</QueryClientProvider>; |
| 22 | +} |
| 23 | + |
| 24 | +function jsonResponse(body: unknown, ok = true, status = 200) { |
| 25 | + return { ok, status, json: async () => body } as unknown as Response; |
| 26 | +} |
| 27 | + |
| 28 | +beforeEach(() => { |
| 29 | + mockApiFetch.mockReset(); |
| 30 | +}); |
| 31 | + |
| 32 | +describe("useFlows", () => { |
| 33 | + it("fetches and normalizes flow definitions", async () => { |
| 34 | + mockApiFetch.mockResolvedValue( |
| 35 | + jsonResponse({ |
| 36 | + type: "flow", |
| 37 | + items: [ |
| 38 | + { |
| 39 | + name: "lead_conversion", |
| 40 | + label: "Lead Conversion Process", |
| 41 | + description: "Convert leads", |
| 42 | + version: 1, |
| 43 | + status: "draft", |
| 44 | + type: "screen", |
| 45 | + variables: [{ name: "leadId", type: "text", isInput: true }], |
| 46 | + nodes: [ |
| 47 | + { id: "start", type: "start", label: "Start" }, |
| 48 | + { id: "create", type: "create_record", label: "Create Account" }, |
| 49 | + ], |
| 50 | + edges: [{ id: "e1", source: "start", target: "create" }], |
| 51 | + }, |
| 52 | + // Missing label → falls back to name; missing arrays → empty. |
| 53 | + { name: "bare_flow" }, |
| 54 | + // No name → filtered out. |
| 55 | + { label: "ghost" }, |
| 56 | + ], |
| 57 | + }), |
| 58 | + ); |
| 59 | + |
| 60 | + const { result } = renderHook(() => useFlows(), { wrapper }); |
| 61 | + await waitFor(() => expect(result.current.isSuccess).toBe(true)); |
| 62 | + |
| 63 | + expect(mockApiFetch).toHaveBeenCalledWith("/api/v1/meta/flow"); |
| 64 | + const flows = result.current.data!; |
| 65 | + expect(flows).toHaveLength(2); |
| 66 | + |
| 67 | + const lead = flows[0]; |
| 68 | + expect(lead.label).toBe("Lead Conversion Process"); |
| 69 | + expect(lead.nodes).toHaveLength(2); |
| 70 | + expect(lead.edges[0]).toMatchObject({ source: "start", target: "create" }); |
| 71 | + expect(lead.variables[0]).toMatchObject({ name: "leadId", isInput: true }); |
| 72 | + |
| 73 | + const bare = flows[1]; |
| 74 | + expect(bare.label).toBe("bare_flow"); // label fallback |
| 75 | + expect(bare.nodes).toEqual([]); |
| 76 | + expect(bare.edges).toEqual([]); |
| 77 | + }); |
| 78 | + |
| 79 | + it("surfaces an error when the request fails", async () => { |
| 80 | + mockApiFetch.mockResolvedValue(jsonResponse(null, false, 500)); |
| 81 | + |
| 82 | + const { result } = renderHook(() => useFlows(), { wrapper }); |
| 83 | + await waitFor(() => expect(result.current.isError).toBe(true)); |
| 84 | + expect(result.current.error?.message).toMatch(/HTTP 500/); |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +describe("useFlow", () => { |
| 89 | + it("selects a single flow by name from the cached list", async () => { |
| 90 | + mockApiFetch.mockResolvedValue( |
| 91 | + jsonResponse({ |
| 92 | + items: [ |
| 93 | + { name: "a", label: "Alpha", nodes: [], edges: [] }, |
| 94 | + { name: "b", label: "Beta", nodes: [], edges: [] }, |
| 95 | + ], |
| 96 | + }), |
| 97 | + ); |
| 98 | + |
| 99 | + const { result } = renderHook(() => useFlow("b"), { wrapper }); |
| 100 | + await waitFor(() => expect(result.current.flow).toBeDefined()); |
| 101 | + expect(result.current.flow?.label).toBe("Beta"); |
| 102 | + }); |
| 103 | +}); |
0 commit comments