Skip to content

Commit

Permalink
add languages unit tests. make languageId lookup case insensitive.
Browse files Browse the repository at this point in the history
  • Loading branch information
PhotoNomad0 committed Jul 1, 2024
1 parent ecfb56e commit 3446059
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
79 changes: 79 additions & 0 deletions src/common/__tests__/languages.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* eslint-env jest */
import * as Languages from '../languages';

describe('Test Languages',()=>{
const minimumLangCount = 9000;

test('getLanguage() should work with mixed and lower case', () => {
const codes = ['sr-latn', 'sr-Latn', 'SR-LATN', 'ur-deva', 'ur-Deva', 'UR-DEVA', 'ZH'];

for (let languageId of codes) {
const languageData = Languages.getLanguage({languageId});
expect(languageData).toBeTruthy();
expect(languageData.languageId.toLowerCase()).toEqual(languageId.toLowerCase());
}
});

test('getLanguages() should work', () => {
const languages = Languages.getLanguages();
const langCount = languages.length;
expect(langCount).toBeLessThan(minimumLangCount * 1.2);

// make sure fields are valid and in sequence
for (let i = 1; i < langCount; i++) {
const lang = languages[i - 1];
expect(lang.languageId.length).toBeGreaterThan(0);
const languageName = lang.languageName || lang.localized
if (!languageName.length) {
console.log(`invalid languageName ${languageName} for languageId ${lang.languageId}`)
}
expect(lang.localized.length).toBeGreaterThan(0);
if (!lang.localized.length) {
console.log(`invalid localized ${lang.localized} for languageId ${lang.languageId}`)
}
expect(lang.localized.length).toBeGreaterThan(0);
const directionValid = lang.direction === 'ltr' || lang.direction === 'rtl'
if (!lang.localized.length) {
console.log(`invalid direction ${lang.direction} for languageId ${lang.languageId}`)
}
expect(directionValid).toBeTruthy();
}
});

test('getLanguages() verify no dupes among language codes.', () => {
let languages = Languages.getLanguages();
let localLanguageCodes = languages.map(language => language.languageId);

const sorted = localLanguageCodes.sort();
let dupsFound = 0;

for ( let idx = 1; idx < sorted.length; idx++ ) {
if ( sorted[idx] == sorted[idx-1]) {
dupsFound++;
}
}

expect(dupsFound).toBeLessThan(1);
expect(sorted.length).toBeGreaterThan(2000);
});

describe('getLanguage()',()=>{
test('Nepali ne should succeed', () => {
const code = 'ne';
const name = 'Nepali';
let foundLanguage = Languages.getLanguage({ languageId: code });

expect(foundLanguage.languageName).toEqual(name);
expect(foundLanguage.languageId).toEqual(code);
});

test('Nepali npi should succeed', () => {
const code = 'npi';
const name = 'Nepali';
let foundLanguage = Languages.getLanguage({ languageId: code });

expect(foundLanguage.languageName).toEqual(name);
expect(foundLanguage.languageId).toEqual(code);
});
});
});
5 changes: 4 additions & 1 deletion src/common/languages.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import languagesJSON from './languages.json'

export const getLanguage = ({ languageId }) => {
let _language
const language = languagesJSON.filter(object => object.lc === languageId)[0]
languageId = languageId.toLowerCase()
const language = languagesJSON.find(object => {
return object.lc?.toLowerCase() === languageId // compare lower case
})
_language = formatLanguage({ language })
return _language
}
Expand Down

0 comments on commit 3446059

Please sign in to comment.