|
| 1 | +/** |
| 2 | + * Tests for ICU MessageFormat escaping utility |
| 3 | + */ |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | +import IntlMessageFormat from "intl-messageformat"; |
| 6 | +import { escapeTextForICU } from "./utils"; |
| 7 | + |
| 8 | +describe("escapeTextForICU", () => { |
| 9 | + it.each([ |
| 10 | + // [input, expectedEscaped, expectedRendered] |
| 11 | + ["It's a test", "It''s a test", "It's a test"], |
| 12 | + [ |
| 13 | + "Use {braces} in code", |
| 14 | + "Use '{'braces'}' in code", |
| 15 | + "Use {braces} in code", |
| 16 | + ], |
| 17 | + [ |
| 18 | + "Use <>content</> for fragments", |
| 19 | + "Use '<'>content'<'/> for fragments", |
| 20 | + "Use <>content</> for fragments", |
| 21 | + ], |
| 22 | + ["Use #hashtags", "Use '#'hashtags", "Use '#'hashtags"], // ICU doesn't unescape # |
| 23 | + [ |
| 24 | + "It's {test} with <tags> and #hash", |
| 25 | + "It''s '{'test'}' with '<'tags> and '#'hash", |
| 26 | + "It's {test} with <tags> and '#'hash", |
| 27 | + ], |
| 28 | + [ |
| 29 | + "Use <Fragment>content</Fragment>", |
| 30 | + "Use '<'Fragment>content'<'/Fragment>", |
| 31 | + "Use <Fragment>content</Fragment>", |
| 32 | + ], |
| 33 | + ["Plain text", "Plain text", "Plain text"], |
| 34 | + [ |
| 35 | + "Write <>text</> or <Fragment>text</Fragment>", |
| 36 | + "Write '<'>text'<'/> or '<'Fragment>text'<'/Fragment>", |
| 37 | + "Write <>text</> or <Fragment>text</Fragment>", |
| 38 | + ], |
| 39 | + ["<>", "'<'>", "<>"], |
| 40 | + ["</>", "'<'/>", "</>"], |
| 41 | + ])( |
| 42 | + "should escape %s correctly", |
| 43 | + (input, expectedEscaped, expectedRendered) => { |
| 44 | + const escaped = escapeTextForICU(input); |
| 45 | + expect(escaped).toBe(expectedEscaped); |
| 46 | + |
| 47 | + const formatter = new IntlMessageFormat(escaped, "en"); |
| 48 | + const result = formatter.format(); |
| 49 | + expect(result).toBe(expectedRendered); |
| 50 | + }, |
| 51 | + ); |
| 52 | +}); |
0 commit comments