-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.spec.js
More file actions
213 lines (178 loc) · 7.06 KB
/
index.spec.js
File metadata and controls
213 lines (178 loc) · 7.06 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const {
parseConfig,
parseRawValue,
parseNumberDef,
parseStringDef,
parseBoolDef,
parseArrayDef,
parseObjectDef,
TYPES
} = require('./index')
/* eslint-disable no-undef */
describe('Module Config', () => {
describe('#parseStringDef', () => {
it('Should return empty string when raw is falsy and def.default is falsy', () => {
const result = parseStringDef(undefined, {})
expect(result).toBe('')
})
it('Should return def.default when raw is falsy and def.default is truthy', () => {
const result = parseStringDef(undefined, { default: 'test' })
expect(result).toBe('test')
})
it('Should return raw when raw is truthy', () => {
const result = parseStringDef('test', { default: 'test2' })
expect(result).toBe('test')
})
})
describe('#parseNumberDef', () => {
it('Should return a Number when raw value is a valid Number', () => {
const result = parseNumberDef('1', { default: 0 })
expect(result).toBe(1)
})
it('Should return def.default when raw is not a valid number and def.default is truthy', () => {
const result = parseNumberDef('test', { default: 1 })
expect(result).toBe(1)
})
it('Should return 0 when raw is not a valid number and default is falsy', () => {
const result = parseNumberDef('test', {})
expect(result).toBe(0)
})
it('Should return 0 when raw is falsey and def.default is falsy', () => {
const result = parseNumberDef(undefined, {})
expect(result).toBe(0)
})
it('Should return def.default value when raw is falsey and def.default is truthy', () => {
const result = parseNumberDef(undefined, { default: 1 })
expect(result).toBe(1)
})
})
describe('#parseBoolDef', () => {
it('Should return true when raw is "true"', () => {
const result = parseBoolDef('true', { default: false })
expect(result).toBe(true)
})
it('Should return true when raw is "True"', () => {
const result = parseBoolDef('True', { default: false })
expect(result).toBe(true)
})
it('Should return true when raw is "TRUE"', () => {
const result = parseBoolDef('TRUE', { default: false })
expect(result).toBe(true)
})
it('Should return true when raw is "1"', () => {
const result = parseBoolDef('1', { default: false })
expect(result).toBe(true)
})
it('Should return false when raw is truthy and not a true value', () => {
const result = parseBoolDef('test', { default: true })
expect(result).toBe(false)
})
it('Should return false when raw is falsey and def.default is falsy', () => {
const result = parseBoolDef(undefined, {})
expect(result).toBe(false)
})
it('Should return def.default when raw is falsey and def.default is truthy', () => {
const result = parseBoolDef(undefined, { default: true })
expect(result).toBe(true)
})
})
describe('#parseArrayDef', () => {
it('Should return ["one","two","three"] when raw is "one,two,three"', () => {
const result = parseArrayDef('one,two,three', { default: ['1'] })
expect(result).toEqual(['one', 'two', 'three'])
})
it('Should return ["1","2","3"] when raw is "1,2,3"', () => {
const result = parseArrayDef('1,2,3', { default: ['1'] })
expect(result).toEqual(['1', '2', '3'])
})
it('Should return ["foo bar"] when raw is "foo bar"', () => {
const result = parseArrayDef('foo bar', { default: ['1'] })
expect(result).toEqual(['foo bar'])
})
it('Should return [] when raw is falsey and def.default is falsy', () => {
const result = parseArrayDef(undefined, {})
expect(result).toEqual([])
})
it('Should return def.default when raw is falsey and def.default is truthy', () => {
const result = parseArrayDef(undefined, { default: ['1'] })
expect(result).toEqual(['1'])
})
})
describe('#parseObjectDef', () => {
it('Should return { foo: "bar", count: 1, valid: true } when raw is "{"foo":"bar","count":1,"valid":true}"', () => {
const result = parseObjectDef('{"foo":"bar","count":1,"valid":true}', { default: { default: true } })
expect(result).toEqual({ foo: 'bar', count: 1, valid: true })
})
it('Should return { arr: [1], obj: { foo: "bar" } } when raw is "{"arr":[1],"obj":{"foo":"bar"}}"', () => {
const result = parseObjectDef('{"arr":[1],"obj":{"foo":"bar"}}', { default: { default: true } })
expect(result).toEqual({ arr: [1], obj: { foo: 'bar' } })
})
it('Should return {} when raw is "bad json"', () => {
const result = parseObjectDef('bad json', { default: { default: true } })
expect(result).toEqual({})
})
it('Should return {} when raw is falsey and def.default is falsy', () => {
const result = parseObjectDef(undefined, {})
expect(result).toEqual({})
})
it('Should return def.default when raw is falsy and def.default is truthy', () => {
const result = parseObjectDef(undefined, { default: { default: true } })
expect(result).toEqual({ default: true })
})
})
describe('#parseRawValue', () => {
it('Should return a parsed number when def.type is "number"', () => {
const result = parseRawValue('1', { type: TYPES.NUMBER })
expect(typeof result).toBe('number')
})
it('Should return a parsed string when def.type is "string"', () => {
const result = parseRawValue('hello', { type: TYPES.STRING })
expect(typeof result).toBe('string')
})
it('Should return a parsed bool when def.type is "bool"', () => {
const result = parseRawValue('TRUE', { type: TYPES.BOOL })
expect(typeof result).toBe('boolean')
})
it('Should return a parsed array when def.type is "array"', () => {
const result = parseRawValue('1,2', { type: TYPES.ARRAY })
expect(Array.isArray(result)).toBe(true)
})
it('Should return a parsed object when def.type is "object"', () => {
const result = parseRawValue('{"foo": "bar"}', { type: TYPES.OBJECT })
expect(result).toEqual({ foo: 'bar' })
})
it('Should return the raw value when type is not recognized', () => {
const result = parseRawValue('some blob', { type: 'blob' })
expect(result).toBe('some blob')
})
})
describe('#parseConfig', () => {
let config = {}
beforeEach(() => {
config = {
TEST_STRING: { default: 'default', type: TYPES.STRING },
TEST_NUMBER: { default: -1, type: TYPES.NUMBER },
TEST_BOOL: { default: false, type: TYPES.BOOL },
TEST_ARRAY: { default: [], type: TYPES.ARRAY },
TEST_OBJECT: { default: {}, type: TYPES.OBJECT }
}
process.env = {
TEST_STRING: 'hello',
TEST_NUMBER: '1',
TEST_BOOL: 'true',
TEST_ARRAY: '1,2',
TEST_OBJECT: '{"foo": "bar"}'
}
})
it('Should parse out the given config', () => {
const result = parseConfig(config)
expect(result).toEqual({
TEST_STRING: 'hello',
TEST_NUMBER: 1,
TEST_BOOL: true,
TEST_ARRAY: ['1', '2'],
TEST_OBJECT: { foo: 'bar' }
})
})
})
})