From 2f5c522cc39da1a6f703baa53775bb9fa5cabd3c Mon Sep 17 00:00:00 2001 From: bot-implementer Date: Tue, 25 Aug 2026 10:45:28 +0000 Subject: [PATCH] test: unit tests for the mailto URL builder --- tests/mailto.test.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 tests/mailto.test.js diff --git a/tests/mailto.test.js b/tests/mailto.test.js new file mode 100644 index 0000000..30b8ad9 --- /dev/null +++ b/tests/mailto.test.js @@ -0,0 +1,33 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { buildMailtoUrl } from "../js/mailto.js"; + +test("builds a plain mailto URL when there is no subject or body", () => { + assert.equal(buildMailtoUrl({ to: "ada@example.com" }), "mailto:ada@example.com"); +}); + +test("includes subject and body as URL-encoded query parameters", () => { + const url = buildMailtoUrl({ + to: "owner@example.com", + subject: "Hello, Ada!", + body: "Line one\nLine two & ", + }); + + assert.ok(url.startsWith("mailto:owner@example.com?")); + + const params = new URL(url).searchParams; + assert.equal(params.get("subject"), "Hello, Ada!"); + assert.equal(params.get("body"), "Line one\nLine two & "); +}); + +test("omits empty subject and body", () => { + const url = buildMailtoUrl({ to: "owner@example.com", subject: "", body: "" }); + assert.equal(url, "mailto:owner@example.com"); +}); + +test("encodes spaces and special characters so the mail client opens pre-filled", () => { + const url = buildMailtoUrl({ to: "o@example.com", subject: "a b", body: "x&y=z" }); + const params = new URL(url).searchParams; + assert.equal(params.get("subject"), "a b"); + assert.equal(params.get("body"), "x&y=z"); +});