Skip to content

Commit 0896cab

Browse files
committed
test(locale): add translations tests
1 parent dda0bb5 commit 0896cab

File tree

5 files changed

+1185
-27
lines changed

5 files changed

+1185
-27
lines changed

.eslintrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"env": {
55
"browser": true,
66
"node": true,
7-
"es6": true
7+
"es6": true,
8+
"jest": true
89
},
910
"settings": {
1011
"react": {

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"dependencies": {},
1010
"devDependencies": {
1111
"@babel/cli": "^7.10.1",
12-
"@babel/register": "^7.10.1",
1312
"@babel/core": "^7.10.2",
1413
"@babel/plugin-proposal-class-properties": "^7.10.1",
1514
"@babel/plugin-proposal-decorators": "^7.10.1",
@@ -18,6 +17,7 @@
1817
"@babel/plugin-transform-react-jsx": "^7.10.1",
1918
"@babel/polyfill": "^7.10.1",
2019
"@babel/preset-env": "^7.10.2",
20+
"@babel/register": "^7.10.1",
2121
"auto-changelog": "^2.0.0",
2222
"babel-eslint": "^10.1.0",
2323
"babel-jest": "^26.0.1",
@@ -34,6 +34,7 @@
3434
"gh-release": "^3.4.0",
3535
"gotrue-js": "^0.9.21",
3636
"html-webpack-plugin": "^4.3.0",
37+
"jest": "^26.0.1",
3738
"mkdirp": "^0.5.1",
3839
"mobx": "^3.2.2",
3940
"mobx-preact": "^1.1.0",
@@ -79,7 +80,9 @@
7980
"react-demo": "cd example && yarn && yarn start",
8081
"release": "node ./script/release.js",
8182
"lint": "eslint src",
82-
"test": "run-s lint format-check",
83+
"test": "run-s lint format-check test:unit",
84+
"test:unit": "jest",
85+
"test:unit:watch": "jest --watch",
8386
"version": "run-s release changelog"
8487
}
8588
}

src/translations/index.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export const defaultLocale = "en";
55
const translations = { en, fr };
66

77
export const getTranslation = (key, locale = defaultLocale) => {
8-
return translations[locale]
9-
? translations[locale][key] || key
10-
: translations[defaultLocale][key] || key;
8+
const translated = translations[locale] && translations[locale][key];
9+
return translated || translations[defaultLocale][key] || key;
1110
};

src/translations/index.test.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
describe("translations", () => {
2+
beforeEach(() => {
3+
jest.resetModules();
4+
});
5+
6+
it("should return translation for default locale", () => {
7+
const { getTranslation } = require("./");
8+
expect(getTranslation("log_in")).toEqual("Log in");
9+
});
10+
11+
it("should return translation for 'en' locale", () => {
12+
const { getTranslation } = require("./");
13+
expect(getTranslation("log_in", "en")).toEqual("Log in");
14+
});
15+
16+
it("should return translation for 'fr' locale", () => {
17+
const { getTranslation } = require("./");
18+
expect(getTranslation("log_in", "fr")).toEqual("Connexion");
19+
});
20+
21+
it("should return key for non existing translation", () => {
22+
const { getTranslation } = require("./");
23+
expect(getTranslation("unknown_key")).toEqual("unknown_key");
24+
});
25+
26+
it("should default to 'en' on missing key", () => {
27+
jest.mock("./en.json", () => ({ log_in: "Log in" }));
28+
jest.mock("./fr.json", () => ({}));
29+
30+
const { getTranslation } = require("./");
31+
expect(getTranslation("log_in")).toEqual("Log in");
32+
expect(getTranslation("log_in", "fr")).toEqual("Log in");
33+
});
34+
});

0 commit comments

Comments
 (0)