@zerobounce/zero-bounce-sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,747 @@
1
+ import { ZeroBounceSDK } from "../src/zero-bounce.js";
2
+ import * as utils from "../src/utils.js";
3
+
4
+ describe("ZeroBounceSDK", () => {
5
+ let zeroBounceSDK;
6
+ const initErrorMessage = "ZeroBounce: Call init function first with a valid api key.";
7
+ const missingParamMessage = "parameter is missing";
8
+
9
+ beforeAll(() => {
10
+ jest.spyOn(console, "error").mockImplementation(() => {});
11
+ });
12
+
13
+ beforeEach(() => {
14
+ zeroBounceSDK = new ZeroBounceSDK();
15
+ });
16
+
17
+ afterEach(() => {
18
+ jest.clearAllMocks();
19
+ });
20
+
21
+ afterAll(() => {
22
+ jest.restoreAllMocks();
23
+ });
24
+
25
+
26
+ describe("getCredits", () => {
27
+ it("should throw an error if not initialized", async () => {
28
+ await zeroBounceSDK.getCredits();
29
+ expect(console.error).toHaveBeenCalledWith(initErrorMessage);
30
+ });
31
+
32
+ it("should return error response with invalid API key", async () => {
33
+ zeroBounceSDK.init("invalid-api-key");
34
+ const response = await zeroBounceSDK.getCredits();
35
+ expect(response).toEqual({ "Credits": "-1" });
36
+ });
37
+
38
+ it("should return the correct number of credits", async () => {
39
+ const expectedResponse = {
40
+ "Credits": "100"
41
+ }
42
+
43
+ jest.spyOn(global, "fetch").mockImplementationOnce(() => Promise.resolve({
44
+ json: () => Promise.resolve(expectedResponse),
45
+ text: () => Promise.resolve(JSON.stringify(expectedResponse)),
46
+ }));
47
+
48
+ zeroBounceSDK.init("valid-api-key");
49
+ const response = await zeroBounceSDK.getCredits();
50
+ expect(response).toEqual(expectedResponse);
51
+ });
52
+ });
53
+
54
+
55
+ describe("getApiUsage", () => {
56
+ const startDate = "2018-01-01";
57
+ const endDate = "2023-12-12";
58
+
59
+ it("should throw an error if not initialized", async () => {
60
+ await zeroBounceSDK.getApiUsage(startDate, endDate);
61
+ expect(console.error).toHaveBeenCalledWith(initErrorMessage);
62
+ });
63
+
64
+ it("should throw an error if stardDate is missing", async () => {
65
+ zeroBounceSDK.init("invalid-api-key");
66
+ await zeroBounceSDK.getApiUsage(null, endDate);
67
+ expect(console.error).toHaveBeenCalledWith(
68
+ expect.stringContaining(missingParamMessage)
69
+ );
70
+ });
71
+
72
+ it("should throw an error if endDate is missing", async () => {
73
+ zeroBounceSDK.init("invalid-api-key");
74
+ await zeroBounceSDK.getApiUsage(startDate, null);
75
+ expect(console.error).toHaveBeenCalledWith(
76
+ expect.stringContaining(missingParamMessage)
77
+ );
78
+ });
79
+
80
+ it("should return error response with invalid API key", async () => {
81
+ zeroBounceSDK.init("invalid-api-key");
82
+ const response = await zeroBounceSDK.getApiUsage(startDate, endDate);
83
+ expect(response).toEqual({ "error": "Invalid API key" });
84
+ });
85
+
86
+ it("should return API usage data", async () => {
87
+ const expectedResponse = {
88
+ "total": 5,
89
+ "status_valid": 4,
90
+ "status_invalid": 1,
91
+ "status_catch_all": 0,
92
+ "status_do_not_mail": 0,
93
+ "status_spamtrap": 0,
94
+ "status_unknown": 0,
95
+ "sub_status_toxic": 0,
96
+ "sub_status_disposable": 0,
97
+ "sub_status_role_based": 0,
98
+ "sub_status_possible_trap": 0,
99
+ "sub_status_global_suppression": 0,
100
+ "sub_status_timeout_exceeded": 0,
101
+ "sub_status_mail_server_temporary_error": 0,
102
+ "sub_status_mail_server_did_not_respond": 0,
103
+ "sub_status_greylisted": 0,
104
+ "sub_status_antispam_system": 0,
105
+ "sub_status_does_not_accept_mail": 0,
106
+ "sub_status_exception_occurred": 0,
107
+ "sub_status_failed_syntax_check": 1,
108
+ "sub_status_mailbox_not_found": 0,
109
+ "sub_status_unroutable_ip_address": 0,
110
+ "sub_status_possible_typo": 0,
111
+ "sub_status_no_dns_entries": 0,
112
+ "sub_status_role_based_catch_all": 0,
113
+ "sub_status_mailbox_quota_exceeded": 0,
114
+ "sub_status_forcible_disconnect": 0,
115
+ "sub_status_failed_smtp_connection": 0,
116
+ "sub_status_mx_forward": 0,
117
+ "sub_status_alternate": 0,
118
+ "sub_status_blocked": 0,
119
+ "sub_status_allowed": 0,
120
+ "start_date": "1/1/2018",
121
+ "end_date": "12/12/2023"
122
+ }
123
+
124
+ jest.spyOn(global, "fetch").mockImplementationOnce(() => Promise.resolve({
125
+ json: () => Promise.resolve(expectedResponse),
126
+ text: () => Promise.resolve(JSON.stringify(expectedResponse)),
127
+ }));
128
+
129
+ zeroBounceSDK.init("valid-api-key");
130
+ const response = await zeroBounceSDK.getApiUsage(startDate, endDate);
131
+ expect(response).toEqual(expectedResponse);
132
+ });
133
+ });
134
+
135
+
136
+ describe("validateEmail", () => {
137
+ const email = "valid@example.com";
138
+ const ip_address = "127.0.0.1";
139
+
140
+ it("should throw an error if not initialized", async () => {
141
+ await zeroBounceSDK.validateEmail(email, ip_address);
142
+ expect(console.error).toHaveBeenCalledWith(initErrorMessage);
143
+ });
144
+
145
+ it("should throw an error if email is missing", async () => {
146
+ zeroBounceSDK.init("invalid-api-key");
147
+ await zeroBounceSDK.validateEmail(null);
148
+ expect(console.error).toHaveBeenCalledWith(
149
+ expect.stringContaining(missingParamMessage)
150
+ );
151
+ });
152
+
153
+ it("should return error response with invalid API key", async () => {
154
+ zeroBounceSDK.init("invalid-api-key");
155
+ const response = await zeroBounceSDK.validateEmail(email, ip_address);
156
+ expect(response).toEqual({
157
+ "error": "Invalid API key or your account ran out of credits"
158
+ });
159
+ });
160
+
161
+ it("should return the validated email data", async () => {
162
+ const expectedResponse = {
163
+ "address": "valid@example.com",
164
+ "status": "valid",
165
+ "sub_status": "",
166
+ "free_email": false,
167
+ "did_you_mean": null,
168
+ "account": null,
169
+ "domain": null,
170
+ "domain_age_days": "9692",
171
+ "smtp_provider": "example",
172
+ "mx_found": "true",
173
+ "mx_record": "mx.example.com",
174
+ "firstname": "zero",
175
+ "lastname": "bounce",
176
+ "gender": "male",
177
+ "country": null,
178
+ "region": null,
179
+ "city": null,
180
+ "zipcode": null,
181
+ "processed_at": "2023-04-27 13:47:23.980"
182
+ }
183
+
184
+ jest.spyOn(global, "fetch").mockImplementationOnce(() => Promise.resolve({
185
+ json: () => Promise.resolve(expectedResponse),
186
+ text: () => Promise.resolve(JSON.stringify(expectedResponse)),
187
+ }));
188
+
189
+ zeroBounceSDK.init("valid-api-key");
190
+ const response = await zeroBounceSDK.validateEmail(email, ip_address);
191
+ expect(response).toEqual(expectedResponse);
192
+ });
193
+ });
194
+
195
+
196
+ describe("validateBatch", () => {
197
+ const emailBatch = [
198
+ { email_address: "valid@example.com" },
199
+ { email_address: "invalid@example.com" },
200
+ ];
201
+
202
+ it("should throw an error if not initialized", async () => {
203
+ await zeroBounceSDK.validateBatch(emailBatch);
204
+ expect(console.error).toHaveBeenCalledWith(initErrorMessage);
205
+ });
206
+
207
+ it("should throw an error if email list is missing", async () => {
208
+ zeroBounceSDK.init("invalid-api-key");
209
+ await zeroBounceSDK.validateBatch(null);
210
+ expect(console.error).toHaveBeenCalledWith(
211
+ expect.stringContaining(missingParamMessage)
212
+ );
213
+ });
214
+
215
+ it("should return error response with invalid API key", async () => {
216
+ zeroBounceSDK.init("invalid-api-key");
217
+ const response = await zeroBounceSDK.validateBatch(emailBatch);
218
+ expect(response).toEqual({
219
+ "email_batch": [],
220
+ "errors": [
221
+ {
222
+ "email_address": "all",
223
+ "error": "Invalid API Key or your account ran out of credits"
224
+ }
225
+ ]
226
+ });
227
+ });
228
+
229
+ it("should return the validated emails data", async () => {
230
+ const expectedResponse = {
231
+ "email_batch": [
232
+ {
233
+ "address": "valid@example.com",
234
+ "status": "valid",
235
+ "sub_status": "",
236
+ "free_email": false,
237
+ "did_you_mean": null,
238
+ "account": null,
239
+ "domain": null,
240
+ "domain_age_days": "9692",
241
+ "smtp_provider": "example",
242
+ "mx_found": "true",
243
+ "mx_record": "mx.example.com",
244
+ "firstname": "zero",
245
+ "lastname": "bounce",
246
+ "gender": "male",
247
+ "country": null,
248
+ "region": null,
249
+ "city": null,
250
+ "zipcode": null,
251
+ "processed_at": "2023-04-27 14:15:15.450"
252
+ },
253
+ {
254
+ "address": "invalid@example.com",
255
+ "status": "invalid",
256
+ "sub_status": "mailbox_not_found",
257
+ "free_email": false,
258
+ "did_you_mean": null,
259
+ "account": null,
260
+ "domain": null,
261
+ "domain_age_days": "9692",
262
+ "smtp_provider": "example",
263
+ "mx_found": "true",
264
+ "mx_record": "mx.example.com",
265
+ "firstname": "zero",
266
+ "lastname": "bounce",
267
+ "gender": "male",
268
+ "country": null,
269
+ "region": null,
270
+ "city": null,
271
+ "zipcode": null,
272
+ "processed_at": "2023-04-27 14:15:15.450"
273
+ }
274
+ ],
275
+ "errors": []
276
+ }
277
+
278
+ jest.spyOn(global, "fetch").mockImplementationOnce(() => Promise.resolve({
279
+ json: () => Promise.resolve(expectedResponse),
280
+ text: () => Promise.resolve(JSON.stringify(expectedResponse)),
281
+ }));
282
+
283
+ zeroBounceSDK.init("valid-api-key");
284
+ const response = await zeroBounceSDK.validateBatch(emailBatch);
285
+ expect(response).toEqual(expectedResponse);
286
+ });
287
+ });
288
+
289
+
290
+ describe("getEmailActivity", () => {
291
+ const email = "valid@example.com";
292
+
293
+ it("should throw an error if not initialized", async () => {
294
+ await zeroBounceSDK.getEmailActivity(email);
295
+ expect(console.error).toHaveBeenCalledWith(initErrorMessage);
296
+ });
297
+
298
+ it("should throw an error if email is missing", async () => {
299
+ zeroBounceSDK.init("invalid-api-key");
300
+ await zeroBounceSDK.getEmailActivity(null);
301
+ expect(console.error).toHaveBeenCalledWith(
302
+ expect.stringContaining(missingParamMessage)
303
+ );
304
+ });
305
+
306
+ it("should return error response with invalid API key", async () => {
307
+ zeroBounceSDK.init("invalid-api-key");
308
+ const response = await zeroBounceSDK.getEmailActivity(email);
309
+ expect(response).toEqual({
310
+ "error": "Invalid API key or your account ran out of credits"
311
+ });
312
+ });
313
+
314
+ it("should return the email activity data", async () => {
315
+ const expectedResponse = {
316
+ "found": true,
317
+ "active_in_days": "180"
318
+ }
319
+
320
+ jest.spyOn(global, "fetch").mockImplementationOnce(() => Promise.resolve({
321
+ json: () => Promise.resolve(expectedResponse),
322
+ text: () => Promise.resolve(JSON.stringify(expectedResponse)),
323
+ }));
324
+
325
+ zeroBounceSDK.init("valid-api-key");
326
+ const response = await zeroBounceSDK.getEmailActivity(email);
327
+ expect(response).toEqual(expectedResponse);
328
+ });
329
+ });
330
+
331
+
332
+ describe("sendFile", () => {
333
+ class File {}
334
+ var file = new File();
335
+
336
+ const payload = {
337
+ file: file,
338
+ email_address_column: 1,
339
+ };
340
+
341
+ it("should throw an error if not initialized", async () => {
342
+ await zeroBounceSDK.sendFile(payload);
343
+ expect(console.error).toHaveBeenCalledWith(initErrorMessage);
344
+ });
345
+
346
+ it("should throw an error if file is missing", async () => {
347
+ const payload = {
348
+ email_address_column: 1,
349
+ };
350
+
351
+ zeroBounceSDK.init("invalid-api-key");
352
+ await zeroBounceSDK.sendFile(payload);
353
+ expect(console.error).toHaveBeenCalledWith(
354
+ expect.stringContaining(missingParamMessage)
355
+ );
356
+ });
357
+
358
+ it("should throw an error if email address column is missing", async () => {
359
+ const payload = {
360
+ file: file,
361
+ };
362
+
363
+ zeroBounceSDK.init("invalid-api-key");
364
+ await zeroBounceSDK.sendFile(payload);
365
+ expect(console.error).toHaveBeenCalledWith(
366
+ expect.stringContaining(missingParamMessage)
367
+ );
368
+ });
369
+
370
+ it("should return error response with invalid API key", async () => {
371
+ zeroBounceSDK.init("invalid-api-key");
372
+ const response = await zeroBounceSDK.sendFile(payload);
373
+ expect(response).toEqual({
374
+ "success": "False",
375
+ "message": [
376
+ "api_key is invalid"
377
+ ]
378
+ });
379
+ });
380
+
381
+ it("should return the data regarding sent file", async () => {
382
+ const expectedResponse = {
383
+ "success": true,
384
+ "message": "File Accepted",
385
+ "file_name": "Your file name.csv",
386
+ "file_id": "aaaaaaaa-zzzz-xxxx-yyyy-5003727fffff"
387
+ }
388
+
389
+ jest.spyOn(global, "fetch").mockImplementationOnce(() => Promise.resolve({
390
+ json: () => Promise.resolve(expectedResponse),
391
+ text: () => Promise.resolve(JSON.stringify(expectedResponse)),
392
+ }));
393
+
394
+ zeroBounceSDK.init("valid-api-key");
395
+ const response = await zeroBounceSDK.sendFile(payload);
396
+ expect(response).toEqual(expectedResponse);
397
+ });
398
+ });
399
+
400
+
401
+ describe("sendScoringFile", () => {
402
+ class File {}
403
+ var file = new File();
404
+
405
+ const payload = {
406
+ file: file,
407
+ email_address_column: 1,
408
+ };
409
+
410
+ it("should throw an error if not initialized", async () => {
411
+ await zeroBounceSDK.sendScoringFile(payload);
412
+ expect(console.error).toHaveBeenCalledWith(initErrorMessage);
413
+ });
414
+
415
+ it("should throw an error if file is missing", async () => {
416
+ const payload = {
417
+ email_address_column: 1,
418
+ };
419
+
420
+ zeroBounceSDK.init("invalid-api-key");
421
+ await zeroBounceSDK.sendScoringFile(payload);
422
+ expect(console.error).toHaveBeenCalledWith(
423
+ expect.stringContaining(missingParamMessage)
424
+ );
425
+ });
426
+
427
+ it("should throw an error if email address column is missing", async () => {
428
+ const payload = {
429
+ file: file,
430
+ };
431
+
432
+ zeroBounceSDK.init("invalid-api-key");
433
+ await zeroBounceSDK.sendScoringFile(payload);
434
+ expect(console.error).toHaveBeenCalledWith(
435
+ expect.stringContaining(missingParamMessage)
436
+ );
437
+ });
438
+
439
+ it("should return error response with invalid API key", async () => {
440
+ zeroBounceSDK.init("invalid-api-key");
441
+ const response = await zeroBounceSDK.sendScoringFile(payload);
442
+ expect(response).toEqual({
443
+ "success": "False",
444
+ "message": [
445
+ "api_key is invalid"
446
+ ]
447
+ });
448
+ });
449
+
450
+ it("should return the data regarding sent scoring file", async () => {
451
+ const expectedResponse = {
452
+ "success": true,
453
+ "message": "File Accepted",
454
+ "file_name": "Your file name.csv",
455
+ "file_id": "aaaaaaaa-zzzz-xxxx-yyyy-5003727fffff"
456
+ }
457
+
458
+ jest.spyOn(global, "fetch").mockImplementationOnce(() => Promise.resolve({
459
+ json: () => Promise.resolve(expectedResponse),
460
+ text: () => Promise.resolve(JSON.stringify(expectedResponse)),
461
+ }));
462
+
463
+ zeroBounceSDK.init("valid-api-key");
464
+ const response = await zeroBounceSDK.sendScoringFile(payload);
465
+ expect(response).toEqual(expectedResponse);
466
+ });
467
+ });
468
+
469
+
470
+ describe("getFileStatus", () => {
471
+ const fileId = "aaaaaaaa-zzzz-xxxx-yyyy-5003727fffff";
472
+
473
+ it("should throw an error if not initialized", async () => {
474
+ await zeroBounceSDK.getFileStatus(fileId);
475
+ expect(console.error).toHaveBeenCalledWith(initErrorMessage);
476
+ });
477
+
478
+ it("should throw an error if file id is missing", async () => {
479
+ zeroBounceSDK.init("invalid-api-key");
480
+ await zeroBounceSDK.getFileStatus(null);
481
+ expect(console.error).toHaveBeenCalledWith(
482
+ expect.stringContaining(missingParamMessage)
483
+ );
484
+ });
485
+
486
+ it("should return error response with invalid API key", async () => {
487
+ zeroBounceSDK.init("invalid-api-key");
488
+ const response = await zeroBounceSDK.getFileStatus(fileId);
489
+ expect(response).toEqual({
490
+ "success": "False",
491
+ "message": [
492
+ "api_key is invalid"
493
+ ]
494
+ });
495
+ });
496
+
497
+ it("should return the completion status of a previously sent file", async () => {
498
+ const expectedResponse = {
499
+ "success": true,
500
+ "file_id": "aaaaaaaa-zzzz-xxxx-yyyy-5003727fffff",
501
+ "file_name": "email_file.csv",
502
+ "upload_date": "2023-04-21T06:50:18Z",
503
+ "file_status": "Complete",
504
+ "complete_percentage": "100%",
505
+ "error_reason": null,
506
+ "return_url": "Your return URL if provided when calling sendfile API"
507
+ }
508
+
509
+ jest.spyOn(global, "fetch").mockImplementationOnce(() => Promise.resolve({
510
+ json: () => Promise.resolve(expectedResponse),
511
+ text: () => Promise.resolve(JSON.stringify(expectedResponse)),
512
+ }));
513
+
514
+ zeroBounceSDK.init("valid-api-key");
515
+ const response = await zeroBounceSDK.getFileStatus(fileId);
516
+ expect(response).toEqual(expectedResponse);
517
+ });
518
+ });
519
+
520
+
521
+ describe("getScoringFileStatus", () => {
522
+ const fileId = "aaaaaaaa-zzzz-xxxx-yyyy-5003727fffff";
523
+
524
+ it("should throw an error if not initialized", async () => {
525
+ await zeroBounceSDK.getScoringFileStatus(fileId);
526
+ expect(console.error).toHaveBeenCalledWith(initErrorMessage);
527
+ });
528
+
529
+ it("should throw an error if file id is missing", async () => {
530
+ zeroBounceSDK.init("invalid-api-key");
531
+ await zeroBounceSDK.getScoringFileStatus(null);
532
+ expect(console.error).toHaveBeenCalledWith(
533
+ expect.stringContaining(missingParamMessage)
534
+ );
535
+ });
536
+
537
+ it("should return error response with invalid API key", async () => {
538
+ zeroBounceSDK.init("invalid-api-key");
539
+ const response = await zeroBounceSDK.getScoringFileStatus(fileId);
540
+ expect(response).toEqual({
541
+ "success": "False",
542
+ "message": [
543
+ "api_key is invalid"
544
+ ]
545
+ });
546
+ });
547
+
548
+ it("should return the completion status of a previously sent scoring file", async () => {
549
+ const expectedResponse = {
550
+ "success": true,
551
+ "file_id": "aaaaaaaa-zzzz-xxxx-yyyy-5003727fffff",
552
+ "file_name": "email_file.csv",
553
+ "upload_date": "2023-04-21T06:50:18Z",
554
+ "file_status": "Complete",
555
+ "complete_percentage": "100%",
556
+ "return_url": "Your return URL if provided when calling sendfile API"
557
+ }
558
+
559
+ jest.spyOn(global, "fetch").mockImplementationOnce(() => Promise.resolve({
560
+ json: () => Promise.resolve(expectedResponse),
561
+ text: () => Promise.resolve(JSON.stringify(expectedResponse)),
562
+ }));
563
+
564
+ zeroBounceSDK.init("valid-api-key");
565
+ const response = await zeroBounceSDK.getScoringFileStatus(fileId);
566
+ expect(response).toEqual(expectedResponse);
567
+ });
568
+ });
569
+
570
+
571
+ describe("getFile", () => {
572
+ const fileId = "aaaaaaaa-zzzz-xxxx-yyyy-5003727fffff";
573
+
574
+ it("should throw an error if not initialized", async () => {
575
+ await zeroBounceSDK.getFile(fileId);
576
+ expect(console.error).toHaveBeenCalledWith(initErrorMessage);
577
+ });
578
+
579
+ it("should throw an error if file id is missing", async () => {
580
+ zeroBounceSDK.init("invalid-api-key");
581
+ await zeroBounceSDK.getFile(null);
582
+ expect(console.error).toHaveBeenCalledWith(
583
+ expect.stringContaining(missingParamMessage)
584
+ );
585
+ });
586
+
587
+ it("should return error response with invalid API key", async () => {
588
+ zeroBounceSDK.init("invalid-api-key");
589
+ const response = await zeroBounceSDK.getFile(fileId);
590
+ expect(response).toEqual({
591
+ "success": "False",
592
+ "message": [
593
+ "api_key is invalid"
594
+ ]
595
+ });
596
+ });
597
+
598
+ it("should return the previously sent file", async () => {
599
+ const expectedResponse = "\"Email Address\"\n\"valid@example.com\"\n";
600
+
601
+ const createRequestSpy = jest.spyOn(utils, "createRequest").mockImplementationOnce(() => {
602
+ return Promise.resolve(expectedResponse);
603
+ });
604
+
605
+ zeroBounceSDK.init("valid-api-key");
606
+ const response = await zeroBounceSDK.getFile(fileId);
607
+ expect(createRequestSpy.mock.calls[0][0]["returnText"]).toEqual(true);
608
+ expect(response).toEqual(expectedResponse);
609
+ });
610
+ });
611
+
612
+
613
+ describe("getScoringFile", () => {
614
+ const fileId = "aaaaaaaa-zzzz-xxxx-yyyy-5003727fffff";
615
+
616
+ it("should throw an error if not initialized", async () => {
617
+ await zeroBounceSDK.getScoringFile(fileId);
618
+ expect(console.error).toHaveBeenCalledWith(initErrorMessage);
619
+ });
620
+
621
+ it("should throw an error if file id is missing", async () => {
622
+ zeroBounceSDK.init("invalid-api-key");
623
+ await zeroBounceSDK.getScoringFile(null);
624
+ expect(console.error).toHaveBeenCalledWith(
625
+ expect.stringContaining(missingParamMessage)
626
+ );
627
+ });
628
+
629
+ it("should return error response with invalid API key", async () => {
630
+ zeroBounceSDK.init("invalid-api-key");
631
+ const response = await zeroBounceSDK.getScoringFile(fileId);
632
+ expect(response).toEqual({
633
+ "success": "False",
634
+ "message": [
635
+ "api_key is invalid"
636
+ ]
637
+ });
638
+ });
639
+
640
+ it("should return the previously sent scoring file", async () => {
641
+ const expectedResponse = "\"Score\"\n\"100\"\n";
642
+
643
+ const createRequestSpy = jest.spyOn(utils, "createRequest").mockImplementationOnce(() => {
644
+ return Promise.resolve(expectedResponse);
645
+ });
646
+
647
+ zeroBounceSDK.init("valid-api-key");
648
+ const response = await zeroBounceSDK.getScoringFile(fileId);
649
+ expect(createRequestSpy.mock.calls[0][0]["returnText"]).toEqual(true);
650
+ expect(response).toEqual(expectedResponse);
651
+ });
652
+ });
653
+
654
+
655
+ describe("deleteFile", () => {
656
+ const fileId = "aaaaaaaa-zzzz-xxxx-yyyy-5003727fffff";
657
+
658
+ it("should throw an error if not initialized", async () => {
659
+ await zeroBounceSDK.deleteFile(fileId);
660
+ expect(console.error).toHaveBeenCalledWith(initErrorMessage);
661
+ });
662
+
663
+ it("should throw an error if file id is missing", async () => {
664
+ zeroBounceSDK.init("invalid-api-key");
665
+ await zeroBounceSDK.deleteFile(null);
666
+ expect(console.error).toHaveBeenCalledWith(
667
+ expect.stringContaining(missingParamMessage)
668
+ );
669
+ });
670
+
671
+ it("should return error response with invalid API key", async () => {
672
+ zeroBounceSDK.init("invalid-api-key");
673
+ const response = await zeroBounceSDK.deleteFile(fileId);
674
+ expect(response).toEqual({
675
+ "success": "False",
676
+ "message": [
677
+ "api_key is invalid"
678
+ ]
679
+ });
680
+ });
681
+
682
+ it("should delete previously sent file", async () => {
683
+ const expectedResponse = {
684
+ "success":true,
685
+ "message":"File Deleted",
686
+ "file_name":"email_file.csv",
687
+ "file_id":"aaaaaaaa-zzzz-xxxx-yyyy-5003727fffff"
688
+ }
689
+
690
+ jest.spyOn(global, "fetch").mockImplementationOnce(() => Promise.resolve({
691
+ json: () => Promise.resolve(expectedResponse),
692
+ text: () => Promise.resolve(JSON.stringify(expectedResponse)),
693
+ }));
694
+
695
+ zeroBounceSDK.init("valid-api-key");
696
+ const response = await zeroBounceSDK.deleteFile(fileId);
697
+ expect(response).toEqual(expectedResponse);
698
+ });
699
+ });
700
+
701
+
702
+ describe("deleteScoringFile", () => {
703
+ const fileId = "aaaaaaaa-zzzz-xxxx-yyyy-5003727fffff";
704
+
705
+ it("should throw an error if not initialized", async () => {
706
+ await zeroBounceSDK.deleteScoringFile(fileId);
707
+ expect(console.error).toHaveBeenCalledWith(initErrorMessage);
708
+ });
709
+
710
+ it("should throw an error if file id is missing", async () => {
711
+ zeroBounceSDK.init("invalid-api-key");
712
+ await zeroBounceSDK.deleteScoringFile(null);
713
+ expect(console.error).toHaveBeenCalledWith(
714
+ expect.stringContaining(missingParamMessage)
715
+ );
716
+ });
717
+
718
+ it("should return error response with invalid API key", async () => {
719
+ zeroBounceSDK.init("invalid-api-key");
720
+ const response = await zeroBounceSDK.deleteScoringFile(fileId);
721
+ expect(response).toEqual({
722
+ "success": "False",
723
+ "message": [
724
+ "api_key is invalid"
725
+ ]
726
+ });
727
+ });
728
+
729
+ it("should delete previously sent scoring file", async () => {
730
+ const expectedResponse = {
731
+ "success":true,
732
+ "message":"File Deleted",
733
+ "file_name":"email_file.csv",
734
+ "file_id":"aaaaaaaa-zzzz-xxxx-yyyy-5003727fffff"
735
+ }
736
+
737
+ jest.spyOn(global, "fetch").mockImplementationOnce(() => Promise.resolve({
738
+ json: () => Promise.resolve(expectedResponse),
739
+ text: () => Promise.resolve(JSON.stringify(expectedResponse)),
740
+ }));
741
+
742
+ zeroBounceSDK.init("valid-api-key");
743
+ const response = await zeroBounceSDK.deleteScoringFile(fileId);
744
+ expect(response).toEqual(expectedResponse);
745
+ });
746
+ });
747
+ });