-
Notifications
You must be signed in to change notification settings - Fork 66.9k
Expand file tree
/
Copy pathget-data.js
More file actions
194 lines (180 loc) · 5.35 KB
/
get-data.js
File metadata and controls
194 lines (180 loc) · 5.35 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import fs from 'fs'
import path from 'path'
import { expect, test, describe, beforeAll, afterAll } from '@jest/globals'
import languages from '../../lib/languages.js'
import { getDataByLanguage, getDeepDataByLanguage, getUIDataMerged } from '../../lib/get-data.js'
import { DataDirectory } from '../helpers/data-directory.js'
describe('get-data', () => {
let dd
const enDirBefore = languages.en.dir
const jaDirBefore = languages.ja.dir
beforeAll(() => {
const dd = new DataDirectory({
data: {
ui: {
key: 'Value',
deep: {
er: 'Depth',
est: 'Deepest',
},
},
variables: {
stuff: {
foo: 'Foo',
bar: 'Bar',
},
},
reusables: {
coolness: 'This is *Markdown*',
otherness: '**Also** Markdown',
},
},
})
languages.en.dir = dd.root
const jaTranslationsRoot = path.join(dd.root, 'translations', 'ja-JP')
fs.mkdirSync(jaTranslationsRoot, { recursive: true })
languages.ja.dir = jaTranslationsRoot
new DataDirectory( // eslint-disable-line no-new
{
data: {
ui: {
key: '価値',
deep: {
er: '深さ',
},
},
variables: {
stuff: {
foo: 'フー',
},
},
reusables: {
coolness: 'これがマークダウンです',
},
},
},
jaTranslationsRoot
)
})
afterAll(() => {
if (dd) dd.destroy()
languages.en.dir = enDirBefore
languages.ja.dir = jaDirBefore
})
test('getDataByLanguage variables English', () => {
// The most basic test
{
const result = getDataByLanguage('variables.stuff.foo', 'en')
expect(result).toBe('Foo')
}
// Test that memoization doesn't go wrong
{
const result = getDataByLanguage('variables.stuff.bar', 'en')
expect(result).toBe('Bar')
}
// Test that unrecognized keys just return `undefined`
{
const result = getDataByLanguage('variables.stuff.neverheardof', 'en')
expect(result).toBeUndefined()
}
})
test('getDataByLanguage variables with non-English', () => {
// The most basic test
{
const result = getDataByLanguage('variables.stuff.foo', 'ja')
expect(result).toBe('フー')
}
// Test fallback to English if not present in translation
{
const result = getDataByLanguage('variables.stuff.bar', 'ja')
expect(result).toBe('Bar')
}
// Test that unrecognized keys just return `undefined`
{
const result = getDataByLanguage('variables.stuff.neverheardof', 'ja')
expect(result).toBeUndefined()
}
})
test('getDataByLanguage variables failures', () => {
// The most basic test
{
const result = getDataByLanguage('variables.stuff.key_non_existent', 'en')
expect(result).toBeUndefined()
}
// Test fallback to English if not present in translation
{
const result = getDataByLanguage('variables.stuff.key_non_existent', 'ja')
expect(result).toBeUndefined()
}
// Returns undefined if not only the key is missing but the whole file too
{
const result = getDataByLanguage('variables.notpresent.whatever', 'en')
expect(result).toBeUndefined()
}
})
test('getDataByLanguage reusables English', () => {
// The most basic test
{
const result = getDataByLanguage('reusables.coolness', 'en')
expect(result).toBe('This is *Markdown*')
}
// Test that memoization doesn't go wrong
{
const result = getDataByLanguage('reusables.otherness', 'en')
expect(result).toBe('**Also** Markdown')
}
})
test('getDataByLanguage reusables non-English', () => {
// The most basic test
{
const result = getDataByLanguage('reusables.coolness', 'ja')
expect(result).toBe('これがマークダウンです')
}
// Test translations fall back to English if file doesn't exist
{
const result = getDataByLanguage('reusables.otherness', 'ja')
expect(result).toBe('**Also** Markdown')
}
})
test('getDataByLanguage failures', () => {
// The most basic test
{
const result = getDataByLanguage('reusables.neverheardof', 'en')
expect(result).toBeUndefined()
}
// Test translations will try English but fail if the fallback fails too
{
const result = getDataByLanguage('reusables.neverheardof', 'ja')
expect(result).toBeUndefined()
}
})
test('getUIDataMerged', () => {
// The most basic test
{
const result = getUIDataMerged('en')
expect(result.key).toBe('Value')
expect(result.deep.er).toBe('Depth')
}
// In a specific language
{
const result = getUIDataMerged('ja')
expect(result.key).toBe('価値')
expect(result.deep.er).toBe('深さ')
// Note how it falls back to English on that key
expect(result.deep.est).toBe('Deepest')
}
})
test('getDeepDataByLanguage', () => {
// The most basic test
{
const result = getDeepDataByLanguage('variables', 'en')
expect(result.stuff.foo).toBe('Foo')
expect(result.stuff.bar).toBe('Bar')
}
// All reusables
{
const result = getDeepDataByLanguage('reusables', 'en')
expect(result['coolness.md']).toBe('This is *Markdown*')
}
})
})