Skip to content

Commit

Permalink
feat: add tests for mailer plugin (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nabin99 committed Feb 14, 2023
1 parent 229bc95 commit 984543d
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 28 deletions.
11 changes: 8 additions & 3 deletions packages/mailer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
"main": "./dist/dzangolab-fastify-mailer.umd.cjs",
"module": "./dist/dzangolab-fastify-mailer.js",
"types": "./dist/types/index.d.ts",
"files": ["dist"],
"files": [
"dist"
],
"scripts": {
"build": "vite build && tsc --emitDeclarationOnly && mv dist/src dist/types",
"lint": "eslint . --ext .js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --ignore-path .gitignore",
"lint:fix": "eslint . --ext .js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore",
"sort-package": "npx sort-package-json",
"test": "vitest run --coverage",
"typecheck": "tsc --noEmit -p tsconfig.json --composite false"
},
"dependencies": {
Expand All @@ -37,6 +40,7 @@
"@types/nodemailer-html-to-text": "3.1.0",
"@typescript-eslint/eslint-plugin": "5.50.0",
"@typescript-eslint/parser": "5.50.0",
"@vitest/coverage-istanbul": "0.28.4",
"eslint": "8.33.0",
"eslint-config-custom": "0.17.1",
"fastify": "4.10.2",
Expand All @@ -45,10 +49,11 @@
"prettier": "2.8.3",
"tsconfig": "0.17.1",
"typescript": "4.9.5",
"vite": "4.1.1"
"vite": "4.1.1",
"vitest": "0.28.4"
},
"peerDependencies": {
"@dzangolab/fastify-config": "0.17.1",
"@dzangolab/fastify-config": ">=0.17.1",
"fastify": ">=4.9.2",
"fastify-plugin": ">=4.3.0",
"mjml": ">=4.13.0"
Expand Down
24 changes: 24 additions & 0 deletions packages/mailer/src/__test__/helpers/createMailerConfig.ts
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;
89 changes: 89 additions & 0 deletions packages/mailer/src/__test__/mailer.spec.ts
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)
);
});
});
8 changes: 4 additions & 4 deletions packages/mailer/tsconfig.json
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"]
}
11 changes: 11 additions & 0 deletions packages/mailer/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,16 @@ export default defineConfig(({ mode }) => {
},
target: "es2022",
},
resolve: {
alias: {
"@/": new URL("src/", import.meta.url).pathname,
},
},
test: {
coverage: {
provider: "istanbul",
reporter: ["text", "json", "html"],
},
},
};
});
46 changes: 25 additions & 21 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 984543d

Please sign in to comment.