Tests for language & reducer utils
This commit is contained in:
46
tests/utils/language.test.ts
Normal file
46
tests/utils/language.test.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import faker from "faker";
|
||||
import { replaceBoth, retrieveFiltered } from "../../utils/language";
|
||||
import { languages, exceptions, mappings } from "../../utils/languages.json";
|
||||
|
||||
describe("replaceBoth", () => {
|
||||
it("replaces excepted sources correctly", () => {
|
||||
Object.entries(exceptions.source).forEach(([code, replacement]) => {
|
||||
const { source } = replaceBoth("exception", { source: code, target: "" })
|
||||
expect(source).toBe(replacement);
|
||||
});
|
||||
});
|
||||
|
||||
it("replaces excepted targets correctly", () => {
|
||||
Object.entries(exceptions.target).forEach(([code, replacement]) => {
|
||||
const { target } = replaceBoth("exception", { source: "", target: code })
|
||||
expect(target).toBe(replacement);
|
||||
});
|
||||
});
|
||||
|
||||
it("replaces mapped sources correctly", () => {
|
||||
Object.entries(mappings.source).forEach(([code, replacement]) => {
|
||||
const { source } = replaceBoth("mapping", { source: code, target: "" })
|
||||
expect(source).toBe(replacement);
|
||||
});
|
||||
});
|
||||
|
||||
it("replaces mapped targets correctly", () => {
|
||||
Object.entries(mappings.target).forEach(([code, replacement]) => {
|
||||
const { target } = replaceBoth("mapping", { source: "", target: code })
|
||||
expect(target).toBe(replacement);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("retrieveFiltered", () => {
|
||||
it("filters by exceptions & by opposite values", () => {
|
||||
const source = faker.random.locale();
|
||||
const target = faker.random.locale();
|
||||
const sourceKeys = Object.keys(languages).filter(code => !Object.keys(exceptions.source).includes(code) && code !== target);
|
||||
const targetKeys = Object.keys(languages).filter(code => !Object.keys(exceptions.target).includes(code) && code !== source);
|
||||
|
||||
const { sourceLangs, targetLangs } = retrieveFiltered(source, target);
|
||||
expect(sourceLangs.map(([code]) => code)).toStrictEqual(sourceKeys);
|
||||
expect(targetLangs.map(([code]) => code)).toStrictEqual(targetKeys);
|
||||
});
|
||||
});
|
||||
53
tests/utils/reducer.test.ts
Normal file
53
tests/utils/reducer.test.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import faker from "faker";
|
||||
import langReducer, { Actions, initialState } from "../../utils/reducer";
|
||||
|
||||
it("changes a field value", () => {
|
||||
const query = faker.random.words();
|
||||
|
||||
const res = langReducer(initialState, {
|
||||
type: Actions.SET_FIELD,
|
||||
payload: {
|
||||
key: "query",
|
||||
value: query
|
||||
}
|
||||
});
|
||||
expect(res).toStrictEqual({ ...initialState, query });
|
||||
});
|
||||
|
||||
it("changes all fields", () => {
|
||||
const state = {
|
||||
source: faker.random.locale(),
|
||||
target: faker.random.locale(),
|
||||
query: faker.random.words()
|
||||
};
|
||||
|
||||
const res = langReducer(initialState, {
|
||||
type: Actions.SET_ALL,
|
||||
payload: { state }
|
||||
});
|
||||
expect(res).toStrictEqual(state);
|
||||
});
|
||||
|
||||
it("switches the languages", () => {
|
||||
const state = {
|
||||
...initialState,
|
||||
source: "es",
|
||||
target: "ca"
|
||||
};
|
||||
|
||||
const res = langReducer(state, { type: Actions.SWITCH_LANGS });
|
||||
expect(res.source).toStrictEqual(state.target);
|
||||
expect(res.target).toStrictEqual(state.source);
|
||||
});
|
||||
|
||||
it("resets the source while switching if they're the same", () => {
|
||||
const state = {
|
||||
...initialState,
|
||||
source: "eo",
|
||||
target: "eo"
|
||||
};
|
||||
|
||||
const res = langReducer(state, { type: Actions.SWITCH_LANGS });
|
||||
expect(res.source).toStrictEqual(initialState.source);
|
||||
expect(res.target).toStrictEqual(state.source);
|
||||
});
|
||||
@@ -7,7 +7,7 @@ const checkTypes = {
|
||||
|
||||
type CheckType = keyof typeof checkTypes;
|
||||
|
||||
export const langTypes = [
|
||||
const langTypes = [
|
||||
"source",
|
||||
"target"
|
||||
] as const;
|
||||
|
||||
Reference in New Issue
Block a user