-
-
Notifications
You must be signed in to change notification settings - Fork 438
Expand file tree
/
Copy pathcompile.test.ts
More file actions
172 lines (143 loc) · 4.54 KB
/
compile.test.ts
File metadata and controls
172 lines (143 loc) · 4.54 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
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
import generate from "@babel/generator"
import { compile, createCompiledCatalog } from "./compile"
describe("compile", () => {
const getSource = (message) =>
generate(compile(message) as any, {
compact: true,
minified: true,
}).code
it("should optimize string only messages", () => {
expect(getSource("Hello World")).toEqual('"Hello World"')
})
it("should allow escaping syntax characters", () => {
expect(getSource("'{name}'")).toEqual('"{name}"')
expect(getSource("''")).toEqual('"\'"')
})
it("should compile arguments", () => {
expect(getSource("{name}")).toEqual('[["name"]]')
expect(getSource("B4 {name} A4")).toEqual('["B4 ",["name"]," A4"]')
})
it("should compile arguments with formats", () => {
expect(getSource("{name, number}")).toEqual('[["name","number"]]')
expect(getSource("{name, number, percent}")).toEqual(
'[["name","number","percent"]]'
)
})
it("should compile plural", () => {
expect(getSource("{name, plural, one {Book} other {Books}}")).toEqual(
'[["name","plural",{one:"Book",other:"Books"}]]'
)
expect(
getSource("{name, plural, one {Book} other {{name} Books}}")
).toEqual('[["name","plural",{one:"Book",other:[["name"]," Books"]}]]')
expect(getSource("{name, plural, one {Book} other {# Books}}")).toEqual(
'[["name","plural",{one:"Book",other:["#"," Books"]}]]'
)
expect(
getSource(
"{name, plural, offset:1 =0 {No Books} one {Book} other {# Books}}"
)
).toEqual(
'[["name","plural",{offset:1,0:"No Books",one:"Book",other:["#"," Books"]}]]'
)
})
it("should compile select", () => {
expect(getSource("{name, select, male {He} female {She}}")).toEqual(
'[["name","select",{male:"He",female:"She"}]]'
)
expect(getSource("{name, select, male {He} female {{name} She}}")).toEqual(
'[["name","select",{male:"He",female:[["name"]," She"]}]]'
)
expect(getSource("{name, select, male {He} female {# She}}")).toEqual(
'[["name","select",{male:"He",female:"# She"}]]'
)
})
it("should report failed message on error", () => {
expect(() =>
getSource("{value, plural, one {Book} other {Books")
).toThrowErrorMatchingSnapshot()
})
})
describe("createCompiledCatalog", () => {
describe("options.namespace", () => {
const getCompiledCatalog = (namespace) =>
createCompiledCatalog(
"fr",
{},
{
namespace,
}
)
it("should compile with es", () => {
expect(getCompiledCatalog("es")).toMatchSnapshot()
})
it("should compile with window", () => {
expect(getCompiledCatalog("window.test")).toMatchSnapshot()
})
it("should compile with global", () => {
expect(getCompiledCatalog("global.test")).toMatchSnapshot()
})
it("should error with invalid value", () => {
expect(() => getCompiledCatalog("global")).toThrowErrorMatchingSnapshot()
})
})
describe("options.strict", () => {
const getCompiledCatalog = (strict) =>
createCompiledCatalog(
"cs",
{
Hello: "Ahoj",
Missing: "",
},
{
strict,
}
)
it("should return message key as a fallback translation", () => {
expect(getCompiledCatalog(false)).toMatchSnapshot()
})
it("should't return message key as a fallback in strict mode", () => {
expect(getCompiledCatalog(true)).toMatchSnapshot()
})
})
describe("options.pseudoLocale", () => {
const getCompiledCatalog = (pseudoLocale) =>
createCompiledCatalog(
"ps",
{
Hello: "Ahoj",
},
{
pseudoLocale,
}
)
it("should return catalog with pseudolocalized messages", () => {
expect(getCompiledCatalog("ps")).toMatchSnapshot()
})
it("should return compiled catalog when pseudoLocale doesn't match current locale", () => {
expect(getCompiledCatalog("en")).toMatchSnapshot()
})
})
describe("options.compilerBabelOptions", () => {
const getCompiledCatalog = (opts = {}) =>
createCompiledCatalog(
"ru",
{
Hello: "Alohà",
},
opts
)
it("by default should return catalog without ASCII chars", () => {
expect(getCompiledCatalog()).toMatchSnapshot()
})
it("should return catalog without ASCII chars", () => {
expect(getCompiledCatalog({
compilerBabelOptions: {
jsescOption: {
minimal: false,
}
}
})).toMatchSnapshot()
})
})
})