-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathangular-localize-spec.js
336 lines (291 loc) · 13.1 KB
/
angular-localize-spec.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* global describe, beforeEach, module, it, inject, expect */
(function () {
'use strict';
describe('localize module', function () {
var i18nMock = {
Apples: function () {
return 'Äpfel';
},
'My hands are <strong>Bananas</strong>!': function () {
return 'Meine Hände sind <strong>Bananen</strong>!';
},
unsafeHTML: function () {
return '<p>XSS<script>console.log("XSS");</script> Test</p>';
},
textWithHTMLSpecialChars: function () {
return '1 & 2 < 3 > 2 & 1';
},
'Hello {name}!': function (data) {
return 'Hallo ' + data.name + '!';
},
'Hello <strong>{name}</strong>!': function (data) {
return 'Hallo <strong>' + data.name + '</strong>!';
}
};
beforeEach(function () {
module('localize');
module(function ($provide) {
$provide.decorator('$window', function ($delegate) {
$delegate.i18n = i18nMock;
return $delegate;
});
});
});
describe('escapeHTML filter', function () {
it('Should escape HTML special characters', inject(
function ($filter) {
expect($filter('escapeHTML')('<>&"'))
.toBe('<>&"');
}
));
it('Should not escape other non-ASCII characters', inject(
function ($filter) {
expect($filter('escapeHTML')('öäü'))
.toBe('öäü');
}
));
});
describe('localizeConfig service', function () {
it('Should return the $window.i18n object as property', inject(
function (localizeConfig) {
expect(localizeConfig.i18n).toBe(i18nMock);
}
));
});
describe('localize service', function () {
it('Should return the i18n translation function result', inject(
function (localize) {
expect(localize('Apples'))
.toBe('Äpfel');
}
));
it('Should return the key if there is no translation function', inject(
function (localize) {
expect(localize('Bananas'))
.toBe('Bananas');
}
));
it('Should replace translation placeholders with data argument values', inject(
function (localize) {
expect(localize('Hello {name}!', {name: 'Bob'}))
.toBe('Hallo Bob!');
}
));
it('Should use an empty object if the data argument is not provided', inject(
function (localize) {
expect(localize('Hello {name}!'))
.toBe('Hallo undefined!');
}
));
it('Should escape user data when the escape argument is true', inject(
function (localize) {
expect(localize('Hello {name}!', {name: '&<>'}, true))
.toBe('Hallo &<>!');
}
));
});
describe('localize filter', function () {
it('Should return the localize service as filter', inject(
function ($filter, localize) {
expect($filter('localize'))
.toBe(localize);
}
));
it('Should set the element text to the translation result', inject(
function ($compile, $rootScope) {
var element = $compile(
'<span>{{text | localize}}</span>'
)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('');
$rootScope.text = 'Apples';
$rootScope.$digest();
expect(element.text()).toBe('Äpfel');
}
));
it('Should replace translation placeholders with object values', inject(
function ($compile, $rootScope) {
var element = $compile(
'<span>{{text | localize:user}}</span>'
)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('');
$rootScope.text = 'Hello {name}!';
$rootScope.$digest();
expect(element.text()).toBe('Hallo undefined!');
$rootScope.user = {name: 'Bob'};
$rootScope.$digest();
expect(element.text()).toBe('Hallo Bob!');
}
));
it('Should escape user data passed to the translation function', inject(
function ($compile, $rootScope) {
var element = $compile(
'<span ng-bind-html="text | localize:user:true"></span>'
)($rootScope);
$rootScope.$digest();
expect(element.html()).toBe('');
$rootScope.text = 'Hello <strong>{name}</strong>!';
$rootScope.$digest();
expect(element.html()).toBe('Hallo <strong>undefined</strong>!');
$rootScope.user = {name: '&<>'};
$rootScope.$digest();
expect(element.html()).toBe('Hallo <strong>&<></strong>!');
}
));
});
describe('localize directive', function () {
it('Should use the translation key as element text if the key is the result', inject(
function ($compile, $rootScope) {
var element = $compile(
'<span localize="Bananas"></span>'
)($rootScope);
expect(element.text()).toBe('Bananas');
}
));
it('Should skip localization if the element content is the key and result', inject(
function ($compile, $rootScope) {
var element = $compile(
'<span localize>Bananas</span>'
)($rootScope);
expect(element.text()).toBe('Bananas');
}
));
it('Should set the element text to the translation result', inject(
function ($compile, $rootScope) {
var element = $compile(
'<span localize="Apples"></span>'
)($rootScope);
expect(element.text()).toBe('Äpfel');
}
));
it('Should use the element content as key for the translation function', inject(
function ($compile, $rootScope) {
var element = $compile(
'<span localize>Apples</span>'
)($rootScope);
expect(element.text()).toBe('Äpfel');
}
));
it('Should allow a safe subset of HTML for the translation result', inject(
function ($compile, $rootScope) {
var element = $compile(
'<span localize>My hands are <strong>Bananas</strong>!</span>'
)($rootScope);
expect(element.html()).toBe('Meine Hände sind <strong>Bananen</strong>!');
}
));
it('Should sanitize translation results and strip unsafe HTML', inject(
function ($compile, $rootScope) {
var element = $compile(
'<span localize>unsafeHTML</span>'
)($rootScope);
expect(element.html()).toBe('<p>XSS Test</p>');
}
));
it('Should insert attribute localize values as text content', inject(
function ($compile, $rootScope) {
var element = $compile(
'<span localize="textWithHTMLSpecialChars"></span>'
)($rootScope);
expect(element.html()).toBe('1 & 2 < 3 > 2 & 1');
}
));
it('Should replace translation placeholders with dataset values', inject(
function ($compile, $rootScope) {
var element = $compile(
'<span data-name="{{user.name}}" localize="Hello {name}!"></span>'
)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('Hallo !');
$rootScope.user = {name: 'Bob'};
$rootScope.$digest();
expect(element.text()).toBe('Hallo Bob!');
}
));
it('Should escape user data passed to the translation function', inject(
function ($compile, $rootScope) {
var element = $compile(
'<span data-name="{{user.name}}" localize>Hello <strong>{name}</strong>!</span>'
)($rootScope);
$rootScope.$digest();
expect(element.html()).toBe('Hallo <strong></strong>!');
$rootScope.user = {name: '&<>'};
$rootScope.$digest();
expect(element.html()).toBe('Hallo <strong>&<></strong>!');
}
));
it('Should set the input placeholder text to the translation result', inject(
function ($compile, $rootScope) {
var element = $compile(
'<input localize="Apples">'
)($rootScope);
expect(element.attr('placeholder')).toBe('Äpfel');
}
));
it('Should use the translation key as placeholder text if the key is the result', inject(
function ($compile, $rootScope) {
var element = $compile(
'<input localize="Bananas">'
)($rootScope);
expect(element.attr('placeholder')).toBe('Bananas');
}
));
});
describe('localize factory', function () {
it('Should create attribute based localize directives', function () {
module(function ($compileProvider) {
$compileProvider
.directive('localizeTitle', function (localizeFactory) {
return localizeFactory();
})
.directive('localizeSummary', function (localizeFactory) {
return localizeFactory();
});
});
inject(function ($compile, $rootScope) {
var element1 = $compile(
'<span localize-title="Bananas"></span>'
)($rootScope),
element2 = $compile(
'<table localize-summary="Apples"></table>'
)($rootScope);
expect(element1.attr('title')).toBe('Bananas');
expect(element2.attr('summary')).toBe('Äpfel');
});
});
it('Should create attribute based localize directives with multiple dashes', function () {
module(function ($compileProvider) {
$compileProvider
.directive('localizeDataContent', function (localizeFactory) {
return localizeFactory();
});
});
inject(function ($compile, $rootScope) {
var element = $compile(
'<span localize-data-content="Apples"></span>'
)($rootScope);
expect(element.attr('data-content')).toBe('Äpfel');
});
});
it('Should observe data-* attributes to update the translation result', function () {
module(function ($compileProvider) {
$compileProvider
.directive('localizeTitle', function (localizeFactory) {
return localizeFactory();
});
});
inject(function ($compile, $rootScope) {
var element = $compile(
'<span data-name="{{user.name}}" localize-title="Hello {name}!"></span>'
)($rootScope);
$rootScope.$digest();
expect(element.attr('title')).toBe('Hallo !');
$rootScope.user = {name: 'Bob'};
$rootScope.$digest();
expect(element.attr('title')).toBe('Hallo Bob!');
});
});
});
});
}());