-
Notifications
You must be signed in to change notification settings - Fork 31
/
spec.js
282 lines (224 loc) · 9.96 KB
/
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
var assert = require('assert');
var React = require('react');
var ReactDOM = require('react-dom/server');
var createReactClass = require('create-react-class');
var counterpart = require('counterpart');
var TranslateClass = require('./');
var Translate = React.createFactory(TranslateClass);
var render = ReactDOM.renderToStaticMarkup;
counterpart.registerTranslations('en', {
test: {
greeting: 'Hello, %(name)s!',
greeting_html: 'Hello, <b>%(name)s</b>!',
tooltip: 'Hey there, %(name)s!',
foo: 'bar %(x)s baz'
},
search_input: {
placeholder: 'Search...',
tooltip: 'Enter a search term'
},
submit_button: {
label: 'Submit Form',
tooltip: 'Click me!'
}
});
counterpart.registerTranslations('de', {
test: {
greeting: 'Hallo %(name)s!',
greeting_html: 'Hallo <b>%(name)s</b>!',
tooltip: 'Wie geht\'s, %(name)s?',
foo: 'bar %(x)s baz'
},
search_input: {
placeholder: 'Suchen...',
tooltip: 'Suchbegriff eingeben'
},
submit_button: {
label: 'Formular absenden',
tooltip: 'Klick mich!'
}
});
// hack: raise React console warnings as failed assertions
console.error = function(message) {
assert(false, message);
};
describe('The Translate component', function() {
it('transfers props', function() {
var props = { component: 'h1', className: 'foo', content: 'bar' };
var markup = render(Translate(props));
assert.matches(/^<h1 [^>]*?class="foo"/, markup);
});
it('translates stuff properly', function() {
counterpart.withLocale('en', function() {
assert.matches(/Hello, Martin/, render(Translate({ with: { name: 'Martin' }, content: 'test.greeting' })));
assert.matches(/Hello, Martin/, render(Translate({ with: { name: 'Martin' }, content: ['test', 'greeting'] })));
assert.matches(/Hello, <b>Martin<\/b>!/, render(Translate({ with: { name: 'Martin' }, unsafe: true, content: 'test.greeting_html' })));
assert.matches(/Hello, <b>Martin<\/b>!/, render(Translate({ with: { name: React.createElement('b', null, 'Martin') }, content: 'test.greeting' })));
var propsWithScope = { with: { name: 'Martin' }, scope: ['test'], content: 'greeting' };
assert.matches(/Hello, Martin/, render(Translate(propsWithScope)));
assert.doesNotMatch(/\sscope="test"/, render(Translate(propsWithScope)));
});
});
it('translates stuff properly using the deprecated way of providing interpolations', function() {
counterpart.withLocale('en', function() {
assert.matches(/Hello, Martin/, render(Translate({ name: 'Martin', content: 'test.greeting' })));
assert.matches(/Hello, Martin/, render(Translate({ name: 'Martin', content: ['test', 'greeting'] })));
assert.matches(/Hello, <b>Martin<\/b>!/, render(Translate({ name: 'Martin', unsafe: true, content: 'test.greeting_html' })));
assert.matches(/Hello, <b>Martin<\/b>!/, render(Translate({ name: React.createElement('b', null, 'Martin'), content: 'test.greeting' })));
var propsWithScope = { name: 'Martin', scope: ['test'], content: 'greeting' };
assert.matches(/Hello, Martin/, render(Translate(propsWithScope)));
assert.doesNotMatch(/\sscope="test"/, render(Translate(propsWithScope)));
});
});
it('supports the translation of HTML element attributes', function() {
counterpart.withLocale('en', function() {
var props = { component: 'input', type: 'search', name: 'q', scope: 'search_input', attributes: { placeholder: 'placeholder', title: 'tooltip' } };
var markup = render(Translate(props));
assert.matches(/^<input [^>]+>$/, markup);
assert.matches(/\stype="search"/, markup);
assert.matches(/\sname="q"/, markup);
assert.matches(/\splaceholder="Search..."/, markup);
assert.matches(/\stitle="Enter a search term"/, markup);
assert.doesNotMatch(/\sscope=/, markup);
});
counterpart.withLocale('de', function() {
var props = { component: 'button', type: 'submit', content: 'submit_button.label', attributes: { title: 'submit_button.tooltip' } };
var markup = render(Translate(props));
assert.matches(/^<button [^>]+>Formular absenden<\/button>$/, markup);
assert.matches(/\stype="submit"/, markup);
assert.matches(/\stitle="Klick mich!"/, markup);
});
counterpart.withLocale('en', function() {
var props = { with: { x: 'foo' }, attributes: { title: 'test.foo' } };
var markup = render(Translate(props));
assert.matches(/\stitle="bar foo baz"/, markup);
});
});
it('does not translate its children (since v0.7 the content attribute is used to translate the, well... content)', function() {
counterpart.withLocale('de', function() {
var props = { component: 'button', type: 'submit', attributes: { title: 'submit_button.tooltip' } };
var markup = render(Translate(props, React.createElement('span', null, 'Do it!')));
assert.matches(/^<button [^>]+><span[^>]*>Do it!<\/span><\/button>$/, markup);
assert.matches(/\stitle="Klick mich!"/, markup);
});
});
it('respects Counterpart\'s current locale', function() {
counterpart.withLocale('de', function() {
assert.matches(/Hallo/, render(Translate({ with: { name: 'Martin' }, content: 'test.greeting' })));
assert.doesNotMatch(/^missing translation:/, render(Translate({ with: { name: 'Martin' } }, 'test.greeting')));
});
});
it('respects Counterpart\'s current scope', function() {
counterpart.withScope('test', function() {
assert.matches(/Hello/, render(Translate({ with: { name: 'Martin' }, content: 'greeting' })));
assert.doesNotMatch(/^missing translation:/, render(Translate({ with: { name: 'Martin' }, content: 'greeting' })));
});
});
it('utilizes the fallback prop if translation is missing', function() {
assert.matches(/foo/, render(Translate({ content: 'mis.sing', fallback: 'foo' })));
});
describe('with the `component` prop set to a "text-only" React component', function() {
it('does not render HTML markup inside that component', function() {
// TODO add special treatment for <textarea>
['option', 'title'].forEach(function(tagName) {
var props = { component: tagName, with: { name: 'Martin' }, content: 'test.greeting' };
var markup = render(Translate(props));
assert.matches(new RegExp('^<' + tagName + '[^>]*>[^<>]*<\/' + tagName + '>$'), markup);
});
});
});
describe('with the `locale` prop explicitly set', function() {
it('disrespects the "global" locale', function() {
var props = { locale: 'de', with: { name: 'Martin' }, component: 'title', content: 'test.greeting' };
var markup = render(Translate(props));
counterpart.withLocale('en', function() {
assert.matches(/Hallo Martin!/, markup);
});
});
});
it('provides a counterpart-inspired convenience method for building components', function() {
var _t = TranslateClass.translate;
var component = _t('greeting', {
scope: 'test', with: { name: 'Martin' }, unsafe: true,
attributes: { title: 'tooltip' }
});
assert(React.isValidElement(component));
counterpart.withLocale('de', function() {
var markup = render(component);
assert.matches(/^<span\s.*?title="Wie geht's, Martin\?"[^>]*>Hallo Martin!<\/span>$/, markup);
assert.doesNotMatch(/\sscope="test"/, markup);
});
assert.matches(/^<span[^>]*>Click me!<\/span>$/, render(_t('submit_button.tooltip')));
});
it('allows a translator to be passed via React\'s context', function() {
var Wrapper = createReactClass({
childContextTypes: {
translator: TranslateClass.translatorType
},
getChildContext: function() {
return {
translator: this.props.translator
};
},
render: function() {
return Translate({ content: 'foo', attributes: { title: 'bar' } });
}
});
Wrapper = React.createFactory(Wrapper);
var markup;
var translator = new counterpart.Instance();
translator.registerTranslations('de', { foo: 'FOO-DE', bar: 'BAR-DE' });
translator.registerTranslations('en', { foo: 'FOO-EN', bar: 'BAR-EN' });
translator.setLocale('en');
markup = render(Wrapper({ translator: translator }));
assert.matches(/FOO-EN/, markup);
assert.matches(/BAR-EN/, markup);
translator.setLocale('de');
markup = render(Wrapper({ translator: translator }));
assert.matches(/FOO-DE/, markup);
assert.matches(/BAR-DE/, markup);
});
it('provides a withTranslations decorator', function() {
function test(Component) {
var markup;
var Component = React.createFactory(Component);
markup = render(Component({ locale: 'en' }));
assert.matches(/FOO-EN/, markup);
markup = render(Component({ locale: 'de' }));
assert.matches(/FOO-DE/, markup);
}
var withTranslations = TranslateClass.withTranslations;
var Component = createReactClass({
render: function() {
return Translate({ content: 'foo', locale: this.props.locale });
}
});
test(withTranslations({ de: { foo: 'FOO-DE' }, en: { foo: 'FOO-EN' } })(Component));
test(withTranslations(Component, { de: { foo: 'FOO-DE' }, en: { foo: 'FOO-EN' } }));
});
it('can register and de-register an onLocaleChange listener', function() {
var setLocale = TranslateClass.setLocale;
function onLocaleChangeListener(expectedLocale, newLocale) {
assert.equal(newLocale, expectedLocale);
}
var cb = onLocaleChangeListener.bind(null, 'de');
TranslateClass.onLocaleChange(cb);
setLocale('de');
TranslateClass.offLocaleChange(cb);
cb = onLocaleChangeListener.bind(null, 'en');
TranslateClass.onLocaleChange(cb);
setLocale('en');
TranslateClass.offLocaleChange(cb);
});
});
// spec helpers
assert.matches = function(regexp, value, message) {
if (!regexp.test(value)) {
assert.fail(value, regexp, message, '=~');
}
};
assert.doesNotMatch = function(regexp, value, message) {
if (regexp.test(value)) {
assert.fail(value, regexp, message, '!~');
}
};