|
| 1 | +import { IterableClient } from "../../src/client/index.js"; |
| 2 | + |
| 3 | +describe("IterableClient", () => { |
| 4 | + describe("Constructor with baseUrl", () => { |
| 5 | + it("should use US endpoint when explicitly provided", () => { |
| 6 | + const client = new IterableClient({ |
| 7 | + apiKey: "a1b2c3d4e5f6789012345678901234ab", |
| 8 | + baseUrl: "https://api.iterable.com", |
| 9 | + }); |
| 10 | + |
| 11 | + expect(client).toBeDefined(); |
| 12 | + expect(client.client.defaults.baseURL).toBe("https://api.iterable.com"); |
| 13 | + }); |
| 14 | + |
| 15 | + it("should use EU endpoint when explicitly provided", () => { |
| 16 | + const client = new IterableClient({ |
| 17 | + apiKey: "a1b2c3d4e5f6789012345678901234ab", |
| 18 | + baseUrl: "https://api.eu.iterable.com", |
| 19 | + }); |
| 20 | + |
| 21 | + expect(client).toBeDefined(); |
| 22 | + expect(client.client.defaults.baseURL).toBe( |
| 23 | + "https://api.eu.iterable.com" |
| 24 | + ); |
| 25 | + }); |
| 26 | + |
| 27 | + it("should use custom endpoint when provided", () => { |
| 28 | + const client = new IterableClient({ |
| 29 | + apiKey: "a1b2c3d4e5f6789012345678901234ab", |
| 30 | + baseUrl: "https://custom.api.example.com", |
| 31 | + }); |
| 32 | + |
| 33 | + expect(client).toBeDefined(); |
| 34 | + expect(client.client.defaults.baseURL).toBe( |
| 35 | + "https://custom.api.example.com" |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should accept all valid IterableConfig properties", () => { |
| 40 | + const client = new IterableClient({ |
| 41 | + apiKey: "a1b2c3d4e5f6789012345678901234ab", |
| 42 | + baseUrl: "https://api.iterable.com", |
| 43 | + timeout: 45000, |
| 44 | + debug: false, |
| 45 | + debugVerbose: true, |
| 46 | + customHeaders: { |
| 47 | + "X-Custom-1": "value1", |
| 48 | + "X-Custom-2": "value2", |
| 49 | + }, |
| 50 | + }); |
| 51 | + |
| 52 | + expect(client).toBeDefined(); |
| 53 | + |
| 54 | + // Verify axios configuration |
| 55 | + expect(client.client.defaults.baseURL).toBe("https://api.iterable.com"); |
| 56 | + expect(client.client.defaults.timeout).toBe(45000); |
| 57 | + |
| 58 | + // Verify API key is set in headers |
| 59 | + expect(client.client.defaults.headers["Api-Key"]).toBe( |
| 60 | + "a1b2c3d4e5f6789012345678901234ab" |
| 61 | + ); |
| 62 | + |
| 63 | + // Verify custom headers are merged into defaults |
| 64 | + expect(client.client.defaults.headers["X-Custom-1"]).toBe("value1"); |
| 65 | + expect(client.client.defaults.headers["X-Custom-2"]).toBe("value2"); |
| 66 | + |
| 67 | + // Verify standard headers are still present |
| 68 | + expect(client.client.defaults.headers["Content-Type"]).toBe( |
| 69 | + "application/json" |
| 70 | + ); |
| 71 | + expect(client.client.defaults.headers["User-Agent"]).toContain( |
| 72 | + "iterable-api" |
| 73 | + ); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe("Constructor with Injected Client", () => { |
| 78 | + it("should use the injected axios instance ignoring baseUrl config", () => { |
| 79 | + const mockAxios = { |
| 80 | + defaults: { |
| 81 | + // note this is the axios instance's baseURL, not the IterableConfig's baseUrl |
| 82 | + baseURL: "https://mock.example.com", |
| 83 | + }, |
| 84 | + interceptors: { |
| 85 | + request: { use: jest.fn() }, |
| 86 | + response: { use: jest.fn() }, |
| 87 | + }, |
| 88 | + } as any; |
| 89 | + |
| 90 | + const client = new IterableClient( |
| 91 | + { |
| 92 | + apiKey: "a1b2c3d4e5f6789012345678901234ab", |
| 93 | + baseUrl: "https://api.iterable.com", |
| 94 | + }, |
| 95 | + mockAxios |
| 96 | + ); |
| 97 | + |
| 98 | + expect(client).toBeDefined(); |
| 99 | + expect(client.client).toBe(mockAxios); |
| 100 | + // The injected client's baseURL should be preserved, not overridden |
| 101 | + expect(client.client.defaults.baseURL).toBe("https://mock.example.com"); |
| 102 | + }); |
| 103 | + }); |
| 104 | +}); |
0 commit comments