-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathtransCore.test.js
More file actions
173 lines (147 loc) · 4.22 KB
/
transCore.test.js
File metadata and controls
173 lines (147 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import transCore from '../src/transCore'
const nsNestedKeys = {
key_1: {
key_1_nested: 'message 1 nested',
key_2_nested: 'message 2 nested',
},
key_2: 'message 2',
}
const nsRootKeys = {
root_key_1: 'root message 1',
root_key_2: 'root message 2',
}
const nsInterpolate = {
key_1: {
key_1_nested: 'message 1 {{count}}',
key_2_nested: 'message 2 {{count}}',
},
key_2: 'message 2',
}
const nsWithEmpty = {
emptyKey: '',
}
const nsConflictWithKeySeparatorKey = {
'key likes sentence.': 'message 1 conflict',
}
describe('transCore', () => {
test('should return an object of root keys', async () => {
const t = transCore({
config: {},
allNamespaces: { nsRootKeys },
lang: 'en',
})
expect(typeof t).toBe('function')
expect(t('nsRootKeys:.', null, { returnObjects: true })).toEqual(nsRootKeys)
})
test('should return an object of root keys for the defaultNs', async () => {
const t = transCore({
config: {
defaultNS: 'nsRootKeys',
},
allNamespaces: { nsRootKeys },
lang: 'en',
})
expect(typeof t).toBe('function')
expect(t('.', null, { returnObjects: true })).toEqual(nsRootKeys)
})
test('should return an object of root keys when using the keySeparator', async () => {
const keySeparator = '___'
const t = transCore({
config: {
keySeparator,
},
allNamespaces: { nsRootKeys },
lang: 'en',
})
expect(typeof t).toBe('function')
expect(
t(`nsRootKeys:${keySeparator}`, null, { returnObjects: true })
).toEqual(nsRootKeys)
})
test('should allow flat keys when keySeparator=false', async () => {
const t = transCore({
config: {
keySeparator: false,
},
allNamespaces: { common: { 'example.flat.key': 'works' } },
lang: 'en',
})
expect(t('common:example.flat.key')).toEqual('works')
})
test('should return an object of nested keys', async () => {
const t = transCore({
config: {},
allNamespaces: { nsObject: nsNestedKeys },
lang: 'en',
})
expect(typeof t).toBe('function')
expect(t('nsObject:key_1', null, { returnObjects: true })).toEqual(
nsNestedKeys.key_1
)
expect(t('nsObject:key_2', null, { returnObjects: true })).toEqual(
nsNestedKeys.key_2
)
})
test('should return an object of nested keys and interpolate correctly', async () => {
const t = transCore({
config: {},
allNamespaces: { nsInterpolate },
pluralRules: { select() {}, resolvedOptions() {} },
lang: 'en',
})
const count = 999
const expected = {
key_1: {
key_1_nested: `message 1 ${count}`,
key_2_nested: `message 2 ${count}`,
},
key_2: 'message 2',
}
expect(typeof t).toBe('function')
expect(t('nsInterpolate:.', { count }, { returnObjects: true })).toEqual(
expected
)
})
test('should return empty string when allowEmptyStrings is passed as true.', () => {
const t = transCore({
config: {
allowEmptyStrings: true,
},
allNamespaces: { nsWithEmpty },
lang: 'en',
})
expect(typeof t).toBe('function')
expect(t('nsWithEmpty:emptyKey')).toEqual('')
})
test('should return empty string when allowEmptyStrings is omitted.', () => {
const t = transCore({
allNamespaces: { nsWithEmpty },
config: {},
lang: 'en',
})
expect(typeof t).toBe('function')
expect(t('nsWithEmpty:emptyKey')).toEqual('')
})
test('should return the key name when allowEmptyStrings is omit passed as false.', () => {
const t = transCore({
config: {
allowEmptyStrings: false,
},
allNamespaces: { nsWithEmpty },
lang: 'en',
})
expect(typeof t).toBe('function')
expect(t('nsWithEmpty:emptyKey')).toEqual('nsWithEmpty:emptyKey')
})
test('should return key string when key conficts with separator and log warning.', () => {
const t = transCore({
config: {},
allNamespaces: { nsConflictWithKeySeparatorKey },
lang: 'en',
})
expect(typeof t).toBe('function')
expect(t('nsConflictWithKeySeparatorKey:key likes sentence.')).toEqual(
'nsConflictWithKeySeparatorKey:key likes sentence.'
)
})
})