Skip to content

Commit

Permalink
fix and test using default sender value for emails
Browse files Browse the repository at this point in the history
  • Loading branch information
despadam committed Jan 28, 2025
1 parent 6556f79 commit 78d656b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const actionType = "email";
export interface EmailJobActionOptions extends JobActionOptions {
actionType: typeof actionType;
to: string;
from: string;
from?: string;
subject: string;
bodyTemplateFile: string;
}
Expand All @@ -24,7 +24,7 @@ export function isEmailJobActionOptions(
return (
opts.actionType === actionType &&
typeof opts.to === "string" &&
typeof opts.from === "string" &&
(opts.from === undefined || typeof opts.from === "string") &&
typeof opts.subject === "string" &&
typeof opts.bodyTemplateFile === "string"
);
Expand Down
55 changes: 55 additions & 0 deletions src/config/job-config/actions/emailaction/emailaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { EmailJobAction } from "./emailaction";
import { EmailJobActionOptions } from "./emailaction.interface";
import { JobClass } from "../../../../jobs/schemas/job.schema";
import { MailService } from "src/common/mail.service";
import { MailerService } from "@nestjs-modules/mailer";

jest.mock("src/common/mail.service");
jest.mock("@nestjs-modules/mailer");

describe("EmailJobAction", () => {
const config: EmailJobActionOptions = {
Expand Down Expand Up @@ -59,3 +61,56 @@ describe("EmailJobAction", () => {
);
});
});

describe("EmailJobAction with default sender", () => {
const config: EmailJobActionOptions = {
actionType: "email",
to: "[email protected]",
subject: "Job {{id}} completed",
bodyTemplateFile: "src/common/email-templates/job-template.spec.html",
};

const mailService = {
sendMail: jest.fn(),
} as unknown as MailService;

const mailerService = {
sendMail: jest.fn(),
} as unknown as MailerService;

const action = new EmailJobAction(mailService, config);

beforeEach(() => {
jest.clearAllMocks();
});

it("should be configured successfully", async () => {
expect(action).toBeDefined();
});

it("should send an email with default sender successfully", async () => {
const job = {
id: "12345",
type: "testemail",
} as JobClass;

const defaultFrom = "[email protected]";
(mailerService.sendMail as jest.Mock).mockImplementation((mailOptions) => {
mailOptions.from = defaultFrom;
return Promise.resolve();
});

(mailService.sendMail as jest.Mock).mockImplementation((mailOptions) => {
return mailerService.sendMail(mailOptions);
});

await action.performJob(job);

expect(mailerService.sendMail).toHaveBeenCalledWith({
to: "[email protected]",
from: defaultFrom,
subject: "Job 12345 completed",
html: "Your testemail job with ID 12345 has been completed successfully.",
});
});
});
6 changes: 4 additions & 2 deletions src/config/job-config/actions/emailaction/emailaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { MailService } from "src/common/mail.service";
*/
export class EmailJobAction implements JobAction<JobDto> {
private toTemplate: TemplateJob;
private from: string;
private from?: string = undefined;
private subjectTemplate: TemplateJob;
private bodyTemplate: TemplateJob;

Expand All @@ -36,7 +36,9 @@ export class EmailJobAction implements JobAction<JobDto> {

Logger.log("EmailJobAction parameters are valid.", "EmailJobAction");

this.from = options.from as string;
if (options["from"]) {
this.from = options.from as string;
}
this.toTemplate = compileJob(options.to);
this.subjectTemplate = compileJob(options.subject);

Expand Down

0 comments on commit 78d656b

Please sign in to comment.