element-web/test/i18n-test/languageHandler-test.js

79 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-11-13 20:20:41 +01:00
const React = require('react');
const expect = require('expect');
import * as languageHandler from '../../src/languageHandler';
const testUtils = require('../test-utils');
describe('languageHandler', function() {
beforeEach(function(done) {
2019-12-16 12:12:48 +01:00
testUtils.stubClient();
2017-11-13 20:20:41 +01:00
2019-11-18 11:03:05 +01:00
languageHandler.setLanguage('en').then(done);
2019-12-17 12:47:01 +01:00
languageHandler.setMissingEntryGenerator(key => key.split("|", 2)[1]);
2017-11-13 20:20:41 +01:00
});
2020-09-15 14:19:47 +02:00
it('translates a string to german', function(done) {
2017-11-13 20:20:41 +01:00
languageHandler.setLanguage('de').then(function() {
const translated = languageHandler._t('Rooms');
expect(translated).toBe('Räume');
2020-09-15 14:19:47 +02:00
}).then(done);
2017-11-13 20:20:41 +01:00
});
it('handles plurals', function() {
2017-11-13 21:10:08 +01:00
const text = 'and %(count)s others...';
2017-11-13 20:20:41 +01:00
expect(languageHandler._t(text, { count: 1 })).toBe('and one other...');
expect(languageHandler._t(text, { count: 2 })).toBe('and 2 others...');
});
it('handles simple variable subsitutions', function() {
2017-11-13 21:10:08 +01:00
const text = 'You are now ignoring %(userId)s';
2017-11-13 20:20:41 +01:00
expect(languageHandler._t(text, { userId: 'foo' })).toBe('You are now ignoring foo');
});
it('handles simple tag substitution', function() {
2017-11-13 21:10:08 +01:00
const text = 'Press <StartChatButton> to start a chat with someone';
expect(languageHandler._t(text, {}, { 'StartChatButton': () => 'foo' }))
.toBe('Press foo to start a chat with someone');
2017-11-13 20:20:41 +01:00
});
it('handles text in tags', function() {
2017-11-13 21:10:08 +01:00
const text = '<a>Click here</a> to join the discussion!';
expect(languageHandler._t(text, {}, { 'a': (sub) => `x${sub}x` }))
.toBe('xClick herex to join the discussion!');
2017-11-13 20:20:41 +01:00
});
it('variable substitution with React component', function() {
2017-11-14 19:34:47 +01:00
const text = 'You are now ignoring %(userId)s';
expect(languageHandler._t(text, { userId: () => <i>foo</i> }))
.toEqual((<span>You are now ignoring <i>foo</i></span>));
2017-11-13 20:20:41 +01:00
});
2017-11-14 19:55:47 +01:00
it('variable substitution with plain React component', function() {
const text = 'You are now ignoring %(userId)s';
expect(languageHandler._t(text, { userId: <i>foo</i> }))
.toEqual((<span>You are now ignoring <i>foo</i></span>));
});
2017-11-13 20:20:41 +01:00
it('tag substitution with React component', function() {
2017-11-13 21:10:08 +01:00
const text = 'Press <StartChatButton> to start a chat with someone';
2017-11-14 19:34:47 +01:00
expect(languageHandler._t(text, {}, { 'StartChatButton': () => <i>foo</i> }))
.toEqual(<span>Press <i>foo</i> to start a chat with someone</span>);
2017-11-13 20:20:41 +01:00
});
it('replacements in the wrong order', function() {
const text = '%(var1)s %(var2)s';
expect(languageHandler._t(text, { var2: 'val2', var1: 'val1' })).toBe('val1 val2');
});
it('multiple replacements of the same variable', function() {
const text = '%(var1)s %(var1)s';
expect(languageHandler.substitute(text, { var1: 'val1' })).toBe('val1 val1');
});
it('multiple replacements of the same tag', function() {
const text = '<a>Click here</a> to join the discussion! <a>or here</a>';
expect(languageHandler.substitute(text, {}, { 'a': (sub) => `x${sub}x` }))
.toBe('xClick herex to join the discussion! xor herex');
});
2017-11-13 20:20:41 +01:00
});