forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite-data.js
119 lines (104 loc) · 4.17 KB
/
site-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
import { fileURLToPath } from 'url'
import path from 'path'
import fs from 'fs'
import { get, isPlainObject, has } from 'lodash-es'
import flat from 'flat'
import loadSiteData from '../../lib/site-data.js'
import patterns from '../../lib/patterns.js'
import { liquid } from '../../lib/render-content/index.js'
import walkSync from 'walk-sync'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
describe('siteData module (English)', () => {
let data
beforeAll(async () => {
data = await loadSiteData()
})
test('makes an object', async () => {
expect(isPlainObject(data)).toBe(true)
})
test('sets a top-level key for each language', async () => {
expect('en' in data).toEqual(true)
expect('ja' in data).toEqual(true)
})
test('includes English variables', async () => {
const prodName = get(data, 'en.site.data.variables.product.prodname_dotcom')
expect(prodName).toBe('GitHub')
})
test('includes English reusables', async () => {
const reusable = get(
data,
'en.site.data.reusables.command_line.switching_directories_procedural'
)
expect(reusable).toBe('1. Change the current working directory to your local repository.')
})
test('includes Japanese variables', async () => {
const prodName = get(data, 'ja.site.data.variables.product.prodname_dotcom')
expect(prodName).toBe('GitHub')
})
test('includes Japanese reusables', async () => {
const reusable = get(data, 'ja.site.data.reusables.audit_log.octicon_icon')
expect(reusable.includes('任意のページの左上で')).toBe(true)
})
// TODO: re-enable once Janky flakyness is resolved
// Docs Engineering issue: 964
test.skip('backfills missing translated site data with English values', async () => {
const newFile = path.join(__dirname, '../../data/newfile.yml')
await fs.writeFile(newFile, 'newvalue: bar')
const data = await loadSiteData()
expect(get(data, 'en.site.data.newfile.newvalue')).toEqual('bar')
expect(get(data, 'ja.site.data.newfile.newvalue')).toEqual('bar')
await fs.unlink(newFile)
})
test('all Liquid templating is valid', async () => {
const dataMap = flat(data)
for (const key in dataMap) {
const value = dataMap[key]
if (!patterns.hasLiquid.test(value)) continue
let message = `${key} contains a malformed Liquid expression`
let result = null
try {
result = await liquid.parseAndRender(value)
} catch (err) {
console.trace(err)
message += `: ${err.message}`
}
expect(typeof result, message).toBe('string')
}
})
test('includes markdown files as data', async () => {
const reusable = get(
data,
'en.site.data.reusables.enterprise_enterprise_support.submit-support-ticket-first-section'
)
expect(typeof reusable).toBe('string')
expect(reusable.includes('1. ')).toBe(true)
})
// Docs Engineering issue: 965
test.skip('encodes bracketed parentheses to prevent them from becoming links', async () => {
const reusable = get(data, 'ja.site.data.reusables.organizations.team_name')
const expectation = `reusable should contain a bracket followed by a space. Actual value: ${reusable}`
expect(reusable.includes('] ('), expectation).toBe(true)
})
test('warn if any YAML reusables are found', async () => {
const reusables = walkSync(path.join(__dirname, '../../data/reusables'))
expect(reusables.length).toBeGreaterThan(100)
const yamlReusables = reusables.filter(
(filename) => filename.endsWith('.yml') || filename.endsWith('.yaml')
)
const message = `reusables are now written as individual Markdown files. Please migrate the following YAML files to Markdown:\n${yamlReusables.join(
'\n'
)}`
expect(yamlReusables.length, message).toBe(0)
})
test('all non-English data has matching English data', async () => {
for (const languageCode of Object.keys(data)) {
if (languageCode === 'en') continue
const nonEnglishKeys = Object.keys(flat(data[languageCode]))
for (const key of nonEnglishKeys) {
if (!has(data.en, key)) {
throw new Error(`matching data not found for ${languageCode}.${key}`)
}
}
}
})
})