Skip to content

Commit c4e6cf4

Browse files
authored
Merge pull request #4 from derzunov/develop
Develop - tests added
2 parents b29cd33 + 60bfaec commit c4e6cf4

File tree

7 files changed

+1578
-55
lines changed

7 files changed

+1578
-55
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# editorconfig.org
2+
root = true
3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
[package.json]
11+
indent_size = 2

package.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
{
22
"name": "redux-react-i18n",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "i18n solution for react/redux",
55
"main": "lib/index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\"",
7+
"test": "jest",
88
"compile": "babel src -d lib",
99
"prepublish": "npm run compile",
1010
"build": "npm run compile"
1111
},
1212
"devDependencies": {
1313
"babel-cli": "^6.18.0",
1414
"babel-core": "^6.18.0",
15-
"babel-preset-react": "^6.16.0",
1615
"babel-preset-latest": "^6.22.0",
17-
"babel-preset-stage-0": "^6.22.0"
16+
"babel-preset-react": "^6.16.0",
17+
"babel-preset-stage-0": "^6.22.0",
18+
"react-addons-test-utils": "^15.4.2",
19+
"react-dom": "^15.4.2"
1820
},
1921
"repository": {
2022
"type": "git",
@@ -35,9 +37,14 @@
3537
},
3638
"homepage": "https://github.com/derzunov/redux-react-i18n#readme",
3739
"dependencies": {
40+
"enzyme": "^2.7.1",
41+
"jest": "^19.0.2",
3842
"react": "^15.3.2",
3943
"react-redux": "^4.4.5",
4044
"redux": "^3.6.0",
4145
"translatr": "^0.1.6"
46+
},
47+
"jest": {
48+
"rootDir": "./tests"
4249
}
4350
}

src/initialState.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const initialState = {
2+
currentLanguage: 'ru-RU',
3+
languages: [
4+
{
5+
code: 'ru-RU',
6+
name: 'Русский'
7+
},
8+
{
9+
code: 'en-US',
10+
name: 'English (USA)'
11+
}
12+
],
13+
dictionaries: {
14+
'ru-RU': {
15+
'key_1': 'Первый дефолтный ключ',
16+
'key_2': [ '$Count', ' ', [ 'штучка', 'штучки', 'штучек' ] ], // 1 штучка, 3 штучки, пять штучек
17+
},
18+
'en-US': {
19+
'key_1': 'First default key',
20+
'key_2': [ '$Count', ' ', [ 'thing', 'things' ] ], // 1 thing, 2 things, 153 things
21+
}
22+
}
23+
}
24+
25+
export default initialState

src/reducer.js

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import initialState from './initialState'
2+
13
const i18n = ( state, action ) => {
24

35
switch ( action.type ) {
@@ -37,30 +39,8 @@ const i18n = ( state, action ) => {
3739
}
3840

3941
default:
40-
return state || {
41-
currentLanguage: 'ru-RU',
42-
languages: [
43-
{
44-
code: 'ru-RU',
45-
name: 'Русский'
46-
},
47-
{
48-
code: 'en-US',
49-
name: 'English (USA)'
50-
}
51-
],
52-
dictionaries: {
53-
'ru-RU': {
54-
'key_1': 'Первый дефолтный ключ',
55-
'key_2': [ '$Count', ' ', [ 'штучка','штучки','штучек' ] ], // 1 штучка, 3 штучки, пять штучек
56-
},
57-
'en-US': {
58-
'key_1': 'First default key',
59-
'key_2': [ '$Count', ' ', [ 'thing','things' ] ], // 1 thing, 2 things, 153 things
60-
}
61-
}
62-
}
42+
return state || initialState
6343
}
6444
}
6545

66-
export default i18n
46+
export default i18n

