1
+ /* eslint-disable @typescript-eslint/no-unused-expressions */
1
2
/* eslint-disable no-console */
2
3
import axios , { AxiosError } from "axios" ;
3
4
import AxiosMockAdapter from "axios-mock-adapter" ;
@@ -6,9 +7,13 @@ import { Mail, MailtrapClient } from "../..";
6
7
import MailtrapError from "../../lib/MailtrapError" ;
7
8
8
9
import CONFIG from "../../config" ;
10
+ import TestingAPI from "../../lib/api/Testing" ;
11
+ import GeneralAPI from "../../lib/api/General" ;
9
12
10
- const { ERRORS } = CONFIG ;
11
- const { TEST_INBOX_ID_MISSING , ACCOUNT_ID_MISSING } = ERRORS ;
13
+ const { ERRORS , CLIENT_SETTINGS } = CONFIG ;
14
+ const { TESTING_ENDPOINT , BULK_ENDPOINT , SENDING_ENDPOINT } = CLIENT_SETTINGS ;
15
+ const { TEST_INBOX_ID_MISSING , ACCOUNT_ID_MISSING , BULK_SANDBOX_INCOMPATIBLE } =
16
+ ERRORS ;
12
17
13
18
describe ( "lib/mailtrap-client: " , ( ) => {
14
19
let mock : AxiosMockAdapter ;
@@ -27,31 +32,206 @@ describe("lib/mailtrap-client: ", () => {
27
32
text : "My TEXT" ,
28
33
} ;
29
34
30
- beforeAll ( ( ) => {
31
- mock = new AxiosMockAdapter ( axios ) ;
32
- } ) ;
35
+ // beforeAll(() => {
36
+ // mock = new AxiosMockAdapter(axios);
37
+ // });
33
38
34
- afterEach ( ( ) => {
35
- mock . reset ( ) ;
36
- } ) ;
39
+ // afterEach(() => {
40
+ // mock.reset();
41
+ // });
37
42
38
43
describe ( "send():" , ( ) => {
44
+ beforeAll ( ( ) => {
45
+ mock = new AxiosMockAdapter ( axios ) ;
46
+ } ) ;
47
+
48
+ afterEach ( ( ) => {
49
+ mock . reset ( ) ;
50
+ } ) ;
51
+
52
+ it ( "rejects with Mailtrap error, bulk and sanbox modes are incompatible." , async ( ) => {
53
+ const client = new MailtrapClient ( {
54
+ token : "MY_API_TOKEN" ,
55
+ bulk : true ,
56
+ sandbox : true ,
57
+ } ) ;
58
+
59
+ try {
60
+ await client . send ( goodMail ) ;
61
+ } catch ( error ) {
62
+ expect ( error ) . toEqual ( new MailtrapError ( BULK_SANDBOX_INCOMPATIBLE ) ) ;
63
+ }
64
+ } ) ;
65
+ const testInboxId = 100 ;
66
+
67
+ it ( "successfully sends testing email." , async ( ) => {
68
+ const testingClient = new MailtrapClient ( {
69
+ token : "MY_API_TOKEN" ,
70
+ sandbox : true ,
71
+ testInboxId,
72
+ } ) ;
73
+ const endpoint = `${ TESTING_ENDPOINT } /api/send/${ testInboxId } ` ;
74
+ const expectedResponseData = {
75
+ success : true ,
76
+ message_ids : [ "0c7fd939-02cf-11ed-88c2-0a58a9feac02" ] ,
77
+ } ;
78
+ mock . onPost ( endpoint ) . reply ( 200 , expectedResponseData ) ;
79
+
80
+ const emailData = {
81
+ from : {
82
+
83
+ name : "sender" ,
84
+ } ,
85
+ to : [
86
+ {
87
+
88
+ name : "recipient" ,
89
+ } ,
90
+ ] ,
91
+ subject : "mock-subject" ,
92
+ text : "Mock text" ,
93
+ html : "<div>Mock text</div>" ,
94
+ } ;
95
+
96
+ const result = await testingClient . send ( emailData ) ;
97
+
98
+ expect ( mock . history . post [ 0 ] . url ) . toEqual ( endpoint ) ;
99
+ expect ( mock . history . post [ 0 ] . data ) . toEqual ( JSON . stringify ( emailData ) ) ;
100
+ expect ( result ) . toEqual ( expectedResponseData ) ;
101
+ } ) ;
102
+
103
+ it ( "successfully sends bulk email." , async ( ) => {
104
+ const bulkClient = new MailtrapClient ( {
105
+ token : "MY_API_TOKEN" ,
106
+ bulk : true ,
107
+ } ) ;
108
+ const endpoint = `${ BULK_ENDPOINT } /api/send` ;
109
+ const expectedResponseData = {
110
+ success : true ,
111
+ message_ids : [ "0c7fd939-02cf-11ed-88c2-0a58a9feac02" ] ,
112
+ } ;
113
+ mock . onPost ( endpoint ) . reply ( 200 , expectedResponseData ) ;
114
+
115
+ const emailData = {
116
+ from : {
117
+
118
+ name : "sender" ,
119
+ } ,
120
+ to : [
121
+ {
122
+
123
+ name : "recipient" ,
124
+ } ,
125
+ ] ,
126
+ subject : "mock-subject" ,
127
+ text : "Mock text" ,
128
+ html : "<div>Mock text</div>" ,
129
+ } ;
130
+
131
+ const result = await bulkClient . send ( emailData ) ;
132
+
133
+ expect ( mock . history . post [ 0 ] . url ) . toEqual ( endpoint ) ;
134
+ expect ( mock . history . post [ 0 ] . data ) . toEqual ( JSON . stringify ( emailData ) ) ;
135
+ expect ( result ) . toEqual ( expectedResponseData ) ;
136
+ } ) ;
137
+
138
+ it ( "handles an API error." , async ( ) => {
139
+ const testingClient = new MailtrapClient ( {
140
+ token : "MY_API_TOKEN" ,
141
+ sandbox : true ,
142
+ testInboxId,
143
+ } ) ;
144
+ const responseData = {
145
+ success : false ,
146
+ errors : [ "mock-error-1" , "mock-error-2" ] ,
147
+ } ;
148
+
149
+ const endpoint = `${ TESTING_ENDPOINT } /api/send/${ testInboxId } ` ;
150
+
151
+ mock . onPost ( endpoint ) . reply ( 400 , responseData ) ;
152
+
153
+ const emailData = {
154
+ from : {
155
+
156
+ name : "sender" ,
157
+ } ,
158
+ to : [
159
+ {
160
+
161
+ name : "recipient" ,
162
+ } ,
163
+ ] ,
164
+ subject : "mock-subject" ,
165
+ text : "Mock text" ,
166
+ html : "<div>Mock text</div>" ,
167
+ } ;
168
+
169
+ const expectedErrorMessage = responseData . errors . join ( "," ) ;
170
+
171
+ expect . assertions ( 3 ) ;
172
+
173
+ try {
174
+ await testingClient . send ( emailData ) ;
175
+ } catch ( error ) {
176
+ expect ( mock . history . post [ 0 ] . url ) . toEqual ( endpoint ) ;
177
+ expect ( mock . history . post [ 0 ] . data ) . toEqual ( JSON . stringify ( emailData ) ) ;
178
+
179
+ if ( error instanceof Error ) {
180
+ expect ( error . message ) . toEqual ( expectedErrorMessage ) ;
181
+ }
182
+ }
183
+ } ) ;
184
+
185
+ it ( "handles an HTTP transport error." , async ( ) => {
186
+ const testingClient = new MailtrapClient ( {
187
+ token : "MY_API_TOKEN" ,
188
+ sandbox : true ,
189
+ testInboxId,
190
+ } ) ;
191
+ const emailData = {
192
+ from : {
193
+
194
+ name : "sender" ,
195
+ } ,
196
+ to : [
197
+ {
198
+
199
+ name : "recipient" ,
200
+ } ,
201
+ ] ,
202
+ subject : "mock-subject" ,
203
+ text : "Mock text" ,
204
+ html : "<div>Mock text</div>" ,
205
+ } ;
206
+
207
+ const expectedErrorMessage = "Request failed with status code 404" ;
208
+
209
+ expect . assertions ( 2 ) ;
210
+
211
+ try {
212
+ await testingClient . send ( emailData ) ;
213
+ } catch ( error ) {
214
+ expect ( error ) . toBeInstanceOf ( MailtrapError ) ;
215
+
216
+ if ( error instanceof MailtrapError ) {
217
+ expect ( error . message ) . toEqual ( expectedErrorMessage ) ;
218
+ }
219
+ }
220
+ } ) ;
221
+
39
222
it ( "sends request to mailtrap api" , async ( ) => {
40
223
const successData = {
41
224
success : "true" ,
42
225
message_ids : [ "00000000-00000000-00000000-00000001" ] ,
43
226
} ;
227
+ const endpoint = `${ SENDING_ENDPOINT } /api/send` ;
44
228
45
- mock
46
- . onPost ( "https://send.api.mailtrap.io/api/send" )
47
- . reply ( 200 , successData ) ;
229
+ mock . onPost ( endpoint ) . reply ( 200 , successData ) ;
48
230
49
231
const client = new MailtrapClient ( { token : "MY_API_TOKEN" } ) ;
50
232
const result = await client . send ( goodMail ) ;
51
233
52
- expect ( mock . history . post [ 0 ] . url ) . toEqual (
53
- "https://send.api.mailtrap.io/api/send"
54
- ) ;
234
+ expect ( mock . history . post [ 0 ] . url ) . toEqual ( endpoint ) ;
55
235
expect ( mock . history . post [ 0 ] . headers ) . toMatchObject ( {
56
236
Accept : "application/json, text/plain, */*" ,
57
237
"Content-Type" : "application/json" ,
@@ -158,44 +338,58 @@ describe("lib/mailtrap-client: ", () => {
158
338
} ) ;
159
339
160
340
describe ( "get testing(): " , ( ) => {
161
- it ( "returns testing API object, console warn is called twice." , ( ) => {
162
- const originalWarn = console . warn ;
163
- const mockLogger = jest . fn ( ) ;
164
- console . warn = mockLogger ;
165
- const client = new MailtrapClient ( { token : "MY_API_TOKEN" } ) ;
166
-
167
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
168
- const testingAPI = client . testing ;
169
-
170
- expect . assertions ( 3 ) ;
341
+ it ( "rejects with Mailtrap error, when `testInboxId` is missing." , ( ) => {
342
+ const client = new MailtrapClient ( {
343
+ token : "MY_API_TOKEN" ,
344
+ } ) ;
171
345
172
- expect ( mockLogger ) . toBeCalledTimes ( 2 ) ;
173
- expect ( mockLogger ) . toBeCalledWith ( TEST_INBOX_ID_MISSING ) ;
174
- expect ( mockLogger ) . toBeCalledWith ( ACCOUNT_ID_MISSING ) ;
346
+ expect . assertions ( 1 ) ;
175
347
176
- console . warn = originalWarn ;
348
+ try {
349
+ client . testing ;
350
+ } catch ( error ) {
351
+ expect ( error ) . toEqual ( new MailtrapError ( TEST_INBOX_ID_MISSING ) ) ;
352
+ }
177
353
} ) ;
178
354
179
- it ( "return testing API object, without calling console warn." , ( ) => {
180
- const originalWarn = console . warn ;
181
- const mockLogger = jest . fn ( ) ;
182
- console . warn = mockLogger ;
183
- const testInboxId = 1 ;
184
- const accountId = 1 ;
355
+ it ( "rejects with Mailtrap error, when `accountId` is missing." , ( ) => {
185
356
const client = new MailtrapClient ( {
186
357
token : "MY_API_TOKEN" ,
187
- testInboxId,
188
- accountId,
358
+ testInboxId : 5 ,
189
359
} ) ;
190
360
191
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
192
- const testingAPI = client . testing ;
361
+ expect . assertions ( 1 ) ;
362
+
363
+ try {
364
+ client . testing ;
365
+ } catch ( error ) {
366
+ expect ( error ) . toEqual ( new MailtrapError ( ACCOUNT_ID_MISSING ) ) ;
367
+ }
368
+ } ) ;
193
369
370
+ it ( "returns testing API object, console warn is called twice." , ( ) => {
371
+ const client = new MailtrapClient ( {
372
+ token : "MY_API_TOKEN" ,
373
+ sandbox : true ,
374
+ testInboxId : 10 ,
375
+ accountId : 10 ,
376
+ } ) ;
194
377
expect . assertions ( 1 ) ;
195
378
196
- expect ( mockLogger ) . toBeCalledTimes ( 0 ) ;
379
+ const testingClient = client . testing ;
380
+ expect ( testingClient ) . toBeInstanceOf ( TestingAPI ) ;
381
+ } ) ;
382
+
383
+ describe ( "get general(): " , ( ) => {
384
+ it ( "returns testing API object, console warn is called twice." , ( ) => {
385
+ const client = new MailtrapClient ( {
386
+ token : "MY_API_TOKEN" ,
387
+ } ) ;
388
+ expect . assertions ( 1 ) ;
197
389
198
- console . warn = originalWarn ;
390
+ const generalClient = client . general ;
391
+ expect ( generalClient ) . toBeInstanceOf ( GeneralAPI ) ;
392
+ } ) ;
199
393
} ) ;
200
394
} ) ;
201
395
} ) ;
0 commit comments