|
| 1 | +// Emoji interpolation in the `markdown` block — it renders through react-markdown |
| 2 | +// (not the mrkdwn parser) and previously showed `:shortcode:` as literal text. |
| 3 | +// These lock in parity with mrkdwn blocks: standard, aliases, skin tones, custom |
| 4 | +// emoji via hooks, and code-span exclusion. Run against the built dist/ via Node's |
| 5 | +// built-in runner (no test framework needed); CI builds before testing. |
| 6 | + |
| 7 | +import test from "node:test"; |
| 8 | +import assert from "node:assert/strict"; |
| 9 | +import React from "react"; |
| 10 | +import ReactDOMServer from "react-dom/server"; |
| 11 | +import { Message } from "../dist/index.mjs"; |
| 12 | + |
| 13 | +const render = (text, hooks) => |
| 14 | + ReactDOMServer.renderToStaticMarkup( |
| 15 | + React.createElement(Message, { |
| 16 | + logo: "logo.png", |
| 17 | + name: "Tester", |
| 18 | + theme: "light", |
| 19 | + hooks, |
| 20 | + blocks: [{ type: "markdown", text }], |
| 21 | + }), |
| 22 | + ); |
| 23 | + |
| 24 | +test("standard shortcodes render as unicode", () => { |
| 25 | + const out = render(":tada:"); |
| 26 | + assert.match(out, /🎉/); |
| 27 | + // the literal shortcode must be gone — that was the bug. |
| 28 | + assert.doesNotMatch(out, /:tada:/); |
| 29 | +}); |
| 30 | + |
| 31 | +test("surrounding text and multiple emoji are preserved", () => { |
| 32 | + const out = render("ship it :tada: nice :smile:"); |
| 33 | + assert.match(out, /ship it/); |
| 34 | + assert.match(out, /nice/); |
| 35 | + assert.match(out, /🎉/); |
| 36 | + assert.match(out, /😄/); |
| 37 | +}); |
| 38 | + |
| 39 | +test("shortcode aliases resolve via the fallback map", () => { |
| 40 | + // `thumbsup` isn't a node-emoji name; it resolves through the library's |
| 41 | + // missing-emoji map, exactly like it does in mrkdwn blocks. |
| 42 | + assert.match(render(":thumbsup:"), /👍/); |
| 43 | +}); |
| 44 | + |
| 45 | +test("skin-tone modifiers render", () => { |
| 46 | + const out = render(":wave::skin-tone-3:"); |
| 47 | + assert.match(out, /👋/); |
| 48 | + assert.match(out, /🏼/); |
| 49 | +}); |
| 50 | + |
| 51 | +test("custom emoji go through hooks.emoji while standard emoji fall back to parse()", () => { |
| 52 | + const hooks = { |
| 53 | + emoji: ({ name }, parse) => { |
| 54 | + if (name === "blob-dance") { |
| 55 | + return React.createElement("img", { |
| 56 | + className: "custom-emoji", |
| 57 | + "data-name": name, |
| 58 | + src: `https://cdn.example.com/${name}.png`, |
| 59 | + alt: name, |
| 60 | + }); |
| 61 | + } |
| 62 | + return parse({ name }); |
| 63 | + }, |
| 64 | + }; |
| 65 | + |
| 66 | + const out = render("standard :tada: custom :blob-dance:", hooks); |
| 67 | + // custom name → consumer-provided image |
| 68 | + assert.match(out, /custom-emoji/); |
| 69 | + assert.match(out, /data-name="blob-dance"/); |
| 70 | + assert.doesNotMatch(out, /:blob-dance:/); |
| 71 | + // standard name → unicode via the parse() fallback |
| 72 | + assert.match(out, /🎉/); |
| 73 | +}); |
| 74 | + |
| 75 | +test("emoji inside code spans are left untouched", () => { |
| 76 | + const out = render("`:tada:`"); |
| 77 | + assert.match(out, /:tada:/); |
| 78 | + assert.doesNotMatch(out, /🎉/); |
| 79 | +}); |
| 80 | + |
| 81 | +test("emoji inside fenced code blocks are left untouched", () => { |
| 82 | + const out = render("```\n:tada:\n```"); |
| 83 | + assert.match(out, /:tada:/); |
| 84 | + assert.doesNotMatch(out, /🎉/); |
| 85 | +}); |
0 commit comments