UNPKG

19.7 kBJavaScriptView Raw
1import test from 'ava'
2import Chance from '../chance.js'
3import _ from 'lodash'
4
5const chance = new Chance()
6
7// chance.avatar()
8test('avatar() returns a legit avatar', t => {
9 _.times(1000, () => {
10 let avatar = chance.avatar()
11 t.true(_.isString(avatar))
12 t.is(avatar.split('/').length, 5)
13 })
14})
15
16test('avatar() can take and ignore an invalid protocol', t => {
17 _.times(1000, () => {
18 let avatar = chance.avatar({ protocol: 'invalid' })
19 t.true(_.isString(avatar))
20 t.is(avatar.indexOf('//'), 0)
21 })
22})
23
24test('avatar() can take and respect a protocol', t => {
25 const protocols = ['http', 'https']
26 _.times(500, () => {
27 protocols.map((protocol) => {
28 let avatar = chance.avatar({ protocol: protocol })
29 t.true(_.isString(avatar))
30 t.is(avatar.indexOf(protocol + ":"), 0)
31 })
32 })
33})
34
35test('avatar() can take and respect email', t => {
36 _.times(1000, () => {
37 let avatar = chance.avatar({ email: chance.email() })
38 t.true(_.isString(avatar))
39 t.is(avatar.split('/').length, 5)
40 })
41})
42
43test('avatar() can take and ignore an invalid file extension', t => {
44 _.times(1000, () => {
45 let avatar = chance.avatar({ fileExtension: 'invalid' })
46 t.true(_.isString(avatar))
47 t.false(avatar.includes('.jpg'))
48 t.false(avatar.includes('.png'))
49 t.false(avatar.includes('.gif'))
50 })
51})
52
53test('avatar() can take and respect a legit file extension', t => {
54 let file_types = ['bmp', 'gif', 'jpg', 'png']
55 _.times(250, () => {
56 _.times(file_types.length, (index) => {
57 let avatar = chance.avatar({ fileExtension: file_types[index] })
58 t.true(_.isString(avatar))
59 t.true(avatar.includes('.' + file_types[index]))
60 })
61 })
62})
63
64test('avatar() can take and ignore an invalid size', t => {
65 _.times(1000, () => {
66 let avatar = chance.avatar({ size: 'abc' })
67 t.true(_.isString(avatar))
68 t.false(avatar.includes('&s='))
69 })
70})
71
72test('avatar() can take and respect a legit size', t => {
73 _.times(1000, (index) => {
74 let avatar = chance.avatar({ size: index + 1 })
75 t.true(_.isString(avatar))
76 t.true(avatar.includes('&s=' + (index + 1).toString()))
77 })
78})
79
80test('avatar() can take and ignore an invalid rating', t => {
81 _.times(1000, () => {
82 let avatar = chance.avatar({ rating: 'invalid' })
83 t.true(_.isString(avatar))
84 t.false(avatar.includes('&r='))
85 })
86})
87
88test('avatar() can take and respect a rating', t => {
89 let ratings = ['g', 'pg', 'r', 'x']
90 _.times(250, () => {
91 _.times(ratings.length, (index) => {
92 let avatar = chance.avatar({ rating: ratings[index] })
93 t.true(_.isString(avatar))
94 t.true(avatar.includes('&r=' + ratings[index]))
95 })
96 })
97})
98
99test('avatar() can take and ignore an invalid fallback image', t => {
100 _.times(1000, () => {
101 let avatar = chance.avatar({ fallback: 'invalid' })
102 t.true(_.isString(avatar))
103 t.false(avatar.includes('&d='))
104 })
105})
106
107test('avatar() can take just an email', t => {
108 _.times(1000, () => {
109 let avatar = chance.avatar('mail@victorquinn.com')
110 t.true(_.isString(avatar))
111 t.false(avatar.includes('&d='))
112 })
113})
114
115test('avatar() can take and respect a fallback image', t => {
116 let fallbacks = [
117 '404', // Return 404 if not found
118 'mm', // Mystery man
119 'identicon', // Geometric pattern based on hash
120 'monsterid', // A generated monster icon
121 'wavatar', // A generated face
122 'retro', // 8-bit icon
123 'blank' // A transparent png
124 ]
125 _.times(100, () => {
126 _.times(fallbacks.length, (index) => {
127 let avatar = chance.avatar({ fallback: fallbacks[index] })
128 t.true(_.isString(avatar))
129 t.true(avatar.includes('&d=' + fallbacks[index]))
130 })
131 })
132})
133
134// chance.color()
135test('color() returns what looks like a hex color', t => {
136 _.times(1000, () => {
137 let color = chance.color({ format: 'hex' })
138 t.true(_.isString(color))
139 t.is(color.length, 7)
140 t.true(/#[a-z0-9]+/m.test(color))
141 })
142})
143
144test('color() returns what looks like a gray scale hex color', t => {
145 _.times(1000, () => {
146 let color = chance.color({ format: 'hex', grayscale: true })
147 t.true(_.isString(color))
148 t.is(color.length, 7)
149 t.true(/#[a-z0-9]+/m.test(color))
150 t.is(color.slice(1, 3), color.slice(3, 5))
151 t.is(color.slice(1, 3), color.slice(5, 7))
152 })
153})
154
155test('color() returns a short hex color', t => {
156 _.times(1000, () => {
157 let color = chance.color({ format: 'shorthex' })
158 t.true(_.isString(color))
159 t.is(color.length, 4)
160 t.true(/#[a-z0-9]+/m.test(color))
161 })
162})
163
164test('color() returns what looks like a grayscale short hex color', t => {
165 _.times(1000, () => {
166 let color = chance.color({ format: 'shorthex', grayscale: true })
167 t.true(_.isString(color))
168 t.is(color.length, 4)
169 t.is(color.slice(1, 2), color.slice(2, 3))
170 t.is(color.slice(1, 2), color.slice(3, 4))
171 })
172})
173
174test('color() returns what looks like an rgb color', t => {
175 _.times(1000, () => {
176 let color = chance.color({ format: 'rgb' })
177 t.true(_.isString(color))
178 let match = /rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)/.exec(color)
179 t.is(match.length, 4)
180 t.true(match[1] >= 0)
181 t.true(match[1] <= 255)
182 t.true(match[2] >= 0)
183 t.true(match[2] <= 255)
184 t.true(match[3] >= 0)
185 t.true(match[3] <= 255)
186 })
187})
188
189test('color() returns what looks like a grayscale rgb color', t => {
190 _.times(1000, () => {
191 let color = chance.color({ format: 'rgb', grayscale: true })
192 t.true(_.isString(color))
193 let match = /rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)/.exec(color)
194 t.is(match.length, 4)
195 t.true(match[1] >= 0)
196 t.true(match[1] <= 255)
197 t.is(match[1], match[2])
198 t.is(match[1], match[3])
199 })
200})
201
202test('color() returns what looks like an rgba color', t => {
203 _.times(1000, () => {
204 let color = chance.color({ format: 'rgba' })
205 t.true(_.isString(color))
206 let match = /rgba\((\d{1,3}),(\d{1,3}),(\d{1,3}),([01]\.?\d*?)\)/.exec(color)
207 t.is(match.length, 5)
208 t.true(match[1] >= 0)
209 t.true(match[1] <= 255)
210 t.true(match[2] >= 0)
211 t.true(match[2] <= 255)
212 t.true(match[3] >= 0)
213 t.true(match[3] <= 255)
214 t.true(match[4] >= 0)
215 t.true(match[4] <= 255)
216 })
217})
218
219test('color() returns what looks like a grayscale rgba color', t => {
220 _.times(1000, () => {
221 let color = chance.color({ format: 'rgba', grayscale: true })
222 t.true(_.isString(color))
223 let match = /rgba\((\d{1,3}),(\d{1,3}),(\d{1,3}),([01]\.?\d*?)\)/.exec(color)
224 t.is(match.length, 5)
225 t.true(match[1] >= 0)
226 t.true(match[1] <= 255)
227 t.true(match[2] >= 0)
228 t.true(match[2] <= 255)
229 t.true(match[3] >= 0)
230 t.true(match[3] <= 255)
231 t.true(match[4] >= 0)
232 t.true(match[4] <= 255)
233 t.is(match[1], match[2])
234 t.is(match[1], match[3])
235 t.true(match[4] >= 0)
236 t.true(match[4] <= 1)
237 })
238})
239
240test('color() 0x color works', t => {
241 _.times(1000, () => {
242 let color = chance.color({ format: '0x' })
243 t.true(_.isString(color))
244 t.is(color.length, 8)
245 t.true(/0x[a-z0-9]+/m.test(color))
246 })
247})
248
249test('color() with name returns a valid color name', t => {
250 _.times(1000, () => {
251 let color = chance.color({ format: 'name' })
252 t.true(_.isString(color))
253 })
254})
255
256test('color() upper case returns upper cased color', t => {
257 _.times(1000, () => {
258 let color = chance.color({ format: 'hex', casing: 'upper' })
259 t.true(_.isString(color))
260 t.is(color.length, 7)
261 t.is(color.charAt(1).toUpperCase(), color.charAt(1))
262 t.is(color.charAt(2).toUpperCase(), color.charAt(2))
263 t.is(color.charAt(3).toUpperCase(), color.charAt(3))
264 t.is(color.charAt(4).toUpperCase(), color.charAt(4))
265 t.is(color.charAt(5).toUpperCase(), color.charAt(5))
266 t.is(color.charAt(6).toUpperCase(), color.charAt(6))
267 })
268})
269
270test('color() bogus format throws error', t => {
271 const fn = () => chance.color({ format: 'banana' })
272 t.throws(fn)
273})
274
275// chance.domain()
276test('domain() returns a domain', t => {
277 _.times(1000, () => {
278 let domain = chance.domain()
279 t.true(_.isString(domain))
280 t.true(domain.split('.').length > 1)
281 })
282})
283
284test('domain() obeys tld, if specified', t => {
285 _.times(1000, () => {
286 let domain = chance.domain({ tld: 'com' })
287 t.true(_.isString(domain))
288 t.is(domain.split('.')[1], 'com')
289 })
290})
291
292// chance.email()
293test('email() returns what looks like an email', t => {
294 _.times(1000, () => {
295 let email = chance.email()
296 t.true(_.isString(email))
297 t.true(email.split('@').length > 1)
298 })
299})
300
301test('email() obeys domain, if specified', t => {
302 _.times(1000, () => {
303 let email = chance.email({ domain: 'victorquinn.com' })
304 t.true(_.isString(email))
305 t.is(email.split('@')[1], 'victorquinn.com')
306 })
307})
308
309test('email() has a leng specified, should generate string before domain with equal length', t => {
310 _.times(1000, () => {
311 let email = chance.email({ length: 5 })
312 t.is(email.split('@')[0].length, 5)
313 })
314})
315
316// chance.fbid()
317test('fbid() returns what looks like a Facebook id', t => {
318 _.times(1000, () => {
319 let fbid = chance.fbid()
320 t.true(_.isString(fbid))
321 t.is(fbid.length, 16)
322 })
323})
324
325// chance.google_analytics()
326test('google_analytics() returns what looks like a valid tracking code', t => {
327 _.times(1000, () => {
328 let tracking_code = chance.google_analytics()
329 t.true(_.isString(tracking_code))
330 t.is(tracking_code.length, 12)
331 t.true(tracking_code.includes('UA-'))
332 })
333})
334
335// chance.hashtag()
336test('hashtag() returns what looks like a hashtag', t => {
337 _.times(1000, () => {
338 let hashtag = chance.hashtag()
339 t.true(_.isString(hashtag))
340 t.true(/^\#\w+$/m.test(hashtag))
341 })
342})
343
344// chance.ip()
345test('ip() returns what looks like an IP address', t => {
346 _.times(1000, () => {
347 let ip = chance.ip()
348 t.true(_.isString(ip))
349 t.is(ip.split('.').length, 4)
350 t.true(/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/.test(ip))
351 })
352})
353
354// chance.ipv6()
355test('ipv6() returns what looks like an IP address (v6)', t => {
356 _.times(1000, () => {
357 let ipv6 = chance.ipv6()
358 t.true(_.isString(ipv6))
359 t.is(ipv6.split(':').length, 8)
360 t.true(/[a-f0-9]+:[a-f0-9]+:[a-f0-9]+:[a-f0-9]+:[a-f0-9]+:[a-f0-9]+:[a-f0-9]+:[a-f0-9]+/.test(ipv6))
361 })
362})
363
364// chance.klout()
365test('klout() returns what looks like a legit Klout score', t => {
366 _.times(1000, () => {
367 let klout = chance.klout()
368 t.true(_.isNumber(klout))
369 t.true(klout > 0)
370 t.true(klout <= 100)
371 })
372})
373
374// chance.locale()
375test('locale() should create a valid two character locale with only language', t => {
376 let locale = chance.locale()
377 t.true(_.isString(locale))
378 t.is(locale.length, 2)
379})
380
381test('locale() should create a locale with a region code', t => {
382 let locale = chance.locale({ region: true })
383 t.true(_.isString(locale))
384 t.true(locale.split('-').length >= 2)
385})
386
387// chance.locales()
388test('locales() should return a list of locales', t => {
389 let locales = chance.locales()
390 t.true(_.isArray(locales))
391 t.true(locales.length > 100)
392})
393
394test('locales() should return a list of locales', t => {
395 let locales = chance.locales({ region: true })
396 t.true(_.isArray(locales))
397 t.true(locales.length > 100)
398})
399
400// chance.mac()
401test('mac() returns what looks like an MAC address (EUI-48)', t => {
402 _.times(1000, () => {
403 let mac = chance.mac()
404 t.true(_.isString(mac))
405 t.is(mac.split(':').length, 6)
406 t.true(/^[0-9a-f]{2}\:[0-9a-f]{2}\:[0-9a-f]{2}\:[0-9a-f]{2}\:[0-9a-f]{2}\:[0-9a-f]{2}$/.test(mac))
407 })
408})
409test('mac() uses delimiter option for MAC address', t => {
410_.times(1000, () => {
411 const delimiter = ([':','-','.'])[Math.floor(Math.random() * 3)]
412 let mac = chance.mac({ delimiter })
413 t.true(_.isString(mac))
414 t.is(mac.split(delimiter).length, 6)
415 t.true((
416 new RegExp(`^${Array(6).fill('[0-9a-f]{2}').join(`\\${delimiter}`)}$`)
417 ).test(mac))
418 })
419})
420
421// chance.md5()
422test('md5() should create a hex-encoded MD5 hash of a random ASCII value when passed nothing', t => {
423 t.is(chance.md5().length, '2063c1608d6e0baf80249c42e2be5804'.length)
424})
425
426test('md5() should create a hex-encoded MD5 hash of an ASCII value when passed a string', t => {
427 t.is(chance.md5('value'), '2063c1608d6e0baf80249c42e2be5804')
428})
429
430test('md5() should create a hex-encoded MD5 hash of an ASCII value when passed an object', t => {
431 t.is(chance.md5({ str: 'value' }), '2063c1608d6e0baf80249c42e2be5804')
432})
433
434test('md5() should create a hex-encoded MD5 hash of a UTF-8 value', t => {
435 t.is(chance.md5('日本'), '4dbed2e657457884e67137d3514119b3')
436})
437
438test('md5() should create a hex-encoded HMAC-MD5 hash of an ASCII value and key', t => {
439 t.is(chance.md5({ str: 'value', key: 'key' }), '01433efd5f16327ea4b31144572c67f6')
440})
441
442test('md5() should create a hex-encoded HMAC-MD5 hash of a UTF-8 value and key', t => {
443 t.is(chance.md5({ str: '日本', key: '日本' }), 'c78b8c7357926981cc04740bd3e9d015')
444})
445
446test('md5() should create a raw MD5 hash of an ASCII value', t => {
447 t.is(chance.md5({ str: 'value', key: null, raw: true }), ' c\xc1`\x8dn\x0b\xaf\x80$\x9cB\xe2\xbeX\x04')
448})
449
450test('md5() should create a raw MD5 hash of a UTF-8 value', t => {
451 t.is(chance.md5({ str: '日本', key: null, raw: true }), 'M\xbe\xd2\xe6WEx\x84\xe6q7\xd3QA\x19\xb3')
452})
453
454test('md5() should create a raw HMAC-MD5 hash of an ASCII value', t => {
455 t.is(chance.md5({ str: 'value', key: 'key', raw: true }), '\x01C>\xfd_\x162~\xa4\xb3\x11DW,g\xf6')
456})
457
458test('md5() should create a raw HMAC-MD5 hash of a UTF-8 value', t => {
459 t.is(chance.md5({ str: '日本', key: '日本', raw: true }), '\xc7\x8b\x8csW\x92i\x81\xcc\x04t\x0b\xd3\xe9\xd0\x15')
460})
461
462// chance.port()
463test('port() should create a number in the valid port range (0 - 65535)', t => {
464 _.times(1000, () => {
465 let port = chance.port()
466 t.true(_.isNumber(port))
467 t.true(port >= 0)
468 t.true(port <= 65535)
469 })
470})
471
472// chance.semver()
473test('semver() works as expected', t => {
474 _.times(1000, () => {
475 let semver = chance.semver()
476 t.true(_.isString(semver))
477 t.true(/[0-9]+\.[0-9]+\.[0-9]+/.test(semver))
478 })
479})
480
481test('semver() accepts a range', t => {
482 _.times(1000, () => {
483 let semver = chance.semver({ range: 'banana' })
484 t.true(_.isString(semver))
485 t.true(/^banana[0-9]+\.[0-9]+\.[0-9]+/.test(semver))
486 })
487})
488
489test('semver() accepts a prerelease flag', t => {
490 _.times(1000, () => {
491 let semver = chance.semver({ range: 'banana' })
492 t.true(_.isString(semver))
493 t.true(/[0-9]+\.[0-9]+\.[0-9]+-?[dev|beta|alpha]?/.test(semver))
494 })
495})
496
497// chance.tld()
498test('tld() returns a tld', t => {
499 _.times(1000, () => {
500 let tld = chance.tld()
501 t.true(_.isString(tld))
502 t.true(tld.length < 6)
503 })
504})
505
506// chance.twitter()
507test('twitter() returns what looks like a Twitter handle', t => {
508 _.times(1000, () => {
509 let twitter = chance.twitter()
510 t.true(_.isString(twitter))
511 t.true(/\@[A-Za-z]+/m.test(twitter))
512 })
513})
514
515// chance.url()
516test('url() deal with url', t => {
517 _.times(1000, () => {
518 let url = chance.url()
519 t.true(_.isString(url))
520 t.true(url.split('.').length > 1)
521 t.true(url.split('://').length > 1)
522 })
523})
524
525test('url() can take and respect a domain', t => {
526 _.times(1000, () => {
527 let url = chance.url({ domain: 'victorquinn.com' })
528 t.true(_.isString(url))
529 t.true(url.split('.').length > 1)
530 t.true(url.split('://').length > 1)
531 t.true(url.split('victorquinn.com').length > 1)
532 })
533})
534
535test('url() can take and respect a domain prefix', t => {
536 _.times(1000, () => {
537 let url = chance.url({ domain_prefix: 'www' })
538 t.true(_.isString(url))
539 t.true(url.split('.').length > 1)
540 t.true(url.split('://').length > 1)
541 t.true(url.split('www').length > 1)
542 })
543})
544
545test('url() can take and respect a path', t => {
546 _.times(1000, () => {
547 let url = chance.url({ path: 'images' })
548 t.true(_.isString(url))
549 t.true(url.split('.').length > 1)
550 t.true(url.split('://').length > 1)
551 t.true(url.split('/images').length > 1)
552 })
553})
554
555test('url() can take and respect extensions', t => {
556 _.times(1000, () => {
557 let url = chance.url({ extensions: ['html'] })
558 t.true(_.isString(url))
559 t.true(url.split('.').length > 1)
560 t.true(url.split('://').length > 1)
561 t.not(url.indexOf('.html'), -1)
562 })
563})
564
565// chance.loremPicsum()
566test('loremPicsum() returns loremPicsum url with default width and height', t => {
567 _.times(1000, () => {
568 let loremPicsumUrl = chance.loremPicsum()
569 t.true(_.isString(loremPicsumUrl))
570 t.true(loremPicsumUrl.split('.').length > 1)
571 t.true(loremPicsumUrl.split('://').length > 1)
572 t.true(loremPicsumUrl.split('picsum.photos').length > 1)
573 t.true(loremPicsumUrl.split('/500/500').length > 1)
574 t.true(loremPicsumUrl.split('/?random').length > 1)
575 })
576})
577test('loremPicsum() returns loremPicsum url that respects width and height', t => {
578 _.times(1000, () => {
579 let width = chance.natural();
580 let height = chance.natural();
581 let loremPicsumUrl = chance.loremPicsum({
582 width,
583 height
584 })
585 t.true(_.isString(loremPicsumUrl))
586 t.true(loremPicsumUrl.split('.').length > 1)
587 t.true(loremPicsumUrl.split('://').length > 1)
588 t.true(loremPicsumUrl.split('picsum.photos').length > 1)
589 t.true(loremPicsumUrl.split('/' + width + '/' + height).length > 1)
590 t.true(loremPicsumUrl.split('/?random').length > 1)
591 })
592})
593test('loremPicsum() returns loremPicsum url that respects greyscale', t => {
594 _.times(1000, () => {
595 let loremPicsumUrl = chance.loremPicsum({
596 greyscale: true
597 })
598 t.true(_.isString(loremPicsumUrl))
599 t.true(loremPicsumUrl.split('.').length > 1)
600 t.true(loremPicsumUrl.split('://').length > 1)
601 t.true(loremPicsumUrl.split('picsum.photos').length > 1)
602 t.true(loremPicsumUrl.split('/g/500/500').length > 1)
603 t.true(loremPicsumUrl.split('/?random').length > 1)
604 })
605})
606test('loremPicsum() returns loremPicsum url that respects blurred', t => {
607 _.times(1000, () => {
608 let loremPicsumUrl = chance.loremPicsum({
609 blurred: true
610 })
611 t.true(_.isString(loremPicsumUrl))
612 t.true(loremPicsumUrl.split('.').length > 1)
613 t.true(loremPicsumUrl.split('://').length > 1)
614 t.true(loremPicsumUrl.split('picsum.photos').length > 1)
615 t.true(loremPicsumUrl.split('/500/500').length > 1)
616 t.true(loremPicsumUrl.split('/?blur').length > 1)
617 })
618})