@@ -33,6 +33,15 @@ const seededRuns = [
33
33
context : 'Tuesday' ,
34
34
abbr_context : 'Tue' ,
35
35
} ,
36
+ birthdate : {
37
+ noArgs : new Date ( '1943-08-06T10:03:17.283Z' ) ,
38
+ ageMode : new Date ( '1942-09-15T09:55:20.478Z' ) ,
39
+ ageRange : new Date ( '1941-12-15T14:59:26.122Z' ) ,
40
+ age : new Date ( '1959-06-26T13:52:19.442Z' ) ,
41
+ yearMode : new Date ( '1943-08-06T10:03:17.283Z' ) ,
42
+ yearRange : new Date ( '1937-10-30T15:52:07.381Z' ) ,
43
+ year : new Date ( '2000-05-16T22:59:36.513Z' ) ,
44
+ } ,
36
45
} ,
37
46
} ,
38
47
{
@@ -66,6 +75,15 @@ const seededRuns = [
66
75
context : 'Monday' ,
67
76
abbr_context : 'Mon' ,
68
77
} ,
78
+ birthdate : {
79
+ noArgs : new Date ( '1936-07-04T15:55:47.989Z' ) ,
80
+ ageMode : new Date ( '1935-08-14T07:41:47.183Z' ) ,
81
+ ageRange : new Date ( '1935-02-03T18:44:07.874Z' ) ,
82
+ age : new Date ( '1959-05-16T12:14:12.585Z' ) ,
83
+ yearMode : new Date ( '1936-07-04T15:55:47.989Z' ) ,
84
+ yearRange : new Date ( '1926-06-20T07:18:05.539Z' ) ,
85
+ year : new Date ( '2000-04-06T02:45:32.324Z' ) ,
86
+ } ,
69
87
} ,
70
88
} ,
71
89
{
@@ -99,6 +117,15 @@ const seededRuns = [
99
117
context : 'Saturday' ,
100
118
abbr_context : 'Sat' ,
101
119
} ,
120
+ birthdate : {
121
+ noArgs : new Date ( '1978-06-29T09:24:02.647Z' ) ,
122
+ ageMode : new Date ( '1977-08-10T01:09:17.468Z' ) ,
123
+ ageRange : new Date ( '1975-10-01T07:11:50.190Z' ) ,
124
+ age : new Date ( '1960-01-14T18:44:13.966Z' ) ,
125
+ yearMode : new Date ( '1978-06-29T09:24:02.647Z' ) ,
126
+ yearRange : new Date ( '1993-10-11T07:44:59.519Z' ) ,
127
+ year : new Date ( '2000-12-04T01:16:03.286Z' ) ,
128
+ } ,
102
129
} ,
103
130
} ,
104
131
] ;
@@ -361,6 +388,92 @@ describe('date', () => {
361
388
expect ( actual ) . toEqual ( expectations . weekday . abbr_context ) ;
362
389
} ) ;
363
390
} ) ;
391
+
392
+ describe ( 'birthdate()' , ( ) => {
393
+ it ( 'should return deterministic value birthdate by default' , ( ) => {
394
+ faker . seed ( seed ) ;
395
+
396
+ const actual = faker . date . birthdate ( {
397
+ refDate : '2000-02-09T20:54:02.397Z' ,
398
+ } ) ;
399
+
400
+ expect ( actual ) . toEqual ( expectations . birthdate . noArgs ) ;
401
+ } ) ;
402
+
403
+ it ( 'should return deterministic value birthdate by age mode ' , ( ) => {
404
+ faker . seed ( seed ) ;
405
+
406
+ const actual = faker . date . birthdate ( {
407
+ mode : 'age' ,
408
+ refDate : '2000-02-09T20:54:02.397Z' ,
409
+ } ) ;
410
+
411
+ expect ( actual ) . toEqual ( expectations . birthdate . ageMode ) ;
412
+ } ) ;
413
+
414
+ it ( 'should return deterministic value birthdate by age range' , ( ) => {
415
+ faker . seed ( seed ) ;
416
+
417
+ const actual = faker . date . birthdate ( {
418
+ min : 20 ,
419
+ max : 80 ,
420
+ mode : 'age' ,
421
+ refDate : '2000-02-09T20:54:02.397Z' ,
422
+ } ) ;
423
+
424
+ expect ( actual ) . toEqual ( expectations . birthdate . ageRange ) ;
425
+ } ) ;
426
+
427
+ it ( 'should return deterministic value birthdate by age' , ( ) => {
428
+ faker . seed ( seed ) ;
429
+
430
+ const actual = faker . date . birthdate ( {
431
+ min : 40 ,
432
+ max : 40 ,
433
+ mode : 'age' ,
434
+ refDate : '2000-02-09T20:54:02.397Z' ,
435
+ } ) ;
436
+
437
+ expect ( actual ) . toEqual ( expectations . birthdate . age ) ;
438
+ } ) ;
439
+
440
+ it ( 'should return deterministic value birthdate by year mode' , ( ) => {
441
+ faker . seed ( seed ) ;
442
+
443
+ const actual = faker . date . birthdate ( {
444
+ mode : 'year' ,
445
+ refDate : '2000-02-09T20:54:02.397Z' ,
446
+ } ) ;
447
+
448
+ expect ( actual ) . toEqual ( expectations . birthdate . yearMode ) ;
449
+ } ) ;
450
+
451
+ it ( 'should return deterministic value birthdate by year range' , ( ) => {
452
+ faker . seed ( seed ) ;
453
+
454
+ const actual = faker . date . birthdate ( {
455
+ min : 1900 ,
456
+ max : 2000 ,
457
+ mode : 'year' ,
458
+ refDate : '2000-02-09T20:54:02.397Z' ,
459
+ } ) ;
460
+
461
+ expect ( actual ) . toEqual ( expectations . birthdate . yearRange ) ;
462
+ } ) ;
463
+
464
+ it ( 'should return deterministic value birthdate by year' , ( ) => {
465
+ faker . seed ( seed ) ;
466
+
467
+ const actual = faker . date . birthdate ( {
468
+ min : 2000 ,
469
+ max : 2000 ,
470
+ mode : 'year' ,
471
+ refDate : '2000-02-09T20:54:02.397Z' ,
472
+ } ) ;
473
+
474
+ expect ( actual ) . toEqual ( expectations . birthdate . year ) ;
475
+ } ) ;
476
+ } ) ;
364
477
} ) ;
365
478
}
366
479
@@ -612,6 +725,45 @@ describe('date', () => {
612
725
faker . definitions . date . weekday . abbr_context = backup_abbr_context ;
613
726
} ) ;
614
727
} ) ;
728
+
729
+ describe ( 'birthdate' , ( ) => {
730
+ it ( 'returns a random birthdate' , ( ) => {
731
+ const birthdate = faker . date . birthdate ( ) ;
732
+ expect ( birthdate ) . toBeInstanceOf ( Date ) ;
733
+ } ) ;
734
+
735
+ it ( 'returns a random birthdate between two years' , ( ) => {
736
+ const min = 1990 ;
737
+ const max = 2000 ;
738
+
739
+ const birthdate = faker . date . birthdate ( { min, max, mode : 'year' } ) ;
740
+
741
+ // birthdate is a date object
742
+ expect ( birthdate ) . toBeInstanceOf ( Date ) ;
743
+
744
+ // Generated date is between min and max
745
+ expect ( birthdate . getUTCFullYear ( ) ) . toBeGreaterThanOrEqual ( min ) ;
746
+ expect ( birthdate . getUTCFullYear ( ) ) . toBeLessThanOrEqual ( max ) ;
747
+ } ) ;
748
+
749
+ it ( 'returns a random birthdate between two ages' , ( ) => {
750
+ const min = 4 ;
751
+ const max = 5 ;
752
+
753
+ const birthdate = faker . date . birthdate ( { min, max, mode : 'age' } ) ;
754
+
755
+ // birthdate is a date object
756
+ expect ( birthdate ) . toBeInstanceOf ( Date ) ;
757
+
758
+ // Generated date is between min and max
759
+ expect ( birthdate . getUTCFullYear ( ) ) . toBeGreaterThanOrEqual (
760
+ new Date ( ) . getUTCFullYear ( ) - max - 1
761
+ ) ;
762
+ expect ( birthdate . getUTCFullYear ( ) ) . toBeLessThanOrEqual (
763
+ new Date ( ) . getUTCFullYear ( ) - min
764
+ ) ;
765
+ } ) ;
766
+ } ) ;
615
767
}
616
768
} ) ;
617
769
} ) ;
0 commit comments