From 672d5080adae87f5269a4fcf2407f513a43852a9 Mon Sep 17 00:00:00 2001 From: Stefan Parviainen Date: Mon, 13 Nov 2017 20:20:41 +0100 Subject: [PATCH] Add unit tests for translation --- test/i18n-test/languageHandler-test.js | 60 ++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 test/i18n-test/languageHandler-test.js diff --git a/test/i18n-test/languageHandler-test.js b/test/i18n-test/languageHandler-test.js new file mode 100644 index 0000000000..2a94768092 --- /dev/null +++ b/test/i18n-test/languageHandler-test.js @@ -0,0 +1,60 @@ +const React = require('react'); +const expect = require('expect'); +import * as languageHandler from '../../src/languageHandler'; + +const testUtils = require('../test-utils'); + +describe('languageHandler', function() { + let sandbox; + + beforeEach(function(done) { + testUtils.beforeEach(this); + sandbox = testUtils.stubClient(); + + languageHandler.setLanguage('en').done(done); + }); + + afterEach(function() { + sandbox.restore(); + }); + + it('translates a string to german', function() { + languageHandler.setLanguage('de').then(function() { + const translated = languageHandler._t('Rooms'); + expect(translated).toBe('Räume'); + }); + }); + + it('handles plurals', function() { + var text = 'and %(count)s others...'; + 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() { + var text = 'You are now ignoring %(userId)s'; + expect(languageHandler._t(text, { userId: 'foo' })).toBe('You are now ignoring foo'); + }); + + it('handles simple tag substitution', function() { + var text = 'Press to start a chat with someone'; + expect(languageHandler._t(text, {}, { 'StartChatButton': () => 'foo' })).toBe('Press foo to start a chat with someone'); + }); + + it('handles text in tags', function() { + var text = 'Click here to join the discussion!'; + expect(languageHandler._t(text, {}, { 'a': (sub) => `x${sub}x` })).toBe('xClick herex to join the discussion!'); + }); + + it('variable substitution with React component', function() { + // Need an extra space at the end because the result of _t() has an extra empty node at the end + var text = 'You are now ignoring %(userId)s '; + expect(JSON.stringify(languageHandler._t(text, { userId: () => foo }))).toBe(JSON.stringify((You are now ignoring foo ))); + }); + + it('tag substitution with React component', function() { + var text = 'Press to start a chat with someone'; + expect(JSON.stringify(languageHandler._t(text, {}, { 'StartChatButton': () => foo }))).toBe(JSON.stringify(Press foo to start a chat with someone)); + + }); +});