-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathjsonToRawHtmlScriptValue.test.mjs
More file actions
37 lines (32 loc) · 1.08 KB
/
jsonToRawHtmlScriptValue.test.mjs
File metadata and controls
37 lines (32 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// @ts-check
import { assertStrictEquals } from "@std/assert/strict-equals";
import { assertThrows } from "@std/assert/throws";
import jsonToRawHtmlScriptValue from "./jsonToRawHtmlScriptValue.mjs";
Deno.test("`jsonToRawHtmlScriptValue` with argument 1 `json` not a string.", () => {
assertThrows(
() => {
jsonToRawHtmlScriptValue(
// @ts-expect-error Testing invalid.
true,
);
},
TypeError,
"Argument 1 `json` must be a string.",
);
});
for (
const [target, replacement, description] of [
["&", "\\u0026", "ampersand"],
["<", "\\u003c", "less than"],
[">", "\\u003e", "greater than"],
["\u2028", "\\u2028", "line separator"],
["\u2029", "\\u2029", "paragraph separator"],
]
) {
Deno.test(`\`jsonToRawHtmlScriptValue\` with JSON containing ${description}.`, () => {
const json = `{"a":"${target}","b":"${target}"}`;
const escaped = jsonToRawHtmlScriptValue(json);
assertStrictEquals(escaped, `{"a":"${replacement}","b":"${replacement}"}`);
assertStrictEquals(JSON.stringify(JSON.parse(escaped)), json);
});
}