-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add tests for mailer plugin (#222)
- Loading branch information
Showing
6 changed files
with
161 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/mailer/src/__test__/helpers/createMailerConfig.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const createMailerConfig = () => { | ||
return { | ||
mailer: { | ||
defaults: { | ||
from: { | ||
address: "sender@example.com", | ||
name: "Mailer Team", | ||
}, | ||
}, | ||
test: { enabled: true, path: "/test/email", to: "receiver@example.com" }, | ||
templating: { templateFolder: "mjml/templates" }, | ||
templateData: { exampleUrl: "http://localhost:2000/" }, | ||
transport: { | ||
auth: { pass: "pass", user: "user" }, | ||
host: "localhost", | ||
port: 20073, | ||
requireTLS: false, | ||
secure: false, | ||
}, | ||
}, | ||
}; | ||
}; | ||
|
||
export default createMailerConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import fastify from "fastify"; | ||
import { describe, expect, it, vi, beforeEach } from "vitest"; | ||
|
||
import createMailerConfig from "./helpers/createMailerConfig"; | ||
|
||
import type { FastifyInstance } from "fastify"; | ||
|
||
const nodemailerMjmlPluginMock = vi.fn(); | ||
const htmlToTextMock = vi.fn(); | ||
const useMock = vi.fn(); | ||
const sendMailMock = vi.fn().mockImplementation((config, callback) => { | ||
callback(); | ||
}); | ||
const createTransportMock = vi.fn().mockReturnValue({ | ||
sendMail: sendMailMock, | ||
use: useMock, | ||
}); | ||
|
||
vi.mock("nodemailer", () => ({ | ||
createTransport: createTransportMock, | ||
})); | ||
|
||
vi.mock("nodemailer-mjml", () => ({ | ||
nodemailerMjmlPlugin: nodemailerMjmlPluginMock, | ||
})); | ||
|
||
vi.mock("nodemailer-html-to-text", () => ({ | ||
htmlToText: htmlToTextMock, | ||
})); | ||
|
||
describe("Mailer", async () => { | ||
let api: FastifyInstance; | ||
|
||
/* eslint-disable-next-line node/no-unsupported-features/es-syntax */ | ||
const { default: plugin } = await import("../plugin"); | ||
|
||
beforeEach(async () => { | ||
api = await fastify(); | ||
|
||
api.decorate("config", createMailerConfig()); | ||
}); | ||
|
||
it("Create Mailer instance with ", async () => { | ||
const { transport, defaults, templating } = createMailerConfig().mailer; | ||
await api.register(plugin); | ||
|
||
expect(createTransportMock).toHaveBeenCalledWith(transport, defaults); | ||
|
||
expect(useMock).toHaveBeenCalledWith("compile", nodemailerMjmlPluginMock()); | ||
|
||
expect(useMock).toHaveBeenCalledWith("compile", htmlToTextMock()); | ||
|
||
expect(nodemailerMjmlPluginMock).toHaveBeenCalledWith({ | ||
templateFolder: templating.templateFolder, | ||
}); | ||
}); | ||
|
||
it("Should throw error if mailer already registerd to api", async () => { | ||
await api.register(plugin); | ||
|
||
await expect(api.register(plugin)).rejects.toThrowError( | ||
"fastify-mailer has already been registered" | ||
); | ||
}); | ||
|
||
it("Should call SendMail method ", async () => { | ||
const { | ||
templateData, | ||
test: { path, to }, | ||
} = createMailerConfig().mailer; | ||
|
||
await api.register(plugin); | ||
|
||
await api.inject({ | ||
method: "GET", | ||
path: path, | ||
}); | ||
|
||
expect(sendMailMock).toHaveBeenCalledWith( | ||
{ | ||
html: expect.stringContaining("<!doctype html>"), | ||
subject: "test email", | ||
to: to, | ||
templateData: templateData, | ||
}, | ||
expect.any(Function) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
{ | ||
"extends": "tsconfig/base.json", | ||
"exclude": ["src/**/__test__/**/*"], | ||
"compilerOptions": { | ||
"outDir": "./dist", | ||
"baseUrl": "./", | ||
"outDir": "./dist" | ||
}, | ||
"include": [ | ||
"src/**/*.ts" | ||
] | ||
"include": ["src/**/*.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.