-
Notifications
You must be signed in to change notification settings - Fork 382
/
interpolate.test.ts
195 lines (171 loc) · 6.49 KB
/
interpolate.test.ts
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
import { compileMessage as compile } from "@lingui/message-utils/compileMessage"
import { interpolate } from "./interpolate"
import { Locale, Locales } from "./i18n"
describe("interpolate", () => {
const prepare = (translation: string, locale?: Locale, locales?: Locales) => {
const tokens = compile(translation)
return interpolate(tokens, locale || "en", locales)
}
it("should process string chunks with provided map fn", () => {
const tokens = compile(
"Message {value, plural, one {{value} Book} other {# Books}}",
(text) => `<${text}>`
)
expect(tokens).toEqual([
"<Message >",
[
"value",
"plural",
{
one: [["value"], "< Book>"],
other: ["#", "< Books>"],
},
],
])
})
it("should interpolate message with variable", () => {
const cache = compile("Hey {name}!")
expect(interpolate(cache, "en", [])({ name: "Joe" })).toEqual("Hey Joe!")
})
it("should not interpolate escaped placeholder", () => {
const msg = prepare("Hey '{name}'!")
expect(msg({ name: "Joe" })).toEqual("Hey {name}!")
})
it("should interpolate plurals", () => {
const plural = prepare(
"{value, plural, one {{value} Book} other {# Books}}"
)
expect(plural({ value: 1 })).toEqual("1 Book")
expect(plural({ value: 2 })).toEqual("2 Books")
const offset = prepare(
"{value, plural, offset:1 =0 {No Books} one {# Book} other {# Books}}"
)
expect(offset({ value: 0 })).toEqual("No Books")
expect(offset({ value: 2 })).toEqual("1 Book")
expect(offset({ value: 3 })).toEqual("2 Books")
})
it("should interpolate plurals with falsy value choice", () => {
const plural = prepare("{value, plural, one {} other {# Books}}")
expect(plural({ value: 1 })).toEqual("")
expect(plural({ value: 2 })).toEqual("2 Books")
})
it("should not replace `#` symbol passed in the variable in the jsx expression", () => {
const plural = prepare(
"{value, plural, one {There is a notification in <1>{documentTitle}</1>} other {There are # notifications in <1>{documentTitle}</1>}}"
)
expect(plural({ value: 1, documentTitle: "Title #1" })).toEqual(
"There is a notification in <1>Title #1</1>"
)
expect(plural({ value: 2, documentTitle: "Title #1" })).toEqual(
"There are 2 notifications in <1>Title #1</1>"
)
})
it("should replace more than one octothorpe symbols in message", () => {
const plural = prepare("{value, plural, one {} other {# and #}}")
expect(plural({ value: 2 })).toEqual("2 and 2")
})
it("when a value is defined (even when empty) plural will return it. Conversely, if a value is not defined, plural defaults to 'other'", () => {
const plural = prepare("{value, plural, =0 {} other {#% discount}}")
expect(plural({ value: 0 })).toEqual("")
expect(plural({ value: 1 })).toEqual("1% discount")
expect(plural({ value: 30 })).toEqual("30% discount")
})
it("should interpolate selectordinal", () => {
const cache = prepare(
"{value, selectordinal, one {#st Book} two {#nd Book}}"
)
expect(cache({ value: 1 })).toEqual("1st Book")
expect(cache({ value: 2 })).toEqual("2nd Book")
})
it("should support nested choice components", () => {
const cache = prepare(
`{
gender, select,
male {{numOfGuests, plural, one {He invites one guest} other {He invites # guests}}}
female {{numOfGuests, plural, one {She invites one guest} other {She invites # guests}}}
other {They is {gender}}}`
)
expect(cache({ numOfGuests: 1, gender: "male" })).toEqual(
"He invites one guest"
)
expect(cache({ numOfGuests: 3, gender: "male" })).toEqual(
"He invites 3 guests"
)
expect(cache({ numOfGuests: 1, gender: "female" })).toEqual(
"She invites one guest"
)
expect(cache({ numOfGuests: 3, gender: "female" })).toEqual(
"She invites 3 guests"
)
expect(cache({ numOfGuests: 3, gender: "unknown" })).toEqual(
"They is unknown"
)
})
it("should interpolate select", () => {
const cache = prepare("{value, select, female {She} other {They}}")
expect(cache({ value: "female" })).toEqual("She")
expect(cache({ value: "n/a" })).toEqual("They")
})
it("should support select with empty string choice", () => {
const cache = prepare("{value, select, female {} other {They}}")
expect(cache({ value: "female" })).toEqual("")
expect(cache({ value: "n/a" })).toEqual("They")
const cache2 = prepare("{value, select, female {0} other {They}}")
expect(cache2({ value: "female" })).toEqual("0")
expect(cache2({ value: "n/a" })).toEqual("They")
})
describe("Custom format", () => {
const testVector = [
["en", undefined, "0.1", "10%", "20%", "3/4/2017", "€0.10", "€1.00"],
[
"fr",
undefined,
"0,1",
"10 %",
"20 %",
"04/03/2017",
"0,10 €",
"1,00 €",
],
["fr", "fr-CH", "0,1", "10%", "20%", "04.03.2017", "0.10 €", "1.00 €"],
] as const
testVector.forEach((tc) => {
const [
locale,
locales,
expectedNumber,
expectedPercent1,
expectedPercent2,
expectedDate,
expectedCurrency1,
expectedCurrency2,
] = tc
it(`should interpolate custom format for locale=${locale} and locales=${locales}`, () => {
const number = prepare("{value, number}", locale, locales)
expect(number({ value: 0.1 })).toEqual(expectedNumber)
const percent = prepare("{value, number, percent}", locale, locales)
expect(percent({ value: 0.1 })).toEqual(expectedPercent1)
expect(percent({ value: 0.2 })).toEqual(expectedPercent2)
const now = new Date("3/4/2017")
const date = prepare("{value, date}", locale, locales)
expect(date({ value: now })).toEqual(expectedDate)
const formats = {
currency: {
style: "currency",
currency: "EUR",
minimumFractionDigits: 2,
} satisfies Intl.NumberFormatOptions,
}
const currency = prepare("{value, number, currency}", locale, locales)
expect(currency({ value: 0.1 }, formats)).toEqual(expectedCurrency1)
expect(currency({ value: 1 }, formats)).toEqual(expectedCurrency2)
})
})
})
it("should not crash on a unicode sequences", () => {
const cache = compile("Hey {name}!")
expect(interpolate(cache, "en", [])({ name: "Joe\\xaa" })).toEqual(
"Hey Joeª!"
)
})
})