|
| 1 | +import createContactExport from "../createContactExport"; |
| 2 | +import { requireClient } from "../../../client"; |
| 3 | + |
| 4 | +const mockClient = { |
| 5 | + contactExports: { |
| 6 | + create: jest.fn(), |
| 7 | + }, |
| 8 | +}; |
| 9 | + |
| 10 | +jest.mock("../../../client", () => ({ |
| 11 | + requireClient: jest.fn(() => mockClient), |
| 12 | +})); |
| 13 | + |
| 14 | +describe("createContactExport", () => { |
| 15 | + beforeEach(() => { |
| 16 | + jest.clearAllMocks(); |
| 17 | + (requireClient as jest.Mock).mockReturnValue(mockClient); |
| 18 | + }); |
| 19 | + |
| 20 | + it("submits the export and returns the response as JSON", async () => { |
| 21 | + mockClient.contactExports.create.mockResolvedValue({ |
| 22 | + id: 42, |
| 23 | + status: "started", |
| 24 | + created_at: "2026-05-20T10:00:00Z", |
| 25 | + updated_at: "2026-05-20T10:00:00Z", |
| 26 | + url: null, |
| 27 | + }); |
| 28 | + |
| 29 | + const result = await createContactExport({ |
| 30 | + filters: [{ name: "list_id", operator: "equal", value: 10 }], |
| 31 | + }); |
| 32 | + |
| 33 | + expect(requireClient).toHaveBeenCalledWith("contact exports"); |
| 34 | + expect(mockClient.contactExports.create).toHaveBeenCalledWith({ |
| 35 | + filters: [{ name: "list_id", operator: "equal", value: 10 }], |
| 36 | + }); |
| 37 | + expect(result.content[0].text).toContain('"id": 42'); |
| 38 | + expect(result.content[0].text).toContain('"status": "started"'); |
| 39 | + expect(result.isError).toBeUndefined(); |
| 40 | + }); |
| 41 | + |
| 42 | + it("submits filters without a value for is_empty / is_not_empty operators", async () => { |
| 43 | + mockClient.contactExports.create.mockResolvedValue({ |
| 44 | + id: 43, |
| 45 | + status: "started", |
| 46 | + }); |
| 47 | + |
| 48 | + await createContactExport({ |
| 49 | + filters: [{ name: "email", operator: "is_empty" }], |
| 50 | + }); |
| 51 | + |
| 52 | + expect(mockClient.contactExports.create).toHaveBeenCalledWith({ |
| 53 | + filters: [{ name: "email", operator: "is_empty" }], |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it("surfaces API errors", async () => { |
| 58 | + mockClient.contactExports.create.mockRejectedValue( |
| 59 | + new Error("invalid filter") |
| 60 | + ); |
| 61 | + |
| 62 | + const result = await createContactExport({ |
| 63 | + filters: [{ name: "bad", operator: "equal", value: "x" }], |
| 64 | + }); |
| 65 | + |
| 66 | + expect(result.isError).toBe(true); |
| 67 | + expect(result.content[0].text).toBe( |
| 68 | + "Failed to create contact export: invalid filter" |
| 69 | + ); |
| 70 | + }); |
| 71 | +}); |
0 commit comments