-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.js
More file actions
133 lines (114 loc) 路 3.84 KB
/
Copy pathtest.js
File metadata and controls
133 lines (114 loc) 路 3.84 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
const Glossary = require('.')
const csvParser = require('csv-parser')
const stringToStream = require('string-to-stream')
const nock = require('nock')
// disallow all network requests
nock.disableNetConnect()
test('exports a function', () => {
expect(typeof Glossary).toBe('function')
})
describe('init', () => {
test('project is required', () => {
expect(() => {
Glossary()
}).toThrow('project is required')
})
test('crowdinKey is required', () => {
expect(() => {
Glossary({project: 'foo'})
}).toThrow('crowdinKey or process.env.CROWDIN_KEY is required')
})
test('crowdinKey falls back to CROWDIN_KEY environment variable', () => {
const existing = process.env.CROWDIN_KEY
process.env.CROWDIN_KEY = '123'
Glossary({project: 'foo'})
if (existing) {
process.env.CROWDIN_KEY = existing
} else {
delete process.env.CROWDIN_KEY
}
})
})
describe('glossary.add()', () => {
const glossary = Glossary({project: 'foo', crowdinKey: 'xyz'})
test('requires a term', () => {
expect(() => {
glossary.add(null)
}).toThrow('term is required')
})
test('requires a description', () => {
expect(() => {
glossary.add('term', null)
}).toThrow('description is required')
})
test('does not allow duplicate terms to be added', () => {
expect(() => {
glossary.add('IPC', 'inter-process communication')
glossary.add('IPC', 'inter-process communication')
}).toThrow('term IPC has already been added')
})
test('properly handles JS builtins like `constructor`', () => {
glossary.add('constructor', 'a thing')
glossary.add('toString', 'a thing')
glossary.add('hasOwnProperty', 'a thing')
})
})
describe('glossary.webpage', () => {
test('is a crowdin URL', () => {
const glossary = Glossary({project: 'foo-bar', crowdinKey: 'xyz'})
expect(glossary.webpage).toBe('https://crowdin.com/project/foo-bar/settings#glossary')
})
})
describe('glossary.csv', () => {
test('produces a valid CSV string', () => {
const glossary = Glossary({project: 'foo', crowdinKey: 'xyz'})
glossary.add('a', 'apples alligators anchovies')
glossary.add('b', 'blissful bubble bath')
glossary.add('c', 'collecting "crunchy" cornichons')
const results = {}
stringToStream(glossary.csv)
.pipe(csvParser(['term', 'description']))
.on('data', ({term, description}) => {
results[term] = description
})
.on('end', () => {
expect(Object.keys(results)).toEqual(['a', 'b', 'c'])
expect(Object.values(results)).toEqual([
'apples alligators anchovies',
'blissful bubble bath',
'collecting "crunchy" cornichons'
])
})
})
})
describe('glossary.upload()', () => {
test('POSTs to the Crowdin API', async () => {
const glossary = Glossary({project: 'foo', crowdinKey: 'xyz'})
const mock = nock('https://api.crowdin.com')
.post('/api/project/foo/upload-glossary')
.query({key: 'xyz'})
.reply(200, 'success')
glossary.add('a', 'apples')
glossary.add('b', 'bananas')
glossary.add('c', 'cornichons')
await glossary.upload()
expect(mock.isDone()).toBe(true)
})
// test('throws an error if no entries have been added')
test('handles errors', async () => {
// capture console.error() messages
const logs = []
const errors = message => (logs.push(message))
console['error'] = jest.fn(errors)
const glossary = Glossary({project: 'foo', crowdinKey: 'xyz'})
const mock = nock('https://api.crowdin.com')
.post('/api/project/foo/upload-glossary')
.query({key: 'xyz'})
.reply(500, 'something is wrong')
await glossary.upload()
expect(logs.length).toBe(2)
expect(logs[0]).toBe('Problem uploading glossary')
expect(logs[1].message).toMatch(/Response code 500/)
expect(mock.isDone()).toBe(true)
})
})