From a78a4b13694ecfe902b697c41304366a1a6e0737 Mon Sep 17 00:00:00 2001 From: Muhammed Ali Yildiz Date: Mon, 14 Mar 2022 10:21:04 -0400 Subject: [PATCH 1/2] added tests for i18n --- src/__tests__/components/i18n.test.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/__tests__/components/i18n.test.js diff --git a/src/__tests__/components/i18n.test.js b/src/__tests__/components/i18n.test.js new file mode 100644 index 00000000..eaae0e83 --- /dev/null +++ b/src/__tests__/components/i18n.test.js @@ -0,0 +1,15 @@ +import i18n from '../../i18n' + +const translationEN = i18n.getDataByLanguage('en') +const translationDE = i18n.getDataByLanguage('de') + +test('check if the same namespaces exists in DE and EN', () => { + expect(Object.keys(translationEN)).toEqual(Object.keys(translationDE)) +}) + +test('check if the namespaces in DE and EN have the same values', () => { + const valuesEN = Object.keys(translationEN).map(key => Object.keys(i18n.getResourceBundle('en', key))) + const valuesDE = Object.keys(translationDE).map(key => Object.keys(i18n.getResourceBundle('de', key))) + + expect(valuesDE).toEqual(valuesEN) +}) From 84f4a14948e281bcc1d7a86b785e3d600c915c7b Mon Sep 17 00:00:00 2001 From: Muhammed Ali Yildiz Date: Thu, 24 Mar 2022 08:40:10 -0400 Subject: [PATCH 2/2] changed i18n test --- src/__tests__/components/i18n.test.js | 34 +++++++++++++++++++++------ 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/__tests__/components/i18n.test.js b/src/__tests__/components/i18n.test.js index eaae0e83..c25e26e5 100644 --- a/src/__tests__/components/i18n.test.js +++ b/src/__tests__/components/i18n.test.js @@ -3,13 +3,33 @@ import i18n from '../../i18n' const translationEN = i18n.getDataByLanguage('en') const translationDE = i18n.getDataByLanguage('de') -test('check if the same namespaces exists in DE and EN', () => { - expect(Object.keys(translationEN)).toEqual(Object.keys(translationDE)) -}) +test('recursively iterate all object keys of i18 EN and DE, checks if the same namespaces exists, if namespaces have the same length', () => { + const isPlainObj = obj => Object.prototype.toString.call(obj) === '[object Object]' + const equal = (a, b) => { + const keysa = Object.keys(a).sort() + const keysb = Object.keys(b).sort() + + if (keysa.length !== keysb.length) { + return false + } + + if (keysa.toString() !== keysb.toString()) { + return false + } + + return keysa.every(key => { + const valuea = a[key] + const valueb = b[key] + const typea = typeof valuea + const typeb = typeof valueb + + if (typea !== typeb) { return false } + if (typea === 'string') { return true } + if (!isPlainObj(valuea) || !isPlainObj(valueb)) { return false } -test('check if the namespaces in DE and EN have the same values', () => { - const valuesEN = Object.keys(translationEN).map(key => Object.keys(i18n.getResourceBundle('en', key))) - const valuesDE = Object.keys(translationDE).map(key => Object.keys(i18n.getResourceBundle('de', key))) + return equal(valuea, valueb) + }) + } - expect(valuesDE).toEqual(valuesEN) + expect(equal(translationEN, translationDE)).toBe(true) })