Skip to content

Commit c5f1d30

Browse files
Merge pull request #70 from railsware/optional-test-inbox
Make `testInboxID` optional param
2 parents 6d5ba68 + 1e10d77 commit c5f1d30

File tree

2 files changed

+72
-20
lines changed

2 files changed

+72
-20
lines changed

src/__tests__/lib/mailtrap-client.test.ts

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import TemplatesBaseAPI from "../../lib/api/Templates";
1515

1616
const { ERRORS, CLIENT_SETTINGS } = CONFIG;
1717
const { TESTING_ENDPOINT, BULK_ENDPOINT, SENDING_ENDPOINT } = CLIENT_SETTINGS;
18-
const { TEST_INBOX_ID_MISSING, ACCOUNT_ID_MISSING, BULK_SANDBOX_INCOMPATIBLE } =
18+
const { ACCOUNT_ID_MISSING, BULK_SANDBOX_INCOMPATIBLE, TEST_INBOX_ID_MISSING } =
1919
ERRORS;
2020

2121
describe("lib/mailtrap-client: ", () => {
@@ -331,7 +331,49 @@ describe("lib/mailtrap-client: ", () => {
331331
}
332332
});
333333

334+
it("throws MailtrapError(TEST_INBOX_ID_MISSING) when sending in sandbox mode without testInboxId", async () => {
335+
const client = new MailtrapClient({
336+
token: "MY_API_TOKEN",
337+
sandbox: true,
338+
accountId: 123,
339+
});
340+
341+
await expect(
342+
client.send({
343+
from: { email: "a@b.com", name: "Sender" },
344+
to: [{ email: "c@d.com" }],
345+
subject: "Test",
346+
text: "Body",
347+
})
348+
).rejects.toEqual(new MailtrapError(TEST_INBOX_ID_MISSING));
349+
});
350+
334351
describe("batch sending:", () => {
352+
it("throws MailtrapError(TEST_INBOX_ID_MISSING) when batch sending in sandbox mode without testInboxId", async () => {
353+
const client = new MailtrapClient({
354+
token: "MY_API_TOKEN",
355+
sandbox: true,
356+
accountId: 123,
357+
});
358+
359+
const batchData = {
360+
base: {
361+
from: { email: "a@b.com", name: "Sender" },
362+
subject: "Test",
363+
text: "Body",
364+
},
365+
requests: [
366+
{
367+
to: [{ email: "c@d.com" }],
368+
},
369+
],
370+
};
371+
372+
await expect(client.batchSend(batchData)).rejects.toEqual(
373+
new MailtrapError(TEST_INBOX_ID_MISSING)
374+
);
375+
});
376+
335377
it("rejects with Mailtrap error when bulk and sandbox modes are used together", async () => {
336378
const batchClient = new MailtrapClient({
337379
token: "MY_API_TOKEN",
@@ -664,7 +706,7 @@ describe("lib/mailtrap-client: ", () => {
664706
});
665707

666708
describe("get testing(): ", () => {
667-
it("rejects with Mailtrap error, when `testInboxId` is missing.", () => {
709+
it("rejects with Mailtrap error, when `accountId` is missing.", () => {
668710
const client = new MailtrapClient({
669711
token: "MY_API_TOKEN",
670712
});
@@ -674,30 +716,25 @@ describe("lib/mailtrap-client: ", () => {
674716
try {
675717
client.testing;
676718
} catch (error) {
677-
expect(error).toEqual(new MailtrapError(TEST_INBOX_ID_MISSING));
719+
expect(error).toEqual(new MailtrapError(ACCOUNT_ID_MISSING));
678720
}
679721
});
680722

681-
it("rejects with Mailtrap error, when `accountId` is missing.", () => {
723+
it("returns testing API object when accountId is provided, even without testInboxId", () => {
682724
const client = new MailtrapClient({
683725
token: "MY_API_TOKEN",
684-
testInboxId: 5,
726+
accountId: 123,
727+
// testInboxId is intentionally omitted
685728
});
686-
687729
expect.assertions(1);
688730

689-
try {
690-
client.testing;
691-
} catch (error) {
692-
expect(error).toEqual(new MailtrapError(ACCOUNT_ID_MISSING));
693-
}
731+
const testingClient = client.testing;
732+
expect(testingClient).toBeInstanceOf(TestingAPI);
694733
});
695734

696735
it("returns testing API object, console warn is called twice.", () => {
697736
const client = new MailtrapClient({
698737
token: "MY_API_TOKEN",
699-
sandbox: true,
700-
testInboxId: 10,
701738
accountId: 10,
702739
});
703740
expect.assertions(1);

src/lib/MailtrapClient.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const {
3232
TESTING_ENDPOINT,
3333
BULK_ENDPOINT,
3434
} = CLIENT_SETTINGS;
35-
const { TEST_INBOX_ID_MISSING, ACCOUNT_ID_MISSING, BULK_SANDBOX_INCOMPATIBLE } =
35+
const { ACCOUNT_ID_MISSING, BULK_SANDBOX_INCOMPATIBLE, TEST_INBOX_ID_MISSING } =
3636
ERRORS;
3737

3838
/**
@@ -93,13 +93,18 @@ export default class MailtrapClient {
9393
}
9494

9595
/**
96-
* Getter for Testing API. Warns if some of the required keys are missing.
96+
* Validates that test inbox ID is present, throws MailtrapError if missing.
9797
*/
98-
get testing() {
99-
if (!this.testInboxId) {
98+
private validateTestInboxIdPresence(): void {
99+
if (this.sandbox && !this.testInboxId) {
100100
throw new MailtrapError(TEST_INBOX_ID_MISSING);
101101
}
102+
}
102103

104+
/**
105+
* Getter for Testing API. Warns if some of the required keys are missing.
106+
*/
107+
get testing() {
103108
this.validateAccountIdPresence();
104109

105110
return new TestingAPI(this.axios, this.accountId);
@@ -132,6 +137,9 @@ export default class MailtrapClient {
132137
return new ContactListsBaseAPI(this.axios, this.accountId);
133138
}
134139

140+
/**
141+
* Getter for Templates API.
142+
*/
135143
get templates() {
136144
this.validateAccountIdPresence();
137145

@@ -164,8 +172,11 @@ export default class MailtrapClient {
164172
*/
165173
public async send(mail: Mail): Promise<SendResponse> {
166174
const host = this.determineHost();
175+
176+
this.validateTestInboxIdPresence();
177+
167178
const url = `${host}/api/send${
168-
this.testInboxId ? `/${this.testInboxId}` : ""
179+
this.sandbox && this.testInboxId ? `/${this.testInboxId}` : ""
169180
}`;
170181
const preparedMail = encodeMailBuffers(mail);
171182

@@ -181,9 +192,13 @@ export default class MailtrapClient {
181192
): Promise<BatchSendResponse> {
182193
const { requests, base } = request;
183194
const host = this.determineHost();
184-
const ifSandbox =
195+
196+
this.validateTestInboxIdPresence();
197+
198+
const sandbox =
185199
this.sandbox && this.testInboxId ? `/${this.testInboxId}` : "";
186-
const url = `${host}/api/batch${ifSandbox}`;
200+
201+
const url = `${host}/api/batch${sandbox}`;
187202

188203
const preparedBase = base ? encodeMailBuffers(base) : undefined;
189204
const preparedRequests = requests.map((singleRequest) =>

0 commit comments

Comments
 (0)