UNPKG

15.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
90// chance.counties()
91test('counties() returns an array of counties', t => {
92 t.true(_.isArray(chance.counties()))
93})
94
95test('counties() returns a (long) county name', t => {
96 _.times(1000, () => t.true(chance.counties({ full: true }).length > 2))
97})
98
99test('counties() can return a random (long) county name', t => {
100 _.times(1000, () => {
101 t.true(chance.counties({ full: true, country: 'uk' }).length > 2)
102 })
103})
104
105// chance.countries()
106test('countries() returns an array of countries', t => {
107 t.true(_.isArray(chance.countries()))
108})
109
110// chance.country()
111test('country() returns a random (short) country name', t => {
112 _.times(1000, () => {
113 t.is(chance.country().length, 2)
114 })
115})
116
117test('country() returns a random (long) country name', t => {
118 _.times(1000, () => {
119 t.true(chance.country({ full: true }).length > 2)
120 })
121})
122
123// chance.county()
124test('county() returns a random county name', t => {
125 _.times(1000, () => {
126 t.true(_.isString(chance.county()))
127 })
128})
129
130test('country() returns a random (long) country name', t => {
131 _.times(1000, () => {
132 t.true(chance.country({ full: true }).length > 2)
133 })
134})
135
136// chance.depth()
137test('depth() looks right', t => {
138 t.is(typeof chance.depth(), 'number')
139})
140
141test('depth() is in the right range', t => {
142 _.times(1000, () => {
143 let depth = chance.depth()
144 t.true(depth > -10994)
145 t.true(depth < 0)
146 })
147})
148
149test('depth() will accept a min and obey it', t => {
150 _.times(1000, () => {
151 let min = chance.floating({ min: -10994, max: 0 })
152 let depth = chance.depth({ min: min })
153 t.true(depth > min)
154 t.true(depth < 0)
155 })
156})
157
158test('depth() will accept a max and obey it', t => {
159 _.times(1000, () => {
160 let max = chance.floating({ min: -10994, max: 0 })
161 let depth = chance.depth({ max: max })
162 t.true(depth > -10994)
163 t.true(depth < max)
164 })
165})
166
167// chance.geohash
168test('geohash() looks right', t => {
169 let geohash = chance.geohash()
170 t.true(_.isString(geohash))
171 t.is(geohash.length, 7)
172})
173
174test('geohash() will accept a length and obey it', t => {
175 _.times(1000, () => {
176 let length = chance.d10()
177 let geohash = chance.geohash({ length: length })
178 t.is(geohash.length, length)
179 })
180})
181
182// chance.latitude()
183test('latitude() looks right', t => {
184 t.is(typeof chance.latitude(), 'number')
185})
186
187test('latitude() is in the right range', t => {
188 _.times(1000, () => {
189 let latitude = chance.latitude()
190 t.true(latitude >= -90)
191 t.true(latitude <= 90)
192 })
193})
194
195test('latitude() will accept a min and obey it', t => {
196 _.times(1000, () => {
197 let min = chance.floating({ min: -90, max: 90 })
198 let latitude = chance.latitude({ min: min })
199 t.true(latitude >= min)
200 t.true(latitude <= 90)
201 })
202})
203
204test('latitude() will accept a max and obey it', t => {
205 _.times(1000, () => {
206 let max = chance.floating({ min: -90, max: 90 })
207 let latitude = chance.latitude({ max: max })
208 t.true(latitude >= -90)
209 t.true(latitude <= max)
210 })
211})
212
213// chance.longitude()
214test('longitude() looks right', t => {
215 t.is(typeof chance.longitude(), 'number')
216})
217
218test('longitude() is in the right range', t => {
219 _.times(1000, () => {
220 let longitude = chance.longitude()
221 t.true(longitude >= -180)
222 t.true(longitude <= 180)
223 })
224})
225
226test('longitude() will accept a min and obey it', t => {
227 _.times(1000, () => {
228 let min = chance.floating({ min: -180, max: 180 })
229 let longitude = chance.longitude({ min: min })
230 t.true(longitude >= min)
231 t.true(longitude <= 180)
232 })
233})
234
235test('longitude() will accept a max and obey it', t => {
236 _.times(1000, () => {
237 let max = chance.floating({ min: -180, max: 180 })
238 let longitude = chance.longitude({ max: max })
239 t.true(longitude >= -180)
240 t.true(longitude <= max)
241 })
242})
243
244// chance.phone()
245test('phone() returns a string', t => {
246 t.true(_.isString(chance.phone()))
247})
248
249test('phone() looks like an actual phone number', t => {
250 t.true(/^\(([2-9][0-8][0-9])\)?[\-. ]?([2-9][0-9]{2,2})[\-. ]?([0-9]{4,4})$/.test(chance.phone()))
251})
252
253test('phone() obeys formatted option', t => {
254 _.times(1000, () => {
255 let phone = chance.phone({ formatted: false })
256 t.true(_.isString(phone))
257 t.true(/^[2-9][0-8]\d[2-9]\d{6,6}$/.test(phone))
258 })
259})
260
261test('phone() obeys formatted option and parens option', t => {
262 _.times(1000, () => {
263 let phone = chance.phone({ formatted: false, parens: true })
264 t.true(_.isString(phone))
265 t.true(/^[2-9][0-8]\d[2-9]\d{6,6}$/.test(phone))
266 })
267})
268
269test('phone() with uk option works', t => {
270 t.true(_.isString(chance.phone({ country: 'uk' })))
271})
272
273test('phone() with uk option works and mobile option', t => {
274 t.true(_.isString(chance.phone({ country: 'uk', mobile: true })))
275})
276
277test('phone() with uk country looks right', t => {
278 t.true(phoneNumber.isValid(chance.phone({ country: 'uk' })))
279})
280
281test('phone() with uk country unformatted looks right', t => {
282 t.true(phoneNumber.isValid(phoneNumber.format(chance.phone({
283 country: 'uk',
284 formatted: false
285 }))))
286})
287
288test('phone() with uk country and mobile option looks right', t => {
289 _.times(1000, () => {
290 t.true(phoneNumber.isValid(chance.phone({
291 country: 'uk',
292 mobile: true
293 })))
294 })
295})
296
297test('phone() with uk country and mobile option unformatted looks right', t => {
298 _.times(1000, () => {
299 t.true(phoneNumber.isValid(phoneNumber.format(chance.phone({
300 country: 'uk',
301 mobile: true,
302 formatted: false
303 }))))
304 })
305})
306
307test('phone() with fr country works', t => {
308 t.true(_.isString(chance.phone({ country: 'fr' })))
309})
310
311test('phone() with fr country works with mobile option', t => {
312 t.true(_.isString(chance.phone({ country: 'fr', mobile: true })))
313})
314
315test('phone() with fr country looks right', t => {
316 _.times(1000, () => {
317 t.true(/0[123459] .. .. .. ../.test(chance.phone({ country: 'fr' })))
318 })
319})
320
321test('phone() with fr country looks right unformatted', t => {
322 _.times(1000, () => {
323 t.true(/0........./.test(chance.phone({
324 country: 'fr',
325 formatted: false
326 })))
327 })
328})
329
330test('phone() with fr country on mobile looks right', t => {
331 _.times(1000, () => {
332 t.true(/0[67] .. .. .. ../.test(chance.phone({
333 country: 'fr',
334 mobile: true
335 })))
336 })
337})
338
339test('phone() with fr country on mobile, unformatted looks right', t => {
340 _.times(1000, () => {
341 t.true(/0[67]......../.test(chance.phone({
342 country: 'fr',
343 mobile: true,
344 formatted: false
345 })))
346 })
347})
348
349test('phone() with br country option works', t => {
350 t.true(_.isString(chance.phone({ country: 'br' })))
351})
352
353test('phone() with br country and mobile option works', t => {
354 t.true(_.isString(chance.phone({ country: 'br', mobile: true })))
355})
356
357test('phone() with br country and formatted false option return a correct format', t => {
358 t.true(/([0-9]{2})([2-5]{1})([0-9]{3})([0-9]{4})/.test(chance.phone({
359 country: 'br',
360 mobile: false,
361 formatted: false
362 })))
363})
364
365test('phone() with br country, formatted false and mobile option return a correct format', t => {
366 t.true(/([0-9]{2})\9([0-9]{4})([0-9]{4})/.test(chance.phone({
367 country: 'br',
368 mobile: true,
369 formatted: false
370 })))
371})
372
373test('phone() with br country and formatted option apply the correct mask', t => {
374 t.true(/\(([0-9]{2})\) ([2-5]{1})([0-9]{3})\-([0-9]{4})/.test(chance.phone({
375 country: 'br',
376 mobile: false,
377 formatted: true
378 })))
379})
380
381test('phone() with br country, formatted and mobile option apply the correct mask', t => {
382 t.true(/\(([0-9]{2})\) 9([0-9]{4})\-([0-9]{4})/.test(chance.phone({
383 country: 'br',
384 mobile: true,
385 formatted: true
386 })))
387})
388
389// chance.postal()
390test('postal() returns a valid basic postal code', t => {
391 _.times(1000, () => {
392 let postal = chance.postal()
393 t.is(postal.length, 7)
394 postal.split('').map((char) => {
395 t.is(char.toUpperCase(), char)
396 })
397 })
398})
399
400// chance.province()
401test('province() returns a random (short) province name', t => {
402 _.times(1000, () => t.true(chance.province().length < 3))
403})
404
405test('province() can return a long random province name', t => {
406 _.times(1000, () => t.true(chance.province({ full: true }).length > 2))
407})
408
409test('province() can return a random long "it" province', t => {
410 _.times(1000, () => {
411 t.true(chance.province({country: 'it', full: true }).length > 2)
412 })
413})
414
415// chance.provinces()
416test('provinces() returns an array of provinces', t => {
417 t.true(_.isArray(chance.provinces()))
418})
419
420test('provinces() supports internationalization', t => {
421 t.not(chance.provinces(), chance.provinces({ country: 'it' }))
422})
423
424// chance.state()
425test('state() returns a random (short) state name', t => {
426 _.times(1000, () => t.true(chance.state().length < 3))
427})
428
429test('state() can take a country and return a state', t => {
430 _.times(1000, () => t.true(chance.state({ country: 'it' }).length === 3))
431})
432
433test('state() can return full state name', t => {
434 _.times(1000, () => {
435 t.true(chance.state({
436 full: true
437 }).length > 2)
438 })
439})
440
441test('state() with country returns a long state name', t => {
442 _.times(1000, () => {
443 t.true(chance.state({
444 country: 'it',
445 full: true
446 }).length > 2)
447 })
448 _.times(1000, () => {
449 t.true(chance.state({
450 country: 'uk',
451 full: true
452 }).length > 2)
453 })
454})
455
456// chance.states()
457test('states() returns an array of states', t => {
458 t.true(_.isArray(chance.states()))
459})
460
461test('states() returns all 50 states and DC', t => {
462 t.is(chance.states().length, 51)
463})
464
465test('states() with territories returns 50 US states, DC, and 7 US territories', t => {
466 t.is(chance.states({
467 territories: true
468 }).length, 58)
469})
470
471test('states() without us states and dc returns 7 US territories', t => {
472 t.is(chance.states({
473 territories: true,
474 us_states_and_dc: false
475 }).length, 7)
476})
477
478test('states() with armed forces returns 50 states, DC, and 3 armed forces military states', t => {
479 t.is(chance.states({
480 armed_forces: true
481 }).length, 54)
482})
483
484test('states() with armed forces without states returns 3 armed forces states', t => {
485 t.is(chance.states({
486 armed_forces: true,
487 us_states_and_dc: false
488 }).length, 3)
489})
490
491test('states() with all options returns 61 items', t => {
492 t.is(chance.states({
493 territories: true,
494 armed_forces: true
495 }).length, 61)
496})
497
498test('states() without states returns 7 territories and 3 armed forces states', t => {
499 t.is(chance.states({
500 territories: true,
501 armed_forces: true,
502 us_states_and_dc: false
503 }).length, 10)
504})
505
506test('states() with country of "it" returns 20 regions', t => {
507 t.is(chance.states({
508 country: 'it'
509 }).length, 20)
510})
511
512test('states() with country of "uk" returns 129 UK counties', t => {
513 t.is(chance.states({
514 country: 'uk'
515 }).length, 129)
516})
517
518// chance.street()
519test('street() works', t => {
520 _.times(100, () => t.is(typeof chance.street(), 'string'))
521})
522
523test('street() works with it country', t => {
524 _.times(100, () => t.is(typeof chance.street({ country: 'it' }), 'string'))
525})
526
527// chance.street_suffix()
528test('street_suffix() returns a single suffix', t => {
529 _.times(1000, () => {
530 let suffix = chance.street_suffix()
531 t.is(typeof suffix, 'object')
532 t.is(typeof suffix.name, 'string')
533 t.is(typeof suffix.abbreviation, 'string')
534 })
535})
536
537// chance.street_suffixes()
538test('street_suffixes() returns the suffix array', t => {
539 let suffixes = chance.street_suffixes()
540 t.true(_.isArray(suffixes))
541 suffixes.map((suffix) => {
542 t.truthy(suffix.name)
543 t.truthy(suffix.abbreviation)
544 })
545})
546
547test('street_suffixes() are short', t => {
548 let suffixes = chance.street_suffixes()
549 suffixes.map((suffix) => {
550 t.true(suffix.abbreviation.length < 5)
551 })
552})
553
554test('street_suffixes() are longish', t => {
555 let suffixes = chance.street_suffixes()
556 suffixes.map((suffix) => {
557 t.true(suffix.name.length > 2)
558 })
559})
560
561// chance.zip()
562test('zip() returns a valid basic zip code', t => {
563 _.times(1000, () => {
564 let zip = chance.zip()
565 t.is(zip.length, 5)
566 t.true(/(^\d{5}$)|(^\d{5}-\d{4}$)/.test(zip))
567 })
568})
569
570test('zip() returns a valid zip+4 code', t => {
571 _.times(1000, () => {
572 let zip = chance.zip({ plusfour: true })
573 t.is(zip.length, 10)
574 t.true(/(^\d{5}$)|(^\d{5}-\d{4}$)/.test(zip))
575 })
576})