-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathlocalize_test.coffee
244 lines (202 loc) · 10.9 KB
/
localize_test.coffee
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
do ($ = jQuery) ->
module = QUnit.module
test = QUnit.test
testOpts = {}
asyncTest = (desc, testFn) ->
test desc, (assert) ->
done = assert.async()
deferredAssertions = testFn(assert)
deferredAssertions.then -> done()
localizableTagWithRel = (tag, localizeKey, attributes) ->
t = $("<#{tag}>").attr("rel", "localize[#{localizeKey}]")
applyTagAttributes(t, attributes)
localizableTagWithDataLocalize = (tag, localizeKey, attributes) ->
t = $("<#{tag}>").attr("data-localize", localizeKey)
applyTagAttributes(t, attributes)
applyTagAttributes = (tag, attributes) ->
if attributes.text?
tag.text(attributes.text)
delete attributes.text
if attributes.val?
tag.val(attributes.val)
delete attributes.val
tag.attr(k,v) for k, v of attributes
tag
module "Basic Usage",
beforeEach: ->
testOpts = language: "ja", pathPrefix: "lang"
asyncTest "basic tag text substitution", (assert) ->
t = localizableTagWithRel("p", "basic", text: "basic fail")
t.localize("test", testOpts).localizePromise.then ->
assert.equal t.text(), "basic success"
asyncTest "basic tag text substitution using data-localize instead of rel", (assert) ->
t = localizableTagWithDataLocalize("p", "basic", text: "basic fail")
t.localize("test", testOpts).localizePromise.then ->
assert.equal t.text(), "basic success"
asyncTest "basic tag text substitution with nested key", (assert) ->
t = localizableTagWithRel("p", "test.nested", text: "nested fail")
t.localize("test", testOpts).localizePromise.then ->
assert.equal t.text(), "nested success"
asyncTest "basic tag text substitution for special title key", (assert) ->
t = localizableTagWithDataLocalize("p", "with_title", text: "with_title element fail", title: "with_title title fail")
t.localize("test", testOpts).localizePromise.then ->
assert.equal t.text(), "with_title text success"
assert.equal t.attr("title"), "with_title title success"
asyncTest "input tag value substitution", (assert) ->
t = localizableTagWithRel("input", "test.input", val: "input fail")
t.localize("test", testOpts).localizePromise.then ->
assert.equal t.val(), "input success"
asyncTest "input test value after second localization without key", (assert) ->
t = localizableTagWithRel("input", "test.input", val: "input fail")
d = $.Deferred()
t.localize("test", testOpts).localizePromise.then ->
t.localize("test2", testOpts).localizePromise.then ->
assert.equal t.val(), "input success"
d.resolve()
d
asyncTest "input tag placeholder substitution", (assert) ->
t = localizableTagWithRel("input", "test.input", placeholder: "placeholder fail")
t.localize("test", testOpts).localizePromise.then ->
assert.equal t.attr("placeholder"), "input success"
asyncTest "textarea tag placeholder substitution", (assert) ->
t = localizableTagWithRel("textarea", "test.input", placeholder: "placeholder fail")
t.localize("test", testOpts).localizePromise.then ->
assert.equal t.attr("placeholder"), "input success"
asyncTest "titled input tag value substitution", (assert) ->
t = localizableTagWithRel("input", "test.input_as_obj", val: "input_as_obj fail")
t.localize("test", testOpts).localizePromise.then ->
assert.equal t.val(), "input_as_obj value success"
asyncTest "titled input tag title substitution", (assert) ->
t = localizableTagWithRel("input", "test.input_as_obj", val: "input_as_obj fail")
t.localize("test", testOpts).localizePromise.then ->
assert.equal t.attr("title"), "input_as_obj title success"
asyncTest "titled input tag placeholder substitution", (assert) ->
t = localizableTagWithRel("input", "test.input_as_obj", placeholder: "placeholder fail")
t.localize("test", testOpts).localizePromise.then ->
assert.equal t.attr("placeholder"), "input_as_obj value success"
asyncTest "image tag src, alt, and title substitution", (assert) ->
t = localizableTagWithRel("img", "test.ruby_image", src: "ruby_square.gif", alt: "a square ruby", title: "A Square Ruby")
t.localize("test", testOpts).localizePromise.then ->
assert.equal t.attr("src"), "ruby_round.gif"
assert.equal t.attr("alt"), "a round ruby"
assert.equal t.attr("title"), "A Round Ruby"
asyncTest "link tag href substitution", (assert) ->
t = localizableTagWithRel("a", "test.link", href: "http://fail", text: "fail")
t.localize("test", testOpts).localizePromise.then ->
assert.equal t.attr("href"), "http://success"
assert.equal t.text(), "success"
asyncTest "chained call", (assert) ->
t = localizableTagWithRel("p", "basic", text: "basic fail")
t.localize("test", testOpts).localize("test", testOpts).localizePromise.then ->
assert.equal t.text(), "basic success"
asyncTest "alternative file extension", (assert) ->
t = localizableTagWithRel("p", "basic", text: "basic fail")
t.localize("test", $.extend({ fileExtension: "foo" }, testOpts)).localizePromise.then ->
assert.equal t.text(), "basic success foo"
selectTag = null
module "Basic Usage for <options",
beforeEach: ->
testOpts = language: "ja", pathPrefix: "lang"
selectTag = $('<select>
<optgroup rel="localize[test.optgroup]" label="optgroup fail">
<option rel="localize[test.option]" value="1">option fail</option>
</optgroup>
</select>')
asyncTest "optgroup tag label substitution", (assert) ->
t = selectTag.find("optgroup")
t.localize("test", testOpts).localizePromise.then ->
assert.equal t.attr("label"), "optgroup success"
asyncTest "option tag text substitution", (assert) ->
t = selectTag.find("option")
t.localize("test", testOpts).localizePromise.then ->
assert.equal t.text(), "option success"
module "Options"
asyncTest "fallback language loads", (assert) ->
opts = language: "fo", fallback: "ja", pathPrefix: "lang"
t = localizableTagWithRel("p", "basic", text: "basic fail")
t.localize("test", opts).localizePromise.then ->
assert.equal t.text(), "basic success"
asyncTest "pathPrefix loads lang files from custom path", (assert) ->
opts = language: "fo", pathPrefix: "/test/lang/custom"
t = localizableTagWithRel("p", "path_prefix", text: "pathPrefix fail")
t.localize("test", opts).localizePromise.then ->
assert.equal t.text(), "pathPrefix success"
asyncTest "custom callback is fired", (assert) ->
opts = language: "ja", pathPrefix: "lang"
opts.callback = (data, defaultCallback) ->
data.custom_callback = "custom callback success"
defaultCallback(data)
t = localizableTagWithRel("p", "custom_callback", text: "custom callback fail")
t.localize("test", opts).localizePromise.then ->
assert.equal t.text(), "custom callback success"
asyncTest "language with country code", (assert) ->
opts = language: "ja-XX", pathPrefix: "lang"
t = localizableTagWithRel("p", "message", text: "country code fail")
t.localize("test", opts).localizePromise.then ->
assert.equal t.text(), "country code success"
# Ref: https://github.com/coderifous/jquery-localize/issues/50
asyncTest "three-letter language code", (assert) ->
opts = language: "ast", pathPrefix: "lang"
t = localizableTagWithRel("p", "basic", text: "basic fail")
t.localize("test", opts).localizePromise.then ->
assert.equal t.text(), "basic success"
# Ref: https://github.com/coderifous/jquery-localize/issues/47
asyncTest "language-country code with no language-only file", (assert) ->
opts = language: "zh-CN", pathPrefix: "lang"
t = localizableTagWithRel("p", "basic", text: "basic fail")
t.localize("test", opts).localizePromise.then ->
assert.equal t.text(), "basic success zh-CN"
module "Language optimization"
asyncTest "skipping language using string match", (assert) ->
opts = language: "en", pathPrefix: "lang", skipLanguage: "en"
t = localizableTagWithRel("p", "en_message", text: "en not loaded")
t.localize("test", opts).localizePromise.then ->
assert.equal t.text(), "en not loaded"
asyncTest "skipping language using regex match", (assert) ->
opts = language: "en-US", pathPrefix: "lang", skipLanguage: /^en/
t = localizableTagWithRel("p", "en_us_message", text: "en-US not loaded")
t.localize("test", opts).localizePromise.then ->
assert.equal t.text(), "en-US not loaded"
asyncTest "skipping language using array match", (assert) ->
opts = language: "en", pathPrefix: "lang", skipLanguage: ["en", "en-US"]
t = localizableTagWithRel("p", "en_message", text: "en not loaded")
t.localize("test", opts).localizePromise.then ->
assert.equal t.text(), "en not loaded"
asyncTest "skipping region language using array match", (assert) ->
opts = language: "en-US", pathPrefix: "lang", skipLanguage: ["en", "en-US"]
t = localizableTagWithRel("p", "en_us_message", text: "en-US not loaded")
t.localize("test", opts).localizePromise.then ->
assert.equal t.text(), "en-US not loaded"
# Ref: https://github.com/coderifous/jquery-localize/issues/62
module "Using object as data source"
asyncTest "basic tag text substitution using object as data source", (assert) ->
obj = basic: "basic success"
t = localizableTagWithRel("p", "basic", text: "basic fail")
t.localize(obj).localizePromise.then ->
assert.equal t.text(), "basic success"
asyncTest "custom callback is fired when object is used as data source", (assert) ->
opts = {}
opts.callback = (data, defaultCallback) ->
data.custom_callback = "custom callback success"
defaultCallback(data)
t = localizableTagWithRel("p", "custom_callback", text: "custom callback fail")
t.localize({}, opts).localizePromise.then ->
assert.equal t.text(), "custom callback success"
asyncTest "tag text must not be replaced if matching object property contains a function", (assert) ->
obj = "function": (->)
t = localizableTagWithRel("p", "function", text: "this text should remain unchanged")
t.localize(obj).localizePromise.then ->
assert.equal t.text(), "this text should remain unchanged"
asyncTest "input value must not be replaced if matching object property contains a function", (assert) ->
obj = "function": (->)
t = localizableTagWithRel("input", "function", text: "remain after default callback")
t.localize(obj).localizePromise.then ->
assert.equal t.text(), "remain after default callback"
asyncTest "input value must not be replaced if custom callback introduced a matching property that contains a function", (assert) ->
opts = {}
opts.callback = (data, defaultCallback) ->
data.added_function = (->)
defaultCallback(data)
t = localizableTagWithRel("input", "added_function", text: "remain after custom callback")
t.localize({}, opts).localizePromise.then ->
assert.equal t.text(), "remain after custom callback"