|
1 | 1 | import axios from "axios";
|
2 |
| -import AxiosMockAdapter from "axios-mock-adapter"; |
3 | 2 |
|
4 | 3 | import Testing from "../../../lib/api/Testing";
|
5 |
| -import handleSendingError from "../../../lib/axios-logger"; |
6 |
| - |
7 |
| -import CONFIG from "../../../config"; |
8 |
| -import MailtrapError from "../../../lib/MailtrapError"; |
9 |
| - |
10 |
| -const { CLIENT_SETTINGS } = CONFIG; |
11 |
| -const { TESTING_ENDPOINT } = CLIENT_SETTINGS; |
12 | 4 |
|
13 | 5 | describe("lib/api/Testing: ", () => {
|
14 |
| - let mock: AxiosMockAdapter; |
15 | 6 | const testInboxId = 100;
|
16 | 7 | const testingAPI = new Testing(axios, testInboxId);
|
17 | 8 |
|
18 | 9 | describe("class Testing(): ", () => {
|
19 | 10 | describe("init: ", () => {
|
20 | 11 | it("initalizes with all necessary params.", () => {
|
21 |
| - expect(testingAPI).toHaveProperty("send"); |
22 | 12 | expect(testingAPI).toHaveProperty("projects");
|
23 | 13 | expect(testingAPI).toHaveProperty("inboxes");
|
24 | 14 | expect(testingAPI).toHaveProperty("messages");
|
25 | 15 | expect(testingAPI).toHaveProperty("attachments");
|
26 | 16 | });
|
27 | 17 | });
|
28 |
| - |
29 |
| - beforeAll(() => { |
30 |
| - /** |
31 |
| - * Init Axios interceptors for handling response.data, errors. |
32 |
| - */ |
33 |
| - axios.interceptors.response.use( |
34 |
| - (response) => response.data, |
35 |
| - handleSendingError |
36 |
| - ); |
37 |
| - mock = new AxiosMockAdapter(axios); |
38 |
| - }); |
39 |
| - |
40 |
| - afterEach(() => { |
41 |
| - mock.reset(); |
42 |
| - }); |
43 |
| - |
44 |
| - describe("send(): ", () => { |
45 |
| - it("successfully sends email.", async () => { |
46 |
| - const endpoint = `${TESTING_ENDPOINT}/api/send/${testInboxId}`; |
47 |
| - const expectedResponseData = { |
48 |
| - success: true, |
49 |
| - message_ids: ["0c7fd939-02cf-11ed-88c2-0a58a9feac02"], |
50 |
| - }; |
51 |
| - mock.onPost(endpoint).reply(200, expectedResponseData); |
52 |
| - |
53 |
| - const emailData = { |
54 |
| - from: { |
55 |
| - |
56 |
| - name: "sender", |
57 |
| - }, |
58 |
| - to: [ |
59 |
| - { |
60 |
| - |
61 |
| - name: "recipient", |
62 |
| - }, |
63 |
| - ], |
64 |
| - subject: "mock-subject", |
65 |
| - text: "Mock text", |
66 |
| - html: "<div>Mock text</div>", |
67 |
| - }; |
68 |
| - |
69 |
| - const result = await testingAPI.send(emailData); |
70 |
| - |
71 |
| - expect(mock.history.post[0].url).toEqual(endpoint); |
72 |
| - expect(mock.history.post[0].data).toEqual(JSON.stringify(emailData)); |
73 |
| - expect(result).toEqual(expectedResponseData); |
74 |
| - }); |
75 |
| - |
76 |
| - it("handles an API error.", async () => { |
77 |
| - const responseData = { |
78 |
| - success: false, |
79 |
| - errors: ["mock-error-1", "mock-error-2"], |
80 |
| - }; |
81 |
| - |
82 |
| - const endpoint = `${TESTING_ENDPOINT}/api/send/${testInboxId}`; |
83 |
| - |
84 |
| - mock.onPost(endpoint).reply(400, responseData); |
85 |
| - |
86 |
| - const emailData = { |
87 |
| - from: { |
88 |
| - |
89 |
| - name: "sender", |
90 |
| - }, |
91 |
| - to: [ |
92 |
| - { |
93 |
| - |
94 |
| - name: "recipient", |
95 |
| - }, |
96 |
| - ], |
97 |
| - subject: "mock-subject", |
98 |
| - text: "Mock text", |
99 |
| - html: "<div>Mock text</div>", |
100 |
| - }; |
101 |
| - |
102 |
| - const expectedErrorMessage = responseData.errors.join(","); |
103 |
| - |
104 |
| - expect.assertions(3); |
105 |
| - |
106 |
| - try { |
107 |
| - await testingAPI.send(emailData); |
108 |
| - } catch (error) { |
109 |
| - expect(mock.history.post[0].url).toEqual(endpoint); |
110 |
| - expect(mock.history.post[0].data).toEqual(JSON.stringify(emailData)); |
111 |
| - |
112 |
| - if (error instanceof Error) { |
113 |
| - expect(error.message).toEqual(expectedErrorMessage); |
114 |
| - } |
115 |
| - } |
116 |
| - }); |
117 |
| - |
118 |
| - it("handles an HTTP transport error.", async () => { |
119 |
| - const emailData = { |
120 |
| - from: { |
121 |
| - |
122 |
| - name: "sender", |
123 |
| - }, |
124 |
| - to: [ |
125 |
| - { |
126 |
| - |
127 |
| - name: "recipient", |
128 |
| - }, |
129 |
| - ], |
130 |
| - subject: "mock-subject", |
131 |
| - text: "Mock text", |
132 |
| - html: "<div>Mock text</div>", |
133 |
| - }; |
134 |
| - |
135 |
| - const expectedErrorMessage = "Request failed with status code 404"; |
136 |
| - |
137 |
| - expect.assertions(2); |
138 |
| - |
139 |
| - try { |
140 |
| - await testingAPI.send(emailData); |
141 |
| - } catch (error) { |
142 |
| - expect(error).toBeInstanceOf(MailtrapError); |
143 |
| - |
144 |
| - if (error instanceof MailtrapError) { |
145 |
| - expect(error.message).toEqual(expectedErrorMessage); |
146 |
| - } |
147 |
| - } |
148 |
| - }); |
149 |
| - }); |
150 | 18 | });
|
151 | 19 | });
|
0 commit comments