UNPKG

18.9 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.md5()
401test('md5() should create a hex-encoded MD5 hash of a random ASCII value when passed nothing', t => {
402 t.is(chance.md5().length, '2063c1608d6e0baf80249c42e2be5804'.length)
403})
404
405test('md5() should create a hex-encoded MD5 hash of an ASCII value when passed a string', t => {
406 t.is(chance.md5('value'), '2063c1608d6e0baf80249c42e2be5804')
407})
408
409test('md5() should create a hex-encoded MD5 hash of an ASCII value when passed an object', t => {
410 t.is(chance.md5({ str: 'value' }), '2063c1608d6e0baf80249c42e2be5804')
411})
412
413test('md5() should create a hex-encoded MD5 hash of a UTF-8 value', t => {
414 t.is(chance.md5('日本'), '4dbed2e657457884e67137d3514119b3')
415})
416
417test('md5() should create a hex-encoded HMAC-MD5 hash of an ASCII value and key', t => {
418 t.is(chance.md5({ str: 'value', key: 'key' }), '01433efd5f16327ea4b31144572c67f6')
419})
420
421test('md5() should create a hex-encoded HMAC-MD5 hash of a UTF-8 value and key', t => {
422 t.is(chance.md5({ str: '日本', key: '日本' }), 'c78b8c7357926981cc04740bd3e9d015')
423})
424
425test('md5() should create a raw MD5 hash of an ASCII value', t => {
426 t.is(chance.md5({ str: 'value', key: null, raw: true }), ' c\xc1`\x8dn\x0b\xaf\x80$\x9cB\xe2\xbeX\x04')
427})
428
429test('md5() should create a raw MD5 hash of a UTF-8 value', t => {
430 t.is(chance.md5({ str: '日本', key: null, raw: true }), 'M\xbe\xd2\xe6WEx\x84\xe6q7\xd3QA\x19\xb3')
431})
432
433test('md5() should create a raw HMAC-MD5 hash of an ASCII value', t => {
434 t.is(chance.md5({ str: 'value', key: 'key', raw: true }), '\x01C>\xfd_\x162~\xa4\xb3\x11DW,g\xf6')
435})
436
437test('md5() should create a raw HMAC-MD5 hash of a UTF-8 value', t => {
438 t.is(chance.md5({ str: '日本', key: '日本', raw: true }), '\xc7\x8b\x8csW\x92i\x81\xcc\x04t\x0b\xd3\xe9\xd0\x15')
439})
440
441// chance.port()
442test('port() should create a number in the valid port range (0 - 65535)', t => {
443 _.times(1000, () => {
444 let port = chance.port()
445 t.true(_.isNumber(port))
446 t.true(port >= 0)
447 t.true(port <= 65535)
448 })
449})
450
451// chance.semver()
452test('semver() works as expected', t => {
453 _.times(1000, () => {
454 let semver = chance.semver()
455 t.true(_.isString(semver))
456 t.true(/[0-9]+\.[0-9]+\.[0-9]+/.test(semver))
457 })
458})
459
460test('semver() accepts a range', t => {
461 _.times(1000, () => {
462 let semver = chance.semver({ range: 'banana' })
463 t.true(_.isString(semver))
464 t.true(/^banana[0-9]+\.[0-9]+\.[0-9]+/.test(semver))
465 })
466})
467
468test('semver() accepts a prerelease flag', t => {
469 _.times(1000, () => {
470 let semver = chance.semver({ range: 'banana' })
471 t.true(_.isString(semver))
472 t.true(/[0-9]+\.[0-9]+\.[0-9]+-?[dev|beta|alpha]?/.test(semver))
473 })
474})
475
476// chance.tld()
477test('tld() returns a tld', t => {
478 _.times(1000, () => {
479 let tld = chance.tld()
480 t.true(_.isString(tld))
481 t.true(tld.length < 6)
482 })
483})
484
485// chance.twitter()
486test('twitter() returns what looks like a Twitter handle', t => {
487 _.times(1000, () => {
488 let twitter = chance.twitter()
489 t.true(_.isString(twitter))
490 t.true(/\@[A-Za-z]+/m.test(twitter))
491 })
492})
493
494// chance.url()
495test('url() deal with url', t => {
496 _.times(1000, () => {
497 let url = chance.url()
498 t.true(_.isString(url))
499 t.true(url.split('.').length > 1)
500 t.true(url.split('://').length > 1)
501 })
502})
503
504test('url() can take and respect a domain', t => {
505 _.times(1000, () => {
506 let url = chance.url({ domain: 'victorquinn.com' })
507 t.true(_.isString(url))
508 t.true(url.split('.').length > 1)
509 t.true(url.split('://').length > 1)
510 t.true(url.split('victorquinn.com').length > 1)
511 })
512})
513
514test('url() can take and respect a domain prefix', t => {
515 _.times(1000, () => {
516 let url = chance.url({ domain_prefix: 'www' })
517 t.true(_.isString(url))
518 t.true(url.split('.').length > 1)
519 t.true(url.split('://').length > 1)
520 t.true(url.split('www').length > 1)
521 })
522})
523
524test('url() can take and respect a path', t => {
525 _.times(1000, () => {
526 let url = chance.url({ path: 'images' })
527 t.true(_.isString(url))
528 t.true(url.split('.').length > 1)
529 t.true(url.split('://').length > 1)
530 t.true(url.split('/images').length > 1)
531 })
532})
533
534test('url() can take and respect extensions', t => {
535 _.times(1000, () => {
536 let url = chance.url({ extensions: ['html'] })
537 t.true(_.isString(url))
538 t.true(url.split('.').length > 1)
539 t.true(url.split('://').length > 1)
540 t.not(url.indexOf('.html'), -1)
541 })
542})
543
544// chance.loremPicsum()
545test('loremPicsum() returns loremPicsum url with default width and height', t => {
546 _.times(1000, () => {
547 let loremPicsumUrl = chance.loremPicsum()
548 t.true(_.isString(loremPicsumUrl))
549 t.true(loremPicsumUrl.split('.').length > 1)
550 t.true(loremPicsumUrl.split('://').length > 1)
551 t.true(loremPicsumUrl.split('picsum.photos').length > 1)
552 t.true(loremPicsumUrl.split('/500/500').length > 1)
553 t.true(loremPicsumUrl.split('/?random').length > 1)
554 })
555})
556test('loremPicsum() returns loremPicsum url that respects width and height', t => {
557 _.times(1000, () => {
558 let width = chance.natural();
559 let height = chance.natural();
560 let loremPicsumUrl = chance.loremPicsum({
561 width,
562 height
563 })
564 t.true(_.isString(loremPicsumUrl))
565 t.true(loremPicsumUrl.split('.').length > 1)
566 t.true(loremPicsumUrl.split('://').length > 1)
567 t.true(loremPicsumUrl.split('picsum.photos').length > 1)
568 t.true(loremPicsumUrl.split('/' + width + '/' + height).length > 1)
569 t.true(loremPicsumUrl.split('/?random').length > 1)
570 })
571})
572test('loremPicsum() returns loremPicsum url that respects greyscale', t => {
573 _.times(1000, () => {
574 let loremPicsumUrl = chance.loremPicsum({
575 greyscale: true
576 })
577 t.true(_.isString(loremPicsumUrl))
578 t.true(loremPicsumUrl.split('.').length > 1)
579 t.true(loremPicsumUrl.split('://').length > 1)
580 t.true(loremPicsumUrl.split('picsum.photos').length > 1)
581 t.true(loremPicsumUrl.split('/g/500/500').length > 1)
582 t.true(loremPicsumUrl.split('/?random').length > 1)
583 })
584})
585test('loremPicsum() returns loremPicsum url that respects blurred', t => {
586 _.times(1000, () => {
587 let loremPicsumUrl = chance.loremPicsum({
588 blurred: true
589 })
590 t.true(_.isString(loremPicsumUrl))
591 t.true(loremPicsumUrl.split('.').length > 1)
592 t.true(loremPicsumUrl.split('://').length > 1)
593 t.true(loremPicsumUrl.split('picsum.photos').length > 1)
594 t.true(loremPicsumUrl.split('/500/500').length > 1)
595 t.true(loremPicsumUrl.split('/?blur').length > 1)
596 })
597})