Files
fnxandGitHub 4e38984747 test: add regression tests for message placeholder interpolation (#20318)
* test: add regression tests for message placeholder interpolation

* test: add test case for toString
2025-11-15 19:02:19 +01:00

87 lines
2.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const assert = require("chai").assert;
const {
getPlaceholderMatcher,
interpolate,
} = require("../../../lib/linter/interpolate");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
describe("getPlaceholderMatcher", () => {
it("returns a global regular expression", () => {
const matcher = getPlaceholderMatcher();
assert.strictEqual(matcher.global, true);
});
it("matches text with placeholders", () => {
const matcher = getPlaceholderMatcher();
assert.match("{{ placeholder }}", matcher);
});
it("does not match text without placeholders", () => {
const matcher = getPlaceholderMatcher();
assert.notMatch("no placeholders in sight", matcher);
});
it("captures the text inside the placeholder", () => {
const matcher = getPlaceholderMatcher();
const text = "{{ placeholder }}";
const matches = Array.from(text.matchAll(matcher));
assert.deepStrictEqual(matches, [[text, " placeholder "]]);
});
});
describe("interpolate()", () => {
it("passes through text without {{ }}", () => {
const message = "This is a very important message!";
assert.strictEqual(interpolate(message, {}), message);
});
it("passes through text with {{ }} that don’t match a key", () => {
const message = "This is a very important {{ message }}!";
assert.strictEqual(interpolate(message, {}), message);
});
it("Properly interpolates keys in {{ }}", () => {
assert.strictEqual(
interpolate("This is a very important {{ message }}!", {
message: "test",
}),
"This is a very important test!",
);
});
const values = [
// Expected (officially supported)
[1, "1"],
[100n, "100"],
[true, "true"],
[false, "false"],
[void 0, "undefined"],
[null, "null"],
// Unexpected
[{ prop: "value" }, "[object Object]"],
[{ toString: () => "stringified" }, "stringified"],
[[1, 2, 3], "1,2,3"],
[new Set([1, 2, 3]), "[object Set]"],
[new Map([["prop", "value"]]), "[object Map]"],
];
for (const [value, message] of values) {
it(`stringifies the value ${value}`, () => {
assert.strictEqual(interpolate("{{ value }}", { value }), message);
});
}
});