-
Notifications
You must be signed in to change notification settings - Fork 156
/
index.js
296 lines (260 loc) · 8.3 KB
/
index.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import React from "react";
import cookie from "cookie";
import intl from "../src/index";
import zhCN from "./locales/zh-CN";
import enUS from "./locales/en-US";
import enUSMore from "./locales/en-US-more";
const locales = {
"en-US": enUS,
"zh-CN": zhCN
};
test("Set specific locale", () => {
intl.init({ locales, currentLocale: "zh-CN" });
expect(intl.get("SIMPLE")).toBe("简单");
intl.init({ locales, currentLocale: "en-US" });
expect(intl.get("SIMPLE")).toBe("Simple");
});
test("Message with variables", () => {
intl.init({ locales, currentLocale: "en-US" });
expect(intl.get("HELLO", { name: "Tony" })).toBe("Hello, Tony");
});
test("Message with brace", () => {
intl.init({ locales, currentLocale: "en-US" });
expect(intl.get("BRACE1")).toBe("The format is {var}");
expect(intl.get("BRACE2")).toBe("The format is ${var}");
});
test("Set specific locale with nested notation", () => {
intl.init({ locales, currentLocale: "en-US" });
expect(intl.get("NESTED.HELLO")).toBe("Hello World");
expect(intl.get("NESTED.HELLO_NAME", { name: "World" })).toBe("Hello, World");
});
test("react-intl mirror API formatMessage:variables", () => {
intl.init({ locales, currentLocale: "en-US" });
const name = "Tony";
expect(
intl.formatMessage(
{ id: "HELLO", defaultMessage: `Hello, {name}` },
{ name }
)
).toBe(intl.get("HELLO", { name }));
});
test("react-intl mirror API formatMessage:defaultMessage", () => {
intl.init({ locales, currentLocale: "en-US" });
expect(intl.formatMessage({ id: "not-exist-key" })).toBe(
intl.get("not-exist-key")
);
});
test("react-intl mirror API formatHTMLMessage:variable", () => {
intl.init({ locales, currentLocale: "en-US" });
let reactEl = intl.formatHTMLMessage(
{ id: "TIP_VAR", defaultMessage: React.createElement("div") },
{
message: "your message"
}
);
expect(reactEl.props.dangerouslySetInnerHTML.__html).toBe(
"This is<span>your message</span>"
);
});
test("react-intl mirror API formatHTMLMessage:defaultMessage", () => {
intl.init({ locales, currentLocale: "en-US" });
let reactEl = intl.formatHTMLMessage({
id: "not-exist-key",
defaultMessage: React.createElement("div", {className: 'test'})
});
expect(reactEl.type).toBe('div');
expect(reactEl.props.className).toBe('test');
});
test("HTML Message without variables", () => {
intl.init({ locales, currentLocale: "en-US" });
let reactEl = intl.getHTML("TIP");
expect(reactEl.props.dangerouslySetInnerHTML.__html).toBe(
"This is <span>HTML</span>"
);
});
test("HTML Message with variables", () => {
intl.init({ locales, currentLocale: "en-US" });
let reactEl = intl.getHTML("TIP_VAR", { message: "your message" });
expect(reactEl.props.dangerouslySetInnerHTML.__html).toBe(
"This is<span>your message</span>"
);
});
test("HTML Message without variables", () => {
intl.init({ locales, currentLocale: "en-US" });
let reactEl = intl.getHTML("TIP");
expect(reactEl.props.dangerouslySetInnerHTML.__html).toBe(
"This is <span>HTML</span>"
);
});
test("HTML Message with variables", () => {
intl.init({ locales, currentLocale: "en-US" });
let reactEl = intl.getHTML("TIP_VAR", {
message: "your message"
});
expect(reactEl.props.dangerouslySetInnerHTML.__html).toBe(
"This is<span>your message</span>"
);
});
test("HTML Message with XSS attack", () => {
intl.init({ locales, currentLocale: "en-US" });
let reactEl = intl.getHTML("TIP_VAR", {
message: "<sctipt>alert(1)</script>"
});
expect(reactEl.props.dangerouslySetInnerHTML.__html).toBe(
"This is<span><sctipt>alert(1)</script></span>"
);
});
test("HTML Message with disable escape html", () => {
intl.init({ locales, currentLocale: "en-US", escapeHtml: false });
let reactEl = intl.getHTML("TIP_VAR", {
message: "<sctipt>alert(1)</script>"
});
expect(reactEl.props.dangerouslySetInnerHTML.__html).toBe(
"This is<span><sctipt>alert(1)</script></span>"
);
});
test("Message with Date", () => {
let start = new Date("Fri Apr 07 2017 17:08:33");
intl.init({ locales, currentLocale: "en-US" });
expect(
intl.get("SALE_START", {
start: start
})
).toBe("Sale begins 4/7/2017");
expect(
intl.get("SALE_END", {
start: start
})
).toBe("Sale begins April 7, 2017");
});
test("Message with Time", () => {
let expires = new Date("Fri Apr 07 2017 17:08:33");
intl.init({ locales, currentLocale: "en-US" });
expect(
intl.get("COUPON", {
expires: expires
})
).toBe("Coupon expires at 5:08:33 PM");
intl.init({ locales, currentLocale: "zh-CN" });
expect(
intl.get("COUPON", {
expires: expires
})
).toBe("优惠卷将在17:08:33过期");
});
test("Message with Currency", () => {
let price = 123456.78;
intl.init({ locales, currentLocale: "en-US" });
expect(
intl.get("SALE_PRICE", {
price: price
})
).toBe("The price is $123,456.78");
intl.init({ locales, currentLocale: "zh-CN" });
expect(
intl.get("SALE_PRICE", {
price: price
})
).toBe("售价CN¥ 123,456.78");
});
test("Message with plural", () => {
intl.init({ locales, currentLocale: "en-US" });
expect(
intl.get("PHOTO", {
num: 0
})
).toBe("You have no photos.");
expect(
intl.get("PHOTO", {
num: 1
})
).toBe("You have one photo.");
expect(
intl.get("PHOTO", {
num: 10
})
).toBe("You have 10 photos.");
intl.init({ locales, currentLocale: "zh-CN" });
expect(
intl.get("PHOTO", {
num: 1
})
).toBe("你有1张照片");
});
test("Without default message, just return empty string", () => {
intl.init({ locales, currentLocale: "en-US" });
expect(intl.get("not-exist-key")).toBe("");
});
test("Should call handler when message is not defined", () => {
const warningHandler = jest.fn();
intl.init({
locales, currentLocale: "en-US",
warningHandler
});
intl.get("not-exist-key");
expect(warningHandler).lastCalledWith('react-intl-universal key \"not-exist-key\" not defined in en-US');
});
test("Default message", () => {
intl.init({ locales, currentLocale: "en-US" });
expect(intl.get("not-exist-key").defaultMessage("this is default msg")).toBe(
"this is default msg"
);
expect(intl.get("not-exist-key").d("this is default msg")).toBe(
"this is default msg"
);
});
test("Default message with nested key", () => {
intl.init({ locales, currentLocale: "en-US" });
expect(intl.get("NOT_EXIST_KEY.HELLO").defaultMessage("Hello World")).toBe("Hello World");
});
test("Default message", () => {
intl.init({ locales, currentLocale: "en-US" });
expect(intl.get("not-exist-key").defaultMessage("this is default msg")).toBe(
"this is default msg"
);
});
test("Default HTML message", () => {
intl.init({ locales, currentLocale: "en-US" });
expect(
intl.getHTML("not-exist-key").defaultMessage("this is default msg")
).toBe("this is default msg");
});
test("Get locale from cookie", () => {
document.cookie = cookie.serialize("lang", "en-US");
expect(intl.getLocaleFromCookie({ cookieLocaleKey: "lang" })).toBe("en-US");
});
test("Get locale from URL", () => {
expect(intl.getLocaleFromURL({ urlLocaleKey: "lang" })).toBe(undefined);
// change url in jsdom: https://github.com/facebook/jest/issues/890
Object.defineProperty(window.location, "search", {
writable: true,
value: "?lang=en-US"
});
expect(intl.getLocaleFromURL({ urlLocaleKey: "lang" })).toBe("en-US");
});
test("Get locale from browser", () => {
expect(intl.getLocaleFromBrowser()).toBe("en-US");
});
test("Determine Locale", () => {
expect(intl.determineLocale()).toBe("en-US");
document.cookie = cookie.serialize("lang", "zh-CN");
expect(intl.determineLocale({ cookieLocaleKey: "lang" })).toBe("zh-CN");
});
test("Get dot key variables", () => {
intl.init({ locales, currentLocale: "en-US" });
expect(intl.get("DOT.HELLO")).toBe("Hello World");
});
test("Get init options", () => {
intl.init({ locales, currentLocale: "en-US" });
const { currentLocale } = intl.getInitOptions();
expect(currentLocale).toBe("en-US");
});
test("load mutiple locale data without overriding existing one", () => {
intl.init({ locales, currentLocale: "en-US" });
const localesMore = {
"en-US": enUSMore,
};
intl.load(localesMore);
expect(intl.get("SIMPLE")).toBe("Simple");
expect(intl.get("MORE")).toBe("More data");
});