|
| 1 | +import getSendingDomain from "../getSendingDomain"; |
| 2 | +import { client } from "../../../client"; |
| 3 | + |
| 4 | +jest.mock("../../../client", () => ({ |
| 5 | + client: { |
| 6 | + sendingDomains: { |
| 7 | + get: jest.fn(), |
| 8 | + }, |
| 9 | + }, |
| 10 | +})); |
| 11 | + |
| 12 | +const originalEnv = { ...process.env }; |
| 13 | + |
| 14 | +describe("getSendingDomain", () => { |
| 15 | + const mockDomain = { |
| 16 | + id: 3938, |
| 17 | + domain_name: "example.com", |
| 18 | + demo: false, |
| 19 | + compliance_status: "compliant", |
| 20 | + dns_verified: true, |
| 21 | + dns_verified_at: "2024-12-26T09:40:44.161Z", |
| 22 | + dns_records: [ |
| 23 | + { |
| 24 | + key: "spf", |
| 25 | + type: "TXT", |
| 26 | + name: "", |
| 27 | + value: "v=spf1 include:_spf.smtp.mailtrap.live ~all", |
| 28 | + status: "pass", |
| 29 | + domain: "example.com", |
| 30 | + }, |
| 31 | + ], |
| 32 | + }; |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + jest.clearAllMocks(); |
| 36 | + Object.assign(process.env, { MAILTRAP_ACCOUNT_ID: "12345" }); |
| 37 | + (client.sendingDomains.get as jest.Mock).mockResolvedValue(mockDomain); |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(() => { |
| 41 | + Object.assign(process.env, originalEnv); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should get sending domain successfully", async () => { |
| 45 | + const result = await getSendingDomain({ sending_domain_id: 3938 }); |
| 46 | + |
| 47 | + expect(client.sendingDomains.get).toHaveBeenCalledWith(3938); |
| 48 | + expect(result.content[0].text).toContain("example.com"); |
| 49 | + expect(result.content[0].text).toContain("ID: 3938"); |
| 50 | + expect(result.content[0].text).toContain("DNS verified: true"); |
| 51 | + expect(result.content[0].text).toContain("spf"); |
| 52 | + expect(result.content[0].text).toContain("pass"); |
| 53 | + expect(result.isError).toBeUndefined(); |
| 54 | + }); |
| 55 | + |
| 56 | + it("should include setup instructions when include_setup_instructions is true", async () => { |
| 57 | + const result = await getSendingDomain({ |
| 58 | + sending_domain_id: 3938, |
| 59 | + include_setup_instructions: true, |
| 60 | + }); |
| 61 | + |
| 62 | + expect(client.sendingDomains.get).toHaveBeenCalledWith(3938); |
| 63 | + expect(result.content[0].text).toContain("Domain: example.com"); |
| 64 | + expect(result.content[0].text).toContain("Add DNS records for example.com"); |
| 65 | + expect(result.content[0].text).toContain("What?"); |
| 66 | + expect(result.content[0].text).toContain("Why?"); |
| 67 | + expect(result.content[0].text).toContain("DNS records to add"); |
| 68 | + expect(result.content[0].text).toContain("docs.mailtrap.io"); |
| 69 | + expect(result.isError).toBeUndefined(); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should not include setup instructions when include_setup_instructions is false or omitted", async () => { |
| 73 | + const result = await getSendingDomain({ |
| 74 | + sending_domain_id: 3938, |
| 75 | + include_setup_instructions: false, |
| 76 | + }); |
| 77 | + |
| 78 | + expect(result.content[0].text).not.toContain("Add DNS records for"); |
| 79 | + expect(result.content[0].text).not.toContain("What?"); |
| 80 | + }); |
| 81 | + |
| 82 | + it("should require MAILTRAP_ACCOUNT_ID", async () => { |
| 83 | + delete process.env.MAILTRAP_ACCOUNT_ID; |
| 84 | + |
| 85 | + const result = await getSendingDomain({ sending_domain_id: 3938 }); |
| 86 | + |
| 87 | + expect(client.sendingDomains.get).not.toHaveBeenCalled(); |
| 88 | + expect(result.isError).toBe(true); |
| 89 | + }); |
| 90 | + |
| 91 | + it("should handle API failure", async () => { |
| 92 | + (client.sendingDomains.get as jest.Mock).mockRejectedValue( |
| 93 | + new Error("Not found") |
| 94 | + ); |
| 95 | + |
| 96 | + const result = await getSendingDomain({ sending_domain_id: 999 }); |
| 97 | + |
| 98 | + expect(result.content[0].text).toContain("Failed to get sending domain"); |
| 99 | + expect(result.isError).toBe(true); |
| 100 | + }); |
| 101 | +}); |
0 commit comments