UNPKG

21.4 kBJavaScriptView Raw
1import test from 'ava'
2import Chance from '../chance.js'
3import _ from 'lodash'
4import phoneNumber from './helpers/phoneNumber.min.js'
5
6const chance = new Chance()
7
8// chance.address()
9test('address() returns a string', t => {
10 t.true(_.isString(chance.address()))
11})
12
13test('address() starts with a number', t => {
14 _.times(1000, () => t.true(/[0-9]+.+/.test(chance.address())))
15})
16
17test('address() can take a short_suffix arg and obey it', t => {
18 _.times(1000, () => {
19 let address = chance.address({ short_suffix: true })
20 t.true(address.split(' ')[2].length < 5)
21 })
22})
23
24// chance.altitude()
25test('altitude() looks right', t => {
26 t.is(typeof chance.altitude(), 'number')
27})
28
29test('altitude() is in the right range', t => {
30 _.times(1000, () => {
31 let altitude = chance.altitude()
32 t.true(altitude > 0)
33 t.true(altitude < 8848)
34 })
35})
36
37test('altitude() will accept a min and obey it', t => {
38 _.times(1000, () => {
39 let min = chance.floating({ min: 0, max: 8848 })
40 let altitude = chance.altitude({ min: min })
41 t.true(altitude > min)
42 t.true(altitude < 8848)
43 })
44})
45
46test('altitude() will accept a max and obey it', t => {
47 _.times(1000, () => {
48 let max = chance.floating({ min: 0, max: 8848 })
49 let altitude = chance.altitude({ max: max })
50 t.true(altitude > 0)
51 t.true(altitude < max)
52 })
53})
54
55// chance.areacode()
56test('areacode() looks right', t => {
57 _.times(1000, () => {
58 let areacode = chance.areacode()
59 t.true(_.isString(areacode))
60 t.true(/^\(([2-9][0-8][0-9])\)$/.test(areacode))
61 })
62})
63
64test('areacode() can take parens', t => {
65 _.times(1000, () => {
66 let areacode = chance.areacode({ parens: false })
67 t.true(_.isString(areacode))
68 t.true(/^([2-9][0-8][0-9])$/.test(areacode))
69 })
70})
71
72// chance.city()
73test('city() looks right', t => {
74 _.times(1000, () => {
75 let city = chance.city()
76 t.true(_.isString(city))
77 t.true(/[a-zA-Z]+/.test(city))
78 })
79})
80
81// chance.coordinates()
82test('coordinates() looks right', t => {
83 _.times(1000, () => {
84 let coordinates = chance.coordinates()
85 t.true(_.isString(coordinates))
86 t.is(coordinates.split(',').length, 2)
87 })
88})
89
90test('coordinates() returns coordinates in DD format as default', t => {
91 _.times(1000, () => {
92 const CHARS_NOT_TO_CONTAIN = ['°', '’', '”']
93
94 let coordinates = chance.coordinates()
95 let [ latitude, longitude ] = coordinates.split(',')
96
97 t.true(_.isString(coordinates))
98 t.is(coordinates.split(',').length, 2)
99 t.true(CHARS_NOT_TO_CONTAIN.every(char => !latitude.includes(char)))
100 t.true(CHARS_NOT_TO_CONTAIN.every(char => !longitude.includes(char)))
101 })
102})
103
104test('coordinates() will obey DD format', t => {
105 _.times(1000, () => {
106 const CHARS_NOT_TO_CONTAIN = ['°', '’', '”']
107
108 let coordinates = chance.coordinates({format: 'dd'})
109 let [ latitude, longitude ] = coordinates.split(',')
110
111 t.true(_.isString(coordinates))
112 t.is(coordinates.split(',').length, 2)
113 t.true(CHARS_NOT_TO_CONTAIN.every(char => !latitude.includes(char)))
114 t.true(CHARS_NOT_TO_CONTAIN.every(char => !longitude.includes(char)))
115 })
116})
117
118test('coordinates() will obey DDM format', t => {
119 _.times(1000, () => {
120 const CHARS_TO_CONTAIN = ['°']
121 const CHARS_NOT_TO_CONTAIN = ['’', '”']
122
123 let coordinates = chance.coordinates({format: 'ddm'})
124 let [ latitude, longitude ] = coordinates.split(',')
125
126 t.true(_.isString(coordinates))
127 t.is(coordinates.split(',').length, 2)
128 t.true(CHARS_TO_CONTAIN.every(char => latitude.includes(char)))
129 t.true(CHARS_TO_CONTAIN.every(char => longitude.includes(char)))
130 t.true(CHARS_NOT_TO_CONTAIN.every(char => !latitude.includes(char)))
131 t.true(CHARS_NOT_TO_CONTAIN.every(char => !longitude.includes(char)))
132 })
133})
134
135test('coordinates() will obey DMS format', t => {
136 _.times(1000, () => {
137 const CHARS_TO_CONTAIN = ['°', '’', '”']
138
139 let coordinates = chance.coordinates({format: 'dms'})
140 let [ latitude, longitude ] = coordinates.split(',')
141
142 t.true(_.isString(coordinates))
143 t.is(coordinates.split(',').length, 2)
144 t.true(CHARS_TO_CONTAIN.every(char => latitude.includes(char)))
145 t.true(CHARS_TO_CONTAIN.every(char => longitude.includes(char)))
146 })
147})
148
149// chance.counties()
150test('counties() returns an array of counties', t => {
151 t.true(_.isArray(chance.counties()))
152})
153
154test('counties() returns a (long) county name', t => {
155 _.times(1000, () => t.true(chance.counties({ full: true }).length > 2))
156})
157
158test('counties() can return a random (long) county name', t => {
159 _.times(1000, () => {
160 t.true(chance.counties({ full: true, country: 'uk' }).length > 2)
161 })
162})
163
164// chance.countries()
165test('countries() returns an array of countries', t => {
166 t.true(_.isArray(chance.countries()))
167})
168
169// chance.country()
170test('country() returns a random (short) country name', t => {
171 _.times(1000, () => {
172 t.is(chance.country().length, 2)
173 })
174})
175
176test('country() returns a random (long) country name', t => {
177 _.times(1000, () => {
178 t.true(chance.country({ full: true }).length > 2)
179 })
180})
181
182// chance.county()
183test('county() returns a random county name', t => {
184 _.times(1000, () => {
185 t.true(_.isString(chance.county()))
186 })
187})
188
189test('country() returns a random (long) country name', t => {
190 _.times(1000, () => {
191 t.true(chance.country({ full: true }).length > 2)
192 })
193})
194
195// chance.depth()
196test('depth() looks right', t => {
197 t.is(typeof chance.depth(), 'number')
198})
199
200test('depth() is in the right range', t => {
201 _.times(1000, () => {
202 let depth = chance.depth()
203 t.true(depth > -10994)
204 t.true(depth < 0)
205 })
206})
207
208test('depth() will accept a min and obey it', t => {
209 _.times(1000, () => {
210 let min = chance.floating({ min: -10994, max: 0 })
211 let depth = chance.depth({ min: min })
212 t.true(depth > min)
213 t.true(depth < 0)
214 })
215})
216
217test('depth() will accept a max and obey it', t => {
218 _.times(1000, () => {
219 let max = chance.floating({ min: -10994, max: 0 })
220 let depth = chance.depth({ max: max })
221 t.true(depth > -10994)
222 t.true(depth < max)
223 })
224})
225
226// chance.geohash
227test('geohash() looks right', t => {
228 let geohash = chance.geohash()
229 t.true(_.isString(geohash))
230 t.is(geohash.length, 7)
231})
232
233test('geohash() will accept a length and obey it', t => {
234 _.times(1000, () => {
235 let length = chance.d10()
236 let geohash = chance.geohash({ length: length })
237 t.is(geohash.length, length)
238 })
239})
240
241// chance.latitude()
242test('latitude() looks right', t => {
243 t.is(typeof chance.latitude(), 'number')
244})
245
246test('latitude() is in the right range', t => {
247 _.times(1000, () => {
248 let latitude = chance.latitude()
249 t.true(latitude >= -90)
250 t.true(latitude <= 90)
251 })
252})
253
254test('latitude() will accept a min and obey it', t => {
255 _.times(1000, () => {
256 let min = chance.floating({ min: -90, max: 90 })
257 let latitude = chance.latitude({ min: min })
258 t.true(latitude >= min)
259 t.true(latitude <= 90)
260 })
261})
262
263test('latitude() will accept a max and obey it', t => {
264 _.times(1000, () => {
265 let max = chance.floating({ min: -90, max: 90 })
266 let latitude = chance.latitude({ max: max })
267 t.true(latitude >= -90)
268 t.true(latitude <= max)
269 })
270})
271
272test('latitude() returns latitude in DD format as default', t => {
273 _.times(1000, () => {
274 const CHARS_NOT_TO_CONTAIN = ['°', '’', '”']
275
276 let latitude = chance.latitude()
277
278 t.is(typeof latitude, 'number')
279 t.true(CHARS_NOT_TO_CONTAIN.every(char => !latitude.toString().includes(char)))
280 })
281})
282
283test('latitude() will obey DD format', t => {
284 _.times(1000, () => {
285 const CHARS_NOT_TO_CONTAIN = ['°', '’', '”']
286
287 let latitude = chance.latitude({format: 'dd'})
288
289 t.is(typeof latitude, 'number')
290 t.true(CHARS_NOT_TO_CONTAIN.every(char => !latitude.toString().includes(char)))
291 })
292})
293
294test('latitude() will obey DDM format', t => {
295 _.times(1000, () => {
296 const CHARS_TO_CONTAIN = ['°']
297 const CHARS_NOT_TO_CONTAIN = ['’', '”']
298
299 let latitude = chance.latitude({format: 'ddm'})
300
301 t.true(_.isString(latitude))
302 t.true(CHARS_TO_CONTAIN.every(char => latitude.includes(char)))
303 t.true(CHARS_NOT_TO_CONTAIN.every(char => !latitude.includes(char)))
304 })
305})
306
307test('latitude() will obey DMS format', t => {
308 _.times(1000, () => {
309 const CHARS_TO_CONTAIN = ['°', '’', '”']
310
311 let latitude = chance.latitude({format: 'dms'})
312
313 t.true(_.isString(latitude))
314 t.true(CHARS_TO_CONTAIN.every(char => latitude.includes(char)))
315 })
316})
317
318// chance.longitude()
319test('longitude() looks right', t => {
320 t.is(typeof chance.longitude(), 'number')
321})
322
323test('longitude() is in the right range', t => {
324 _.times(1000, () => {
325 let longitude = chance.longitude()
326 t.true(longitude >= -180)
327 t.true(longitude <= 180)
328 })
329})
330
331test('longitude() will accept a min and obey it', t => {
332 _.times(1000, () => {
333 let min = chance.floating({ min: -180, max: 180 })
334 let longitude = chance.longitude({ min: min })
335 t.true(longitude >= min)
336 t.true(longitude <= 180)
337 })
338})
339
340test('longitude() will accept a max and obey it', t => {
341 _.times(1000, () => {
342 let max = chance.floating({ min: -180, max: 180 })
343 let longitude = chance.longitude({ max: max })
344 t.true(longitude >= -180)
345 t.true(longitude <= max)
346 })
347})
348
349test('longitude() returns longitude in DD format as default', t => {
350 _.times(1000, () => {
351 const CHARS_NOT_TO_CONTAIN = ['°', '’', '”']
352
353 let longitude = chance.longitude()
354
355 t.is(typeof longitude, 'number')
356 t.true(CHARS_NOT_TO_CONTAIN.every(char => !longitude.toString().includes(char)))
357 })
358})
359
360test('longitude() will obey DD format', t => {
361 _.times(1000, () => {
362 const CHARS_NOT_TO_CONTAIN = ['°', '’', '”']
363
364 let longitude = chance.longitude({format: 'dd'})
365
366 t.is(typeof longitude, 'number')
367 t.true(CHARS_NOT_TO_CONTAIN.every(char => !longitude.toString().includes(char)))
368 })
369})
370
371test('longitude() will obey DDM format', t => {
372 _.times(1000, () => {
373 const CHARS_TO_CONTAIN = ['°']
374 const CHARS_NOT_TO_CONTAIN = ['’', '”']
375
376 let longitude = chance.longitude({format: 'ddm'})
377
378 t.true(_.isString(longitude))
379 t.true(CHARS_TO_CONTAIN.every(char => longitude.includes(char)))
380 t.true(CHARS_NOT_TO_CONTAIN.every(char => !longitude.includes(char)))
381 })
382})
383
384test('longitude() will obey DMS format', t => {
385 _.times(1000, () => {
386 const CHARS_TO_CONTAIN = ['°', '’', '”']
387
388 let longitude = chance.longitude({format: 'dms'})
389
390 t.true(_.isString(longitude))
391 t.true(CHARS_TO_CONTAIN.every(char => longitude.includes(char)))
392 })
393})
394
395// chance.phone()
396test('phone() returns a string', t => {
397 t.true(_.isString(chance.phone()))
398})
399
400test('phone() looks like an actual phone number', t => {
401 t.true(/^\(([2-9][0-8][0-9])\)?[\-. ]?([2-9][0-9]{2,2})[\-. ]?([0-9]{4,4})$/.test(chance.phone()))
402})
403
404test('phone() obeys formatted option', t => {
405 _.times(1000, () => {
406 let phone = chance.phone({ formatted: false })
407 t.true(_.isString(phone))
408 t.true(/^[2-9][0-8]\d[2-9]\d{6,6}$/.test(phone))
409 })
410})
411
412test('phone() obeys formatted option and parens option', t => {
413 _.times(1000, () => {
414 let phone = chance.phone({ formatted: false, parens: true })
415 t.true(_.isString(phone))
416 t.true(/^[2-9][0-8]\d[2-9]\d{6,6}$/.test(phone))
417 })
418})
419
420test("phone() obeys exampleNumber option", (t) => {
421 _.times(1000, () => {
422 let phone = chance.phone({ exampleNumber: true });
423 t.true(_.isString(phone));
424 t.true(/^\(555\)?[\-. ]?([2-9][0-9]{2,2})[\-. ]?([0-9]{4,4})$/.test(phone));
425 });
426});
427
428test("phone() obeys formatted option and exampleNumber option", (t) => {
429 _.times(1000, () => {
430 let phone = chance.phone({ exampleNumber: true, formatted: false });
431 t.true(_.isString(phone));
432 t.true(/^555[2-9]\d{6,6}$/.test(phone));
433 });
434});
435
436test('phone() with uk option works', t => {
437 t.true(_.isString(chance.phone({ country: 'uk' })))
438})
439
440test('phone() with uk option works and mobile option', t => {
441 t.true(_.isString(chance.phone({ country: 'uk', mobile: true })))
442})
443
444test('phone() with uk country looks right', t => {
445 t.true(phoneNumber.isValid(chance.phone({ country: 'uk' })))
446})
447
448test('phone() with uk country unformatted looks right', t => {
449 t.true(phoneNumber.isValid(phoneNumber.format(chance.phone({
450 country: 'uk',
451 formatted: false
452 }))))
453})
454
455test('phone() with uk country and mobile option looks right', t => {
456 _.times(1000, () => {
457 t.true(phoneNumber.isValid(chance.phone({
458 country: 'uk',
459 mobile: true
460 })))
461 })
462})
463
464test('phone() with uk country and mobile option unformatted looks right', t => {
465 _.times(1000, () => {
466 t.true(phoneNumber.isValid(phoneNumber.format(chance.phone({
467 country: 'uk',
468 mobile: true,
469 formatted: false
470 }))))
471 })
472})
473
474test('phone() with fr country works', t => {
475 t.true(_.isString(chance.phone({ country: 'fr' })))
476})
477
478test('phone() with fr country works with mobile option', t => {
479 t.true(_.isString(chance.phone({ country: 'fr', mobile: true })))
480})
481
482test('phone() with fr country looks right', t => {
483 _.times(1000, () => {
484 t.true(/0[123459] .. .. .. ../.test(chance.phone({ country: 'fr' })))
485 })
486})
487
488test('phone() with fr country looks right unformatted', t => {
489 _.times(1000, () => {
490 t.true(/0........./.test(chance.phone({
491 country: 'fr',
492 formatted: false
493 })))
494 })
495})
496
497test('phone() with fr country on mobile looks right', t => {
498 _.times(1000, () => {
499 t.true(/0[67] .. .. .. ../.test(chance.phone({
500 country: 'fr',
501 mobile: true
502 })))
503 })
504})
505
506test('phone() with fr country on mobile, unformatted looks right', t => {
507 _.times(1000, () => {
508 t.true(/0[67]......../.test(chance.phone({
509 country: 'fr',
510 mobile: true,
511 formatted: false
512 })))
513 })
514})
515
516test('phone() with br country option works', t => {
517 t.true(_.isString(chance.phone({ country: 'br' })))
518})
519
520test('phone() with br country and mobile option works', t => {
521 t.true(_.isString(chance.phone({ country: 'br', mobile: true })))
522})
523
524test('phone() with br country and formatted false option return a correct format', t => {
525 t.true(/([0-9]{2})([2-5]{1})([0-9]{3})([0-9]{4})/.test(chance.phone({
526 country: 'br',
527 mobile: false,
528 formatted: false
529 })))
530})
531
532test('phone() with br country, formatted false and mobile option return a correct format', t => {
533 t.true(/([0-9]{2})\9([0-9]{4})([0-9]{4})/.test(chance.phone({
534 country: 'br',
535 mobile: true,
536 formatted: false
537 })))
538})
539
540test('phone() with br country and formatted option apply the correct mask', t => {
541 t.true(/\(([0-9]{2})\) ([2-5]{1})([0-9]{3})\-([0-9]{4})/.test(chance.phone({
542 country: 'br',
543 mobile: false,
544 formatted: true
545 })))
546})
547
548test('phone() with br country, formatted and mobile option apply the correct mask', t => {
549 t.true(/\(([0-9]{2})\) 9([0-9]{4})\-([0-9]{4})/.test(chance.phone({
550 country: 'br',
551 mobile: true,
552 formatted: true
553 })))
554})
555
556// chance.postal()
557test('postal() returns a valid basic postal code', t => {
558 _.times(1000, () => {
559 let postal = chance.postal()
560 t.is(postal.length, 7)
561 postal.split('').map((char) => {
562 t.is(char.toUpperCase(), char)
563 })
564 })
565})
566
567test('postcode() returns a valid basic postcode', t => {
568 _.times(10, () => {
569 let postcode = chance.postcode();
570 t.regex(postcode, /^[A-Z]{1,2}\d[A-Z\d]? \d[A-Z]{2}$/);
571 })
572})
573
574// chance.province()
575test('province() returns a random (short) province name', t => {
576 _.times(1000, () => t.true(chance.province().length < 3))
577})
578
579test('province() can return a long random province name', t => {
580 _.times(1000, () => t.true(chance.province({ full: true }).length > 2))
581})
582
583test('province() can return a random long "it" province', t => {
584 _.times(1000, () => {
585 t.true(chance.province({country: 'it', full: true }).length > 2)
586 })
587})
588
589// chance.provinces()
590test('provinces() returns an array of provinces', t => {
591 t.true(_.isArray(chance.provinces()))
592})
593
594test('provinces() supports internationalization', t => {
595 t.not(chance.provinces(), chance.provinces({ country: 'it' }))
596})
597
598// chance.state()
599test('state() returns a random (short) state name', t => {
600 _.times(1000, () => t.true(chance.state().length < 3))
601})
602
603test('state() can take a country and return a state', t => {
604 _.times(1000, () => t.true(chance.state({ country: 'it' }).length === 3))
605})
606
607test('state() can return full state name', t => {
608 _.times(1000, () => {
609 t.true(chance.state({
610 full: true
611 }).length > 2)
612 })
613})
614
615test('state() with country returns a long state name', t => {
616 _.times(1000, () => {
617 t.true(chance.state({
618 country: 'it',
619 full: true
620 }).length > 2)
621 })
622 _.times(1000, () => {
623 t.true(chance.state({
624 country: 'uk',
625 full: true
626 }).length > 2)
627 })
628})
629
630// chance.states()
631test('states() returns an array of states', t => {
632 t.true(_.isArray(chance.states()))
633})
634
635test('states() returns all 50 states and DC', t => {
636 t.is(chance.states().length, 51)
637})
638
639test('states() with territories returns 50 US states, DC, and 7 US territories', t => {
640 t.is(chance.states({
641 territories: true
642 }).length, 58)
643})
644
645test('states() without us states and dc returns 7 US territories', t => {
646 t.is(chance.states({
647 territories: true,
648 us_states_and_dc: false
649 }).length, 7)
650})
651
652test('states() with armed forces returns 50 states, DC, and 3 armed forces military states', t => {
653 t.is(chance.states({
654 armed_forces: true
655 }).length, 54)
656})
657
658test('states() with armed forces without states returns 3 armed forces states', t => {
659 t.is(chance.states({
660 armed_forces: true,
661 us_states_and_dc: false
662 }).length, 3)
663})
664
665test('states() with all options returns 61 items', t => {
666 t.is(chance.states({
667 territories: true,
668 armed_forces: true
669 }).length, 61)
670})
671
672test('states() without states returns 7 territories and 3 armed forces states', t => {
673 t.is(chance.states({
674 territories: true,
675 armed_forces: true,
676 us_states_and_dc: false
677 }).length, 10)
678})
679
680test('states() with country of "it" returns 20 regions', t => {
681 t.is(chance.states({
682 country: 'it'
683 }).length, 20)
684})
685
686test('states() with country of "uk" returns 129 UK counties', t => {
687 t.is(chance.states({
688 country: 'uk'
689 }).length, 129)
690})
691
692test('states() with country of "mx" returns 32 MX states', t => {
693 t.is(chance.states({
694 country: 'mx'
695 }).length, 32)
696})
697
698// chance.street()
699test('street() works', t => {
700 _.times(100, () => t.is(typeof chance.street(), 'string'))
701})
702
703test('street() works with it country', t => {
704 _.times(100, () => t.is(typeof chance.street({ country: 'it' }), 'string'))
705})
706
707// chance.street_suffix()
708test('street_suffix() returns a single suffix', t => {
709 _.times(1000, () => {
710 let suffix = chance.street_suffix()
711 t.is(typeof suffix, 'object')
712 t.is(typeof suffix.name, 'string')
713 t.is(typeof suffix.abbreviation, 'string')
714 })
715})
716
717// chance.street_suffixes()
718test('street_suffixes() returns the suffix array', t => {
719 let suffixes = chance.street_suffixes()
720 t.true(_.isArray(suffixes))
721 suffixes.map((suffix) => {
722 t.truthy(suffix.name)
723 t.truthy(suffix.abbreviation)
724 })
725})
726
727test('street_suffixes() are short', t => {
728 let suffixes = chance.street_suffixes()
729 suffixes.map((suffix) => {
730 t.true(suffix.abbreviation.length < 5)
731 })
732})
733
734test('street_suffixes() are longish', t => {
735 let suffixes = chance.street_suffixes()
736 suffixes.map((suffix) => {
737 t.true(suffix.name.length > 2)
738 })
739})
740
741// chance.zip()
742test('zip() returns a valid basic zip code', t => {
743 _.times(1000, () => {
744 let zip = chance.zip()
745 t.is(zip.length, 5)
746 t.true(/(^\d{5}$)|(^\d{5}-\d{4}$)/.test(zip))
747 })
748})
749
750test('zip() returns a valid zip+4 code', t => {
751 _.times(1000, () => {
752 let zip = chance.zip({ plusfour: true })
753 t.is(zip.length, 10)
754 t.true(/(^\d{5}$)|(^\d{5}-\d{4}$)/.test(zip))
755 })
756})