Files
LingvAI/tests/utils/language.test.ts

69 lines
2.1 KiB
TypeScript
Raw Normal View History

import faker from "faker";
import { replaceBoth, retrieveFromType, getName, CheckType, LangType } from "@utils/language";
import { languages, exceptions, mappings } from "@utils/languages.json";
2021-03-12 19:41:20 +01:00
describe("replaceBoth", () => {
2021-03-13 19:37:44 +01:00
const testReplacer = (
checkType: CheckType,
checkObj: {
[key in LangType]: {
[key: string]: string
}
},
langType: LangType
) => (
Object.entries(checkObj[langType]).forEach(([code, replacement]) => {
const res = replaceBoth(checkType, { source: "auto", target: "en", [langType]: code })
2021-03-13 19:37:44 +01:00
expect(res[langType]).toBe(replacement);
})
);
2021-03-12 19:41:20 +01:00
it("replaces excepted sources correctly", () => {
2021-03-13 19:37:44 +01:00
testReplacer("exception", exceptions, "source");
2021-03-12 19:41:20 +01:00
});
it("replaces excepted targets correctly", () => {
2021-03-13 19:37:44 +01:00
testReplacer("exception", exceptions, "target");
2021-03-12 19:41:20 +01:00
});
it("replaces mapped sources correctly", () => {
2021-03-13 19:37:44 +01:00
testReplacer("mapping", mappings, "source");
2021-03-12 19:41:20 +01:00
});
it("replaces mapped targets correctly", () => {
2021-03-13 19:37:44 +01:00
testReplacer("mapping", mappings, "target");
2021-03-12 19:41:20 +01:00
});
});
describe("retrieveFromType", () => {
const checkExceptions = (langType: LangType) => (
retrieveFromType(langType).forEach(([code]) => !Object.keys(exceptions).includes(code))
2021-03-13 19:37:44 +01:00
);
it("returns full list on empty type", () => {
expect(retrieveFromType()).toStrictEqual(Object.entries(languages));
});
it("filters source exceptions", () => {
checkExceptions("source");
});
it("filters target exceptions", () => {
checkExceptions("target");
});
});
describe("getName", () => {
it("returns name from valid code", () => {
const langEntries = Object.entries(languages);
const randomEntry = faker.random.arrayElement(langEntries);
const [code, name] = randomEntry;
expect(getName(code)).toEqual(name);
});
it("returns null on wrong code", () => {
const randomCode = faker.random.words();
expect(getName(randomCode)).toBeNull();
2021-03-12 19:41:20 +01:00
});
});