aboutsummaryrefslogtreecommitdiff
path: root/test/modules/date.spec.ts
diff options
context:
space:
mode:
authorSamuel Koch <[email protected]>2023-11-13 05:07:05 -0300
committerGitHub <[email protected]>2023-11-13 09:07:05 +0100
commit7ce8c285cb5b2d13e95a17be78ecbd52f3c7d9db (patch)
tree4c541c1d86ff332c54b2ec0d93a541d05bc6db44 /test/modules/date.spec.ts
parentef965da48a8089e6bb19bcf260bfcd8af1a43799 (diff)
downloadfaker-7ce8c285cb5b2d13e95a17be78ecbd52f3c7d9db.tar.xz
faker-7ce8c285cb5b2d13e95a17be78ecbd52f3c7d9db.zip
fix(date): ensures correct range for birthdate (#2535)
Diffstat (limited to 'test/modules/date.spec.ts')
-rw-r--r--test/modules/date.spec.ts29
1 files changed, 29 insertions, 0 deletions
diff --git a/test/modules/date.spec.ts b/test/modules/date.spec.ts
index 715d714a..bbd8020d 100644
--- a/test/modules/date.spec.ts
+++ b/test/modules/date.spec.ts
@@ -587,6 +587,35 @@ describe('date', () => {
expect(birthdate.getUTCFullYear()).toBeLessThanOrEqual(max);
});
+ it('returns a random birthdate that is 18+ by default', () => {
+ // Generate the latest possible value => youngest
+ faker.seed(2855577693);
+
+ const refDate = new Date();
+ const birthdate = faker.date.birthdate({ refDate });
+ expect(birthdate).toBeInstanceOf(Date);
+ const value = birthdate.valueOf();
+ const refDateValue = refDate.valueOf();
+ expect(value).toBeLessThanOrEqual(refDateValue);
+ const deltaDate = new Date(refDateValue - value);
+ expect(deltaDate.getUTCFullYear() - 1970).toBeGreaterThanOrEqual(18);
+ });
+
+ it('returns a random birthdate in one year', () => {
+ const min = 1990;
+ const max = 1990;
+
+ const birthdate = faker.date.birthdate({ min, max, mode: 'year' });
+
+ // birthdate is a date object
+ expect(birthdate).toBeInstanceOf(Date);
+ expect(birthdate.toISOString()).not.toMatch(/T00:00:00.000Z/);
+
+ // Generated date is between min and max
+ expect(birthdate.getUTCFullYear()).toBeGreaterThanOrEqual(min);
+ expect(birthdate.getUTCFullYear()).toBeLessThanOrEqual(max);
+ });
+
it('returns a random birthdate between two ages', () => {
const min = 4;
const max = 5;