From 2a19b42b15906cfbe9e3bb7a43bf14cac0c03625 Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sat, 25 Mar 2017 09:41:24 +0100 Subject: [PATCH] making I18n class testable, adding minimal test --- js/privatebin.js | 13 ++++++++++++- js/test.js | 24 +++++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/js/privatebin.js b/js/privatebin.js index 28e08e44..74b2211e 100644 --- a/js/privatebin.js +++ b/js/privatebin.js @@ -538,6 +538,18 @@ jQuery.PrivateBin = function($, sjcl, Base64, RawDeflate) { }); } + /** + * resets state, used for unit testing + * + * @name I18n.reset + * @function + */ + me.reset = function() + { + language = null; + translations = {}; + } + return me; })(window, document); @@ -810,7 +822,6 @@ jQuery.PrivateBin = function($, sjcl, Base64, RawDeflate) { $cipherData = $templates = id = symmetricKey = null; } - /** * init navigation manager * diff --git a/js/test.js b/js/test.js index 2d5a544c..3999158a 100644 --- a/js/test.js +++ b/js/test.js @@ -270,7 +270,8 @@ describe('Helper', function () { cookieArray = [], count = 0; labels.forEach(function(item, i) { - var key = item.replace(/[\s;,=]/g, 'x'), + // deliberatly using a non-ascii key for replacing invalid characters + var key = item.replace(/[\s;,=]/g, '£'), value = (values[i] || values[0]).replace(/[\s;,=]/g, ''); cookieArray.push(key + '=' + value); if (Math.random() < 1 / i) @@ -325,6 +326,27 @@ describe('Helper', function () { }); }); +describe('I18n', function () { + describe('translate', function () { + before(function () { + $.PrivateBin.I18n.reset(); + }); + + jsc.property( + 'returns message ID unchanged if no translation found', + 'string', + function (messageId) { + var result = $.PrivateBin.I18n.translate(messageId); + $.PrivateBin.I18n.reset(); + var alias = $.PrivateBin.I18n._(messageId); + $.PrivateBin.I18n.reset(); + return messageId === result && + messageId === alias; + } + ); + }); +}); + describe('Model', function () { describe('getPasteId', function () { before(function () {