-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathutf8-encoding-decoding.test.js
More file actions
47 lines (38 loc) · 1.5 KB
/
utf8-encoding-decoding.test.js
File metadata and controls
47 lines (38 loc) · 1.5 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
38
39
40
41
42
43
44
45
46
47
"use strict";
import { jsonStringifyWithUtfEscape } from "../utils/json-encoding";
const jsonData1 = "🦊";
const jsonData2 = "🦊🤗😍";
const jsonData3 = "üåă";
const decodedData1 = '"\\u00f0\\u009f\\u00a6\\u008a"';
const decodedData2 =
'"\\u00f0\\u009f\\u00a6\\u008a\\u00f0\\u009f\\u00a4\\u0097\\u00f0\\u009f\\u0098\\u008d"';
const decodedData3 = '"\\u00c3\\u00bc\\u00c3\\u00a5\\u00c4\\u0083"';
describe("JSON encode", () => {
it("Encodes single emoji", () => {
expect(jsonStringifyWithUtfEscape(jsonData1)).toBe(decodedData1);
});
it("Encodes multiple emojis", () => {
expect(jsonStringifyWithUtfEscape(jsonData2)).toBe(decodedData2);
});
it("Various characters", () => {
expect(jsonStringifyWithUtfEscape(jsonData3)).toBe(decodedData3);
});
it("passes through non-string data", () => {
const foo42 = { foo: 42 };
expect(jsonStringifyWithUtfEscape(foo42)).toBe(JSON.stringify(foo42));
});
});
describe("Decodes", () => {
it("With single emoji", () => {
const parsedJson = JSON.parse(decodedData1);
expect(decodeURIComponent(escape(parsedJson))).toBe(jsonData1);
});
it("With multiple emojis", () => {
const parsedJson = JSON.parse(decodedData2);
expect(decodeURIComponent(escape(parsedJson))).toBe(jsonData2);
});
it("With various character", () => {
const parsedJson = JSON.parse(decodedData3);
expect(decodeURIComponent(escape(parsedJson))).toBe(jsonData3);
});
});