forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-data.js
294 lines (271 loc) · 7.87 KB
/
get-data.js
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
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(() => {
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(() => {
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*')
}
})
})
const VALID_ENGLISH_MARKDOWN = `
---
front: matter
---
*English* /Markdown/
`.trim()
const VALID_JAPANESE_MARKDOWN = `
---
front:matter
---
*Japanese* /Markdown/
`.trim()
const INVALID_JAPANESE_MARKDOWN = `
---
front: >'matter
---
*Japanese* /Markdown/
`.trim()
describe('get-data on corrupt translations', () => {
let dd
const enDirBefore = languages.en.dir
const jaDirBefore = languages.ja.dir
beforeAll(() => {
dd = new DataDirectory({
data: {
variables: {
everything: {
is: 'Awesome',
},
},
reusables: {
cool: VALID_ENGLISH_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: {
variables: {
everything: {
is: 'just you wait',
},
},
reusables: {
cool: VALID_JAPANESE_MARKDOWN,
},
},
},
jaTranslationsRoot
)
const ymlFile = path.join(jaTranslationsRoot, 'data', 'variables', 'everything.yml')
console.assert(fs.existsSync(ymlFile), `${ymlFile} wasn't created`)
fs.writeFileSync(ymlFile, `>:This is not valid Yaml:>`, 'utf-8')
const mdFile = path.join(jaTranslationsRoot, 'data', 'reusables', 'cool.md')
console.assert(fs.existsSync(mdFile), `${mdFile} wasn't created`)
fs.writeFileSync(mdFile, INVALID_JAPANESE_MARKDOWN, 'utf-8')
})
afterAll(() => {
dd.destroy()
languages.en.dir = enDirBefore
languages.ja.dir = jaDirBefore
})
test('getDataByLanguage on a corrupt .yml file', () => {
// First make sure it works in English
{
const result = getDataByLanguage('variables.everything.is', 'en')
expect(result).toBe('Awesome')
}
// Japanese translations would fall back due to a corrupt Yaml file
{
const result = getDataByLanguage('variables.everything.is', 'ja')
expect(result).toBe('Awesome')
}
})
test('getDataByLanguage on a corrupt .md file', () => {
// First make sure it works in English
{
const result = getDataByLanguage('reusables.cool', 'en')
expect(result).toBe('*English* /Markdown/')
}
// Japanese translations would fall back due to a corrupt Yaml file
{
const result = getDataByLanguage('reusables.cool', 'ja')
expect(result).toBe('*English* /Markdown/')
}
})
})