|
| 1 | +import { renderHook } from "@testing-library/react"; |
| 2 | +import type { FlowType } from "@/types/flow"; |
| 3 | +import useDeleteFlow from "../use-delete-flow"; |
| 4 | + |
| 5 | +const mockSetFlows = jest.fn(); |
| 6 | +const mockMutate = jest.fn(); |
| 7 | + |
| 8 | +const flowsState: { flows: FlowType[] } = { flows: [] }; |
| 9 | + |
| 10 | +jest.mock("@/controllers/API/queries/flows/use-delete-delete-flows", () => ({ |
| 11 | + useDeleteDeleteFlows: () => ({ mutate: mockMutate, isPending: false }), |
| 12 | +})); |
| 13 | + |
| 14 | +type Selector<T> = (state: T) => unknown; |
| 15 | +type FlowsManagerState = { flows: FlowType[]; setFlows: jest.Mock }; |
| 16 | + |
| 17 | +jest.mock("@/stores/flowsManagerStore", () => { |
| 18 | + const store = Object.assign( |
| 19 | + (selector: Selector<FlowsManagerState>) => |
| 20 | + selector({ flows: flowsState.flows, setFlows: mockSetFlows }), |
| 21 | + { getState: () => ({ flows: flowsState.flows, setFlows: mockSetFlows }) }, |
| 22 | + ); |
| 23 | + return { __esModule: true, default: store }; |
| 24 | +}); |
| 25 | + |
| 26 | +jest.mock("@/stores/typesStore", () => ({ |
| 27 | + useTypesStore: { setState: jest.fn() }, |
| 28 | +})); |
| 29 | + |
| 30 | +jest.mock("@/utils/reactflowUtils", () => ({ |
| 31 | + processFlows: (flows: FlowType[]) => ({ data: {}, flows }), |
| 32 | + extractFieldsFromComponenents: () => ({}), |
| 33 | +})); |
| 34 | + |
| 35 | +const makeFlow = (id: string): FlowType => |
| 36 | + ({ id, name: id, data: { nodes: [], edges: [], viewport: {} } }) as FlowType; |
| 37 | + |
| 38 | +describe("useDeleteFlow", () => { |
| 39 | + beforeEach(() => { |
| 40 | + jest.clearAllMocks(); |
| 41 | + flowsState.flows = []; |
| 42 | + }); |
| 43 | + |
| 44 | + it("should_remove_only_deleted_flows_when_delete_succeeds", async () => { |
| 45 | + flowsState.flows = [makeFlow("flow-a"), makeFlow("flow-b")]; |
| 46 | + mockMutate.mockImplementation((_vars, { onSuccess }) => onSuccess()); |
| 47 | + |
| 48 | + const { result } = renderHook(() => useDeleteFlow()); |
| 49 | + await result.current.deleteFlow({ id: "flow-a" }); |
| 50 | + |
| 51 | + expect(mockSetFlows).toHaveBeenCalledWith([ |
| 52 | + expect.objectContaining({ id: "flow-b" }), |
| 53 | + ]); |
| 54 | + }); |
| 55 | + |
| 56 | + it("should_keep_flow_created_while_delete_was_in_flight", async () => { |
| 57 | + flowsState.flows = [makeFlow("placeholder")]; |
| 58 | + // Simulate a flow landing in the store (e.g. a template flow created by |
| 59 | + // the welcome overlay handoff) before the DELETE response arrives. |
| 60 | + mockMutate.mockImplementation((_vars, { onSuccess }) => { |
| 61 | + flowsState.flows = [makeFlow("placeholder"), makeFlow("template-flow")]; |
| 62 | + onSuccess(); |
| 63 | + }); |
| 64 | + |
| 65 | + const { result } = renderHook(() => useDeleteFlow()); |
| 66 | + await result.current.deleteFlow({ id: "placeholder" }); |
| 67 | + |
| 68 | + expect(mockSetFlows).toHaveBeenCalledWith([ |
| 69 | + expect.objectContaining({ id: "template-flow" }), |
| 70 | + ]); |
| 71 | + }); |
| 72 | + |
| 73 | + it("should_reject_when_delete_fails", async () => { |
| 74 | + flowsState.flows = [makeFlow("flow-a")]; |
| 75 | + const error = new Error("boom"); |
| 76 | + mockMutate.mockImplementation((_vars, { onError }) => onError(error)); |
| 77 | + |
| 78 | + const { result } = renderHook(() => useDeleteFlow()); |
| 79 | + |
| 80 | + await expect(result.current.deleteFlow({ id: "flow-a" })).rejects.toThrow( |
| 81 | + "boom", |
| 82 | + ); |
| 83 | + expect(mockSetFlows).not.toHaveBeenCalled(); |
| 84 | + }); |
| 85 | +}); |
0 commit comments