Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodemailer testing #28

Merged
merged 6 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const transport = Nodemailer.createTransport(MailtrapTransport({
token: TOKEN
}))

// Note: 'sandbox: true' can be passed for making requests to testing API
narekhovhannisyan marked this conversation as resolved.
Show resolved Hide resolved
transport.sendMail({
text: "Welcome to Mailtrap Sending!",
to: {
Expand Down
38 changes: 38 additions & 0 deletions src/__tests__/lib/normalizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,43 @@ describe("lib/normalizer: ", () => {
// @ts-ignore
cb(null, mailData);
});

it("checks if testing client is switched.", () => {
expect.assertions(3);

const mockResponse = {
statusCode: 200,
};
const mockClient = {
testing: {
send: jest.fn(() => Promise.resolve(mockResponse)),
},
};

const callback = (error: Error, data: SendError) => {
expect(error).toBeNull();
expect(data).toEqual(mockResponse);
};
const mailData = {
sandbox: true,
text: "mock-text",
to: {
address: "mock@mail.com",
name: "mock-name",
},
from: {
address: "mock@mail.com",
name: "mock-name",
},
subject: "mock-subject",
};

// @ts-ignore
const cb = normalizeCallback(mockClient, callback);

// @ts-ignore
cb(null, mailData);
expect(mockClient.testing.send).toBeCalledTimes(1);
});
});
});
2 changes: 1 addition & 1 deletion src/adapters/mail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default function adaptMail(data: MailtrapMailOptions): Mail | SendError {
mail.custom_variables = data.customVariables;
}

if (data.templateUuid) {
if (!data.sandbox && data.templateUuid) {
return {
...mail,
template_uuid: data.templateUuid,
Expand Down
4 changes: 3 additions & 1 deletion src/lib/normalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export default function normalizeCallback(
});
}

return client
const mailtrapClient = data.sandbox ? client.testing : client;

return mailtrapClient
.send(mail as MailtrapMail)
.then((sendResponse) => callback(null, sendResponse))
.catch((error) => {
Expand Down
14 changes: 12 additions & 2 deletions src/types/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type AdditionalFields = {
custom_variables?: CustomVariables;
template_uuid?: string;
template_variables?: TemplateVariables;
sandbox?: boolean | undefined;
};

export type NormalizeCallbackData =
Expand All @@ -26,19 +27,28 @@ export type NormalizeCallback = (
info: SendResponse | SendError
) => void;

interface MailtrapMailOptionsSandbox extends NodemailerMail.Options {
customVariables?: CustomVariables;
category?: string;
sandbox: boolean;
}

export interface MailtrapMailOptions extends NodemailerMail.Options {
customVariables?: CustomVariables;
category?: string;
templateUuid?: string;
templateVariables?: Record<string, string | number | boolean>;
sandbox?: boolean | undefined;
}

export type MailtrapResponse = SendResponse | SendError;

export interface MailtrapTransporter extends Transporter<MailtrapResponse> {
sendMail(
mailOptions: MailtrapMailOptions,
mailOptions: MailtrapMailOptions | MailtrapMailOptionsSandbox,
callback: (err: Error | null, info: MailtrapResponse) => void
): void;
sendMail(mailOptions: MailtrapMailOptions): Promise<MailtrapResponse>;
sendMail(
mailOptions: MailtrapMailOptions | MailtrapMailOptionsSandbox
): Promise<MailtrapResponse>;
}
Loading