|
| 1 | +import axios from "axios"; |
| 2 | +import AxiosMockAdapter from "axios-mock-adapter"; |
| 3 | + |
| 4 | +import ContactEventsApi from "../../../../lib/api/resources/ContactEvents"; |
| 5 | +import handleSendingError from "../../../../lib/axios-logger"; |
| 6 | +import MailtrapError from "../../../../lib/MailtrapError"; |
| 7 | + |
| 8 | +import CONFIG from "../../../../config"; |
| 9 | + |
| 10 | +const { CLIENT_SETTINGS } = CONFIG; |
| 11 | +const { GENERAL_ENDPOINT } = CLIENT_SETTINGS; |
| 12 | + |
| 13 | +describe("lib/api/resources/ContactEvents: ", () => { |
| 14 | + let mock: AxiosMockAdapter; |
| 15 | + const accountId = 100; |
| 16 | + const contactEventsAPI = new ContactEventsApi(axios, accountId); |
| 17 | + |
| 18 | + const contactIdentifier = "[email protected]"; |
| 19 | + const payload = { |
| 20 | + name: "purchase_completed", |
| 21 | + params: { |
| 22 | + order_id: 12345, |
| 23 | + amount: 49.99, |
| 24 | + currency: "USD", |
| 25 | + coupon_used: false, |
| 26 | + }, |
| 27 | + }; |
| 28 | + |
| 29 | + const successResponse = { |
| 30 | + contact_id: "019a4a05-5924-7d93-86ce-f79293418083", |
| 31 | + contact_email: contactIdentifier, |
| 32 | + name: payload.name, |
| 33 | + params: payload.params, |
| 34 | + }; |
| 35 | + |
| 36 | + describe("class ContactEventsApi(): ", () => { |
| 37 | + describe("init: ", () => { |
| 38 | + it("initializes with all necessary params.", () => { |
| 39 | + expect(contactEventsAPI).toHaveProperty("create"); |
| 40 | + }); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + beforeAll(() => { |
| 45 | + axios.interceptors.response.use( |
| 46 | + (response) => response.data, |
| 47 | + handleSendingError |
| 48 | + ); |
| 49 | + mock = new AxiosMockAdapter(axios); |
| 50 | + }); |
| 51 | + |
| 52 | + afterEach(() => { |
| 53 | + mock.reset(); |
| 54 | + }); |
| 55 | + |
| 56 | + describe("create(): ", () => { |
| 57 | + it("successfully creates a contact event.", async () => { |
| 58 | + const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/contacts/${contactIdentifier}/events`; |
| 59 | + const expectedResponseData = successResponse; |
| 60 | + |
| 61 | + expect.assertions(2); |
| 62 | + |
| 63 | + mock.onPost(endpoint, payload).reply(200, expectedResponseData); |
| 64 | + const result = await contactEventsAPI.create(contactIdentifier, payload); |
| 65 | + |
| 66 | + expect(mock.history.post[0].url).toEqual(endpoint); |
| 67 | + expect(result).toEqual(expectedResponseData); |
| 68 | + }); |
| 69 | + |
| 70 | + it("fails with error when accountId is invalid.", async () => { |
| 71 | + const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/contacts/${contactIdentifier}/events`; |
| 72 | + const expectedErrorMessage = "Account not found"; |
| 73 | + |
| 74 | + expect.assertions(2); |
| 75 | + |
| 76 | + mock.onPost(endpoint).reply(404, { error: expectedErrorMessage }); |
| 77 | + |
| 78 | + try { |
| 79 | + await contactEventsAPI.create(contactIdentifier, payload); |
| 80 | + } catch (error) { |
| 81 | + expect(error).toBeInstanceOf(MailtrapError); |
| 82 | + if (error instanceof MailtrapError) { |
| 83 | + expect(error.message).toEqual(expectedErrorMessage); |
| 84 | + } |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + it("fails with error when payload is invalid.", async () => { |
| 89 | + const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/contacts/${contactIdentifier}/events`; |
| 90 | + const expectedErrorMessage = "Invalid event name"; |
| 91 | + |
| 92 | + expect.assertions(2); |
| 93 | + |
| 94 | + mock.onPost(endpoint).reply(422, { error: expectedErrorMessage }); |
| 95 | + |
| 96 | + try { |
| 97 | + await contactEventsAPI.create(contactIdentifier, payload); |
| 98 | + } catch (error) { |
| 99 | + expect(error).toBeInstanceOf(MailtrapError); |
| 100 | + if (error instanceof MailtrapError) { |
| 101 | + expect(error.message).toEqual(expectedErrorMessage); |
| 102 | + } |
| 103 | + } |
| 104 | + }); |
| 105 | + }); |
| 106 | +}); |
0 commit comments