tests/LocPresentational.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import React from 'react'
2+
import { shallow } from 'enzyme'
3+
import LocPresentational from '../src/LocPresentational'
4+
5+
describe('<LocPresentational/> ru-RU', () => {
6+
7+
it('should render <LocPresentational/> component with correct ru-RU with 1st plural form', () => {
8+
let props = {
9+
currentLanguage: 'ru-RU',
10+
locKey: 'key_4',
11+
dictionary: {
12+
key_4: [ '$Count', ' ', [ 'тестик', 'тестика', 'тестиков' ] ]
13+
}
14+
}
15+
const wrapper = shallow( <LocPresentational number="1" { ...props }/> )
16+
expect( wrapper.find( 'span' ).text()).toEqual( '1 тестик' )
17+
})
18+
19+
it('should render <LocPresentational/> component with correct ru-RU with 2nd plural form', () => {
20+
let props = {
21+
currentLanguage: 'ru-RU',
22+
locKey: 'key_4',
23+
dictionary: {
24+
key_4: [ '$Count', ' ', [ 'тестик', 'тестика', 'тестиков' ] ]
25+
}
26+
}
27+
const wrapper = shallow( <LocPresentational number="42" { ...props }/> )
28+
expect( wrapper.find( 'span' ).text()).toEqual( '42 тестика' )
29+
})
30+
31+
it('should render <LocPresentational/> component with correct ru-RU with 3rd plural form', () => {
32+
let props = {
33+
currentLanguage: 'ru-RU',
34+
locKey: 'key_4',
35+
dictionary: {
36+
key_4: [ '$Count', ' ', [ 'тестик', 'тестика', 'тестиков' ] ]
37+
}
38+
}
39+
const wrapper = shallow( <LocPresentational number="1937" { ...props }/> )
40+
expect( wrapper.find( 'span' ).text()).toEqual( '1937 тестиков' )
41+
})
42+
})

tests/reducer.test.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import reducer from '../src/reducer'
2+
import initialState from '../src/initialState'
3+
4+
describe('i18n reducer', () => {
5+
it( 'should return the initial state', () => {
6+
expect(
7+
reducer( undefined, {} )
8+
).toEqual( initialState )
9+
} )
10+
11+
it( 'should handle SET_CURRENT', () => {
12+
let newState = Object.assign( {}, initialState )
13+
newState.currentLanguage = 'en-US'
14+
expect(
15+
reducer( initialState, {
16+
type: 'SET_CURRENT',
17+
data: 'en-US'
18+
} )
19+
).toEqual(
20+
newState
21+
)
22+
} )
23+
24+
it( 'should handle SET_LANGUAGES', () => {
25+
26+
let newState = Object.assign( {}, initialState )
27+
newState.languages = [
28+
{
29+
code: 'car-CAR',
30+
name: 'Птичий'
31+
},
32+
{
33+
code: 'en-US',
34+
name: 'English (USA)'
35+
}
36+
]
37+
expect(
38+
reducer( initialState, {
39+
type: 'SET_LANGUAGES',
40+
data: [
41+
{
42+
code: 'car-CAR',
43+
name: 'Птичий'
44+
},
45+
{
46+
code: 'en-US',
47+
name: 'English (USA)'
48+
}
49+
]
50+
} )
51+
).toEqual(
52+
newState
53+
)
54+
} )
55+
56+
it( 'should handle SET_DICTIONARIES', () => {
57+
58+
let newState = Object.assign( {}, initialState )
59+
newState.dictionaries = {
60+
'car-CAR': {
61+
'key_1': 'Первый дефолтный ключ птичьего языка для теста',
62+
'key_2': [ '$Count', ' ', [ 'птичка', 'птички', 'птичек' ] ],
63+
},
64+
'en-US': {
65+
'key_1': 'First default key',
66+
'key_2': [ '$Count', ' ', [ 'thing', 'things' ] ],
67+
}
68+
}
69+
expect(
70+
reducer( initialState, {
71+
type: 'SET_DICTIONARIES',
72+
data: {
73+
'car-CAR': {
74+
'key_1': 'Первый дефолтный ключ птичьего языка для теста',
75+
'key_2': [ '$Count', ' ', [ 'птичка', 'птички', 'птичек' ] ],
76+
},
77+
'en-US': {
78+
'key_1': 'First default key',
79+
'key_2': [ '$Count', ' ', [ 'thing', 'things' ] ],
80+
}
81+
}
82+
} )
83+
).toEqual(
84+
newState
85+
)
86+
} )
87+
88+
it( 'should handle ADD_DICTIONARY', () => {
89+
90+
let newState = Object.assign( {}, initialState )
91+
newState.dictionaries[ 'new-NEW' ] = {
92+
'new_key_1': 'First new default key',
93+
'new_key_2': [ '$Count new', ' ', [ 'thing', 'things' ] ],
94+
}
95+
96+
expect(
97+
reducer( initialState, {
98+
type: 'ADD_DICTIONARY',
99+
data: {
100+
languageCode: 'new-NEW',
101+
dictionary: {
102+
'new_key_1': 'First new default key',
103+
'new_key_2': [ '$Count new', ' ', [ 'thing', 'things' ] ],
104+
}
105+
}
106+
} )
107+
).toEqual(
108+
newState
109+
)
110+
} )
111+
112+
})

0 commit comments

Comments
 (0)