forked from oclif/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.test.ts
329 lines (262 loc) · 7.36 KB
/
util.test.ts
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import {expect} from 'chai'
import {THEME_KEYS} from '../../src/interfaces/config'
import {
capitalize,
castArray,
isNotFalsy,
isTruthy,
last,
maxBy,
mergeNestedObjects,
parseTheme,
sumBy,
} from '../../src/util/util'
describe('capitalize', () => {
it('capitalizes the string', () => {
expect(capitalize('dominik')).to.equal('Dominik')
})
it('works with an empty string', () => {
expect(capitalize('')).to.equal('')
})
})
type Item = {x: number}
describe('sumBy', () => {
it('returns zero for empty array', () => {
const arr: Item[] = []
expect(sumBy(arr, (i) => i.x)).to.equal(0)
})
it('returns sum for non-empty array', () => {
const arr: Item[] = [{x: 1}, {x: 2}, {x: 3}]
expect(sumBy(arr, (i) => i.x)).to.equal(6)
})
})
describe('maxBy', () => {
it('returns undefined for empty array', () => {
const arr: Item[] = []
expect(maxBy(arr, (i) => i.x)).to.be.undefined
})
it('returns max value in the array', () => {
const arr: Item[] = [{x: 1}, {x: 3}, {x: 2}]
expect(maxBy(arr, (i) => i.x)).to.equal(arr[1])
})
})
describe('last', () => {
it('returns undefined for empty array', () => {
expect(last([])).to.be.undefined
})
it('returns undefined for undefined', () => {
expect(last()).to.be.undefined
})
it('returns last value in the array', () => {
const arr: Item[] = [{x: 1}, {x: 3}, {x: 2}]
expect(last(arr)).to.equal(arr[2])
})
it('returns only item in array', () => {
expect(last([6])).to.equal(6)
})
})
describe('isNotFalsy', () => {
it('should return true for truthy values', () => {
expect(isNotFalsy('true')).to.be.true
expect(isNotFalsy('1')).to.be.true
expect(isNotFalsy('yes')).to.be.true
expect(isNotFalsy('y')).to.be.true
})
it('should return false for falsy values', () => {
expect(isNotFalsy('false')).to.be.false
expect(isNotFalsy('0')).to.be.false
expect(isNotFalsy('no')).to.be.false
expect(isNotFalsy('n')).to.be.false
})
})
describe('isTruthy', () => {
it('should return true for truthy values', () => {
expect(isTruthy('true')).to.be.true
expect(isTruthy('1')).to.be.true
expect(isTruthy('yes')).to.be.true
expect(isTruthy('y')).to.be.true
})
it('should return false for falsy values', () => {
expect(isTruthy('false')).to.be.false
expect(isTruthy('0')).to.be.false
expect(isTruthy('no')).to.be.false
expect(isTruthy('n')).to.be.false
})
})
describe('castArray', () => {
it('should cast a value to an array', () => {
expect(castArray('foo')).to.deep.equal(['foo'])
})
it('should return an array if the value is an array', () => {
expect(castArray(['foo'])).to.deep.equal(['foo'])
})
it('should return an empty array if the value is undefined', () => {
expect(castArray()).to.deep.equal([])
})
})
describe('mergeNestedObjects', () => {
it('should merge nested objects', () => {
const a = {
tsconfig: {
compilerOptions: {
outDir: 'dist',
rootDir: 'src',
},
'ts-node': {
transpileOnly: true,
},
},
}
const b = {
tsconfig: {
compilerOptions: {
outDir: 'dist',
rootDir: 'src',
},
'ts-node': {
transpileOnly: false,
},
},
}
expect(mergeNestedObjects([a, b], 'tsconfig.ts-node')).to.deep.equal({
transpileOnly: true,
})
})
})
describe('theme parsing', () => {
it('should parse untyped theme json to theme', () => {
const untypedTheme = {
alias: '#FFFFFF',
bin: '#FFFFFF',
command: '#FFFFFF',
commandSummary: '#FFFFFF',
dollarSign: '#FFFFFF',
flag: '#FFFFFF',
flagDefaultValue: '#FFFFFF',
flagOptions: '#FFFFFF',
flagRequired: '#FFFFFF',
flagSeparator: '#FFFFFF',
flagType: '#FFFFFF',
sectionDescription: '#FFFFFF',
sectionHeader: '#FFFFFF',
topic: '#FFFFFF',
version: '#FFFFFF',
}
const theme = parseTheme(untypedTheme)
for (const key of Object.keys(theme)) {
expect(THEME_KEYS.includes(key)).to.be.true
}
})
it('should parse alias', () => {
const untypedTheme = {
alias: '#FFFFFF',
}
const theme = parseTheme(untypedTheme)
expect(theme.alias?.hex()).to.equal('#FFFFFF')
})
it('should parse bin', () => {
const untypedTheme = {
bin: '#FFFFFF',
}
const theme = parseTheme(untypedTheme)
expect(theme.bin?.hex()).to.equal('#FFFFFF')
})
it('should parse command', () => {
const untypedTheme = {
command: '#FFFFFF',
}
const theme = parseTheme(untypedTheme)
expect(theme.command?.hex()).to.equal('#FFFFFF')
})
it('should parse commandSummary', () => {
const untypedTheme = {
commandSummary: '#FFFFFF',
}
const theme = parseTheme(untypedTheme)
expect(theme.commandSummary?.hex()).to.equal('#FFFFFF')
})
it('should parse dollarSign', () => {
const untypedTheme = {
dollarSign: '#FFFFFF',
}
const theme = parseTheme(untypedTheme)
expect(theme.dollarSign?.hex()).to.equal('#FFFFFF')
})
it('should parse flag', () => {
const untypedTheme = {
flag: '#FFFFFF',
}
const theme = parseTheme(untypedTheme)
expect(theme.flag?.hex()).to.equal('#FFFFFF')
})
it('should parse flagDefaultValue', () => {
const untypedTheme = {
flagDefaultValue: '#FFFFFF',
}
const theme = parseTheme(untypedTheme)
expect(theme.flagDefaultValue?.hex()).to.equal('#FFFFFF')
})
it('should parse flagOptions', () => {
const untypedTheme = {
flagOptions: '#FFFFFF',
}
const theme = parseTheme(untypedTheme)
expect(theme.flagOptions?.hex()).to.equal('#FFFFFF')
})
it('should parse flagRequired', () => {
const untypedTheme = {
flagRequired: '#FFFFFF',
}
const theme = parseTheme(untypedTheme)
expect(theme.flagRequired?.hex()).to.equal('#FFFFFF')
})
it('should parse flagSeparator', () => {
const untypedTheme = {
flagSeparator: '#FFFFFF',
}
const theme = parseTheme(untypedTheme)
expect(theme.flagSeparator?.hex()).to.equal('#FFFFFF')
})
it('should parse flagType', () => {
const untypedTheme = {
flagType: '#FFFFFF',
}
const theme = parseTheme(untypedTheme)
expect(theme.flagType?.hex()).to.equal('#FFFFFF')
})
it('should parse sectionDescription', () => {
const untypedTheme = {
sectionDescription: '#FFFFFF',
}
const theme = parseTheme(untypedTheme)
expect(theme.sectionDescription?.hex()).to.equal('#FFFFFF')
})
it('should parse sectionHeader', () => {
const untypedTheme = {
sectionHeader: '#FFFFFF',
}
const theme = parseTheme(untypedTheme)
expect(theme.sectionHeader?.hex()).to.equal('#FFFFFF')
})
it('should parse topic', () => {
const untypedTheme = {
topic: '#FFFFFF',
}
const theme = parseTheme(untypedTheme)
expect(theme.topic?.hex()).to.equal('#FFFFFF')
})
it('should parse version', () => {
const untypedTheme = {
version: '#FFFFFF',
}
const theme = parseTheme(untypedTheme)
expect(theme.version?.hex()).to.equal('#FFFFFF')
})
it('should not parse color key that is not part of Theme', () => {
const untypedTheme = {
batman: '#000000',
}
const theme = parseTheme(untypedTheme)
expect(Object.keys(theme).includes('batman')).to.be.false
})
})