File tree 5 files changed +1185
-27
lines changed
5 files changed +1185
-27
lines changed Original file line number Diff line number Diff line change 4
4
"env" : {
5
5
"browser" : true ,
6
6
"node" : true ,
7
- "es6" : true
7
+ "es6" : true ,
8
+ "jest" : true
8
9
},
9
10
"settings" : {
10
11
"react" : {
Original file line number Diff line number Diff line change 9
9
"dependencies" : {},
10
10
"devDependencies" : {
11
11
"@babel/cli" : " ^7.10.1" ,
12
- "@babel/register" : " ^7.10.1" ,
13
12
"@babel/core" : " ^7.10.2" ,
14
13
"@babel/plugin-proposal-class-properties" : " ^7.10.1" ,
15
14
"@babel/plugin-proposal-decorators" : " ^7.10.1" ,
18
17
"@babel/plugin-transform-react-jsx" : " ^7.10.1" ,
19
18
"@babel/polyfill" : " ^7.10.1" ,
20
19
"@babel/preset-env" : " ^7.10.2" ,
20
+ "@babel/register" : " ^7.10.1" ,
21
21
"auto-changelog" : " ^2.0.0" ,
22
22
"babel-eslint" : " ^10.1.0" ,
23
23
"babel-jest" : " ^26.0.1" ,
34
34
"gh-release" : " ^3.4.0" ,
35
35
"gotrue-js" : " ^0.9.21" ,
36
36
"html-webpack-plugin" : " ^4.3.0" ,
37
+ "jest" : " ^26.0.1" ,
37
38
"mkdirp" : " ^0.5.1" ,
38
39
"mobx" : " ^3.2.2" ,
39
40
"mobx-preact" : " ^1.1.0" ,
79
80
"react-demo" : " cd example && yarn && yarn start" ,
80
81
"release" : " node ./script/release.js" ,
81
82
"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" ,
83
86
"version" : " run-s release changelog"
84
87
}
85
88
}
Original file line number Diff line number Diff line change @@ -5,7 +5,6 @@ export const defaultLocale = "en";
5
5
const translations = { en, fr } ;
6
6
7
7
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 ;
11
10
} ;
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments