This repository has been archived by the owner on Oct 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
230 lines (213 loc) · 6.25 KB
/
test.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
import test from 'ava'
import supervillains from 'supervillains'
import jaymock from '.'
const fixtures = {
flat: {
firstName: 'name.firstName',
lastName: 'name.lastName'
},
nested: {
addresses: {
homeAddress: {
streetAddress: 'address.streetAddress',
city: 'address.city',
zipCode: 'address.zipCode'
},
workAddress: {
streetAddress: 'address.streetAddress',
city: 'address.city',
zipCode: 'address.zipCode'
}
}
},
repeat: {
firstName: 'name.firstName',
lastName: 'name.lastName',
_repeat: 5
},
repeatNested: {
firstName: 'name.firstName',
lastName: 'name.lastName',
ipAddress: {
ipv4: 'internet.ip',
ipv6: 'internet.ipv6',
_repeat: 3
}
},
arrayFunction: {
ipAddress: 'internet.ip|5'
},
customFunction: {
color: 'hexColor'
},
customFunctionAsObject: {
unicorn: 'foo'
},
customNestedFunction: {
supervillain: 'supervillains.random'
},
customArrayFunction: {
color: 'hexColor|10'
},
customNestedArrayFunction: {
supervillains: 'supervillains.random|5'
},
fakerFake: {
fullName: 'fake({{name.lastName}}, {{name.firstName}} {{name.suffix}})'
},
invalidFakerFunction: {
invalid: 'name.doesnt_exist'
},
invalidCustomFunction: {
invalid: 'doesnt_exist'
},
fakerLocale: {
name: 'name.firstName'
},
fakerSeed: {
number: 'random.number'
}
}
const ipAddressRegex = /^(?:\d{1,3}\.){3}\d{1,3}$/
const hexColorRegex = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/
const randomHexColor = () => '#' + ('000000' + Math.floor(Math.random() * 16777215).toString(16)).slice(-6)
test('flat object', t => {
const data = fixtures.flat
const expectedKeys = Object.keys(data)
const obj = jaymock().populate(data)
const actualKeys = Object.keys(obj)
t.deepEqual(expectedKeys, actualKeys)
actualKeys.forEach(key => {
t.true(obj[key] !== undefined && obj[key].length > 0)
})
})
test('nested object', t => {
const data = fixtures.nested
const expectedKeys = Object.keys(data)
const obj = jaymock().populate(data)
const actualKeys = Object.keys(data)
t.deepEqual(expectedKeys, actualKeys)
actualKeys.forEach(key => {
t.true(obj[key] !== undefined && (obj[key].length > 0 || typeof obj[key] === 'object'))
})
})
test('repeat mother object', t => {
const data = fixtures.repeat
const expectedKeys = Object.keys(data).filter(x => x !== '_repeat')
const objects = jaymock().populate(data)
objects.forEach(obj => {
const actualKeys = Object.keys(obj)
t.deepEqual(expectedKeys, actualKeys)
actualKeys.forEach(key => {
t.true(obj[key] !== undefined && obj[key].length > 0)
})
})
})
test('repeat nested object', t => {
const data = fixtures.repeatNested
const obj = jaymock().populate(data)
Object.keys(obj).forEach(key => {
const value = obj[key]
t.true(value !== undefined && value.length > 0)
if (key === 'ipAddress') {
t.true(Array.isArray(value))
value.forEach(innerObj => {
const expectedKeys = Object.keys(data.ipAddress).filter(x => x !== '_repeat')
const actualKeys = Object.keys(innerObj)
t.deepEqual(expectedKeys, actualKeys)
actualKeys.forEach(innerKey => {
t.true(innerObj[innerKey] !== undefined && innerObj[innerKey].length > 0)
})
})
}
})
})
test('{faker function}|{desired array length}', t => {
const data = fixtures.arrayFunction
const expectedKeys = Object.keys(data)
const obj = jaymock().populate(data)
const actualKeys = Object.keys(obj)
t.deepEqual(expectedKeys, actualKeys)
const actualArray = obj.ipAddress
t.true(Array.isArray(actualArray) && actualArray.length === parseInt(data.ipAddress.split('|')[1], 10))
actualArray.forEach(value => t.regex(value, ipAddressRegex))
})
test('custom data generation function', t => {
const data = fixtures.customFunction
const expectedKeys = Object.keys(data)
const jm = jaymock()
jm.extend('hexColor', () => randomHexColor())
const obj = jm.populate(data)
const actualKeys = Object.keys(obj)
t.deepEqual(expectedKeys, actualKeys)
t.regex(obj.color, hexColorRegex)
})
test('custom data generation function passed as object', t => {
const data = fixtures.customFunctionAsObject
const jm = jaymock()
jm.extend({foo: () => 'bar'})
const obj = jm.populate(data)
t.is(obj.unicorn, 'bar')
})
test('custom nested, data generation function', t => {
const data = fixtures.customNestedFunction
const jm = jaymock()
jm.extend({supervillains})
const obj = jm.populate(data)
t.true(supervillains.all.includes(obj.supervillain))
})
test('{custom function}|{desired array length}', t => {
const data = fixtures.customArrayFunction
const expectedKeys = Object.keys(data)
const jm = jaymock()
jm.extend('hexColor', () => randomHexColor())
const obj = jm.populate(data)
const actualKeys = Object.keys(obj)
t.deepEqual(expectedKeys, actualKeys)
const actualArray = obj.color
t.true(Array.isArray(actualArray) && actualArray.length === parseInt(data.color.split('|')[1], 10))
actualArray.forEach(value => t.regex(value, hexColorRegex))
})
test('{custom nested function}|{desired array length}', t => {
const data = fixtures.customNestedArrayFunction
const jm = jaymock()
jm.extend({supervillains})
const obj = jm.populate(data)
t.true(obj.supervillains.every(element => supervillains.all.includes(element)))
})
test('faker.fake() generation function', t => {
const data = fixtures.fakerFake
const expectedKeys = Object.keys(data)
const obj = jaymock().populate(data)
const actualKeys = Object.keys(obj)
t.deepEqual(expectedKeys, actualKeys)
t.true(obj.fullName.split(' ').length > 1)
})
test('invalid faker function', t => {
const data = fixtures.invalidFakerFunction
const error = t.throws(() => {
jaymock().populate(data)
}, Error)
t.is(error.message, `Function ${JSON.stringify(data.invalid)} does not exist`)
})
test('invalid custom function', t => {
const data = fixtures.invalidCustomFunction
const error = t.throws(() => {
jaymock().populate(data)
}, Error)
t.is(error.message, `Function ${JSON.stringify(data.invalid)} does not exist`)
})
test('faker locale', t => {
const data = fixtures.fakerLocale
const jm = jaymock()
jm.setFakerLocale('ru')
const obj = jm.populate(data)
t.regex(obj[Object.keys(obj)[0]], /[\w\u0430-\u044F]+/)
})
test('faker random seed', t => {
const data = fixtures.fakerSeed
const jm = jaymock()
jm.setFakerSeed(1)
const obj = jm.populate(data)
t.is(obj[Object.keys(obj)[0]], 41702)
})