diff options
| author | Samuel Koch <[email protected]> | 2023-11-13 05:07:05 -0300 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-11-13 09:07:05 +0100 |
| commit | 7ce8c285cb5b2d13e95a17be78ecbd52f3c7d9db (patch) | |
| tree | 4c541c1d86ff332c54b2ec0d93a541d05bc6db44 | |
| parent | ef965da48a8089e6bb19bcf260bfcd8af1a43799 (diff) | |
| download | faker-7ce8c285cb5b2d13e95a17be78ecbd52f3c7d9db.tar.xz faker-7ce8c285cb5b2d13e95a17be78ecbd52f3c7d9db.zip | |
fix(date): ensures correct range for birthdate (#2535)
| -rw-r--r-- | src/modules/date/index.ts | 2 | ||||
| -rw-r--r-- | test/modules/__snapshots__/date.spec.ts.snap | 12 | ||||
| -rw-r--r-- | test/modules/date.spec.ts | 29 |
3 files changed, 36 insertions, 7 deletions
diff --git a/src/modules/date/index.ts b/src/modules/date/index.ts index 69854688..d79da51f 100644 --- a/src/modules/date/index.ts +++ b/src/modules/date/index.ts @@ -890,7 +890,7 @@ export class SimpleDateModule extends SimpleModuleBase { options.min ?? refYear - 80 ); max = new Date(Date.UTC(0, 11, 30)).setUTCFullYear( - options.max ?? refYear - 18 + options.max ?? refYear - 19 ); } diff --git a/test/modules/__snapshots__/date.spec.ts.snap b/test/modules/__snapshots__/date.spec.ts.snap index 701c8cc7..4ade2cc7 100644 --- a/test/modules/__snapshots__/date.spec.ts.snap +++ b/test/modules/__snapshots__/date.spec.ts.snap @@ -71,11 +71,11 @@ exports[`date > 42 > birthdate > with age mode and refDate 1`] = `1963-09-27T06: exports[`date > 42 > birthdate > with age range and refDate 1`] = `1962-12-27T20:13:59.702Z`; -exports[`date > 42 > birthdate > with only refDate 1`] = `1964-08-06T01:03:57.017Z`; +exports[`date > 42 > birthdate > with only refDate 1`] = `1964-03-22T08:05:39.972Z`; exports[`date > 42 > birthdate > with year and refDate 1`] = `0020-07-07T19:06:53.022Z`; -exports[`date > 42 > birthdate > with year mode and refDate 1`] = `1964-08-06T01:03:57.017Z`; +exports[`date > 42 > birthdate > with year mode and refDate 1`] = `1964-03-22T08:05:39.972Z`; exports[`date > 42 > birthdate > with year range and refDate 1`] = `0057-12-20T11:59:23.890Z`; @@ -199,11 +199,11 @@ exports[`date > 1211 > birthdate > with age mode and refDate 1`] = `1998-08-21T2 exports[`date > 1211 > birthdate > with age range and refDate 1`] = `1996-10-13T01:44:07.645Z`; -exports[`date > 1211 > birthdate > with only refDate 1`] = `1999-06-29T11:06:58.506Z`; +exports[`date > 1211 > birthdate > with only refDate 1`] = `1998-07-25T13:16:46.938Z`; exports[`date > 1211 > birthdate > with year and refDate 1`] = `0021-01-26T13:16:31.421Z`; -exports[`date > 1211 > birthdate > with year mode and refDate 1`] = `1999-06-29T11:06:58.506Z`; +exports[`date > 1211 > birthdate > with year mode and refDate 1`] = `1998-07-25T13:16:46.938Z`; exports[`date > 1211 > birthdate > with year range and refDate 1`] = `0113-12-03T19:45:27.654Z`; @@ -325,11 +325,11 @@ exports[`date > 1337 > birthdate > with age mode and refDate 1`] = `1956-08-25T0 exports[`date > 1337 > birthdate > with age range and refDate 1`] = `1956-02-15T21:16:40.120Z`; -exports[`date > 1337 > birthdate > with only refDate 1`] = `1957-07-05T09:38:29.057Z`; +exports[`date > 1337 > birthdate > with only refDate 1`] = `1957-03-31T18:18:18.869Z`; exports[`date > 1337 > birthdate > with year and refDate 1`] = `0020-05-27T14:46:44.831Z`; -exports[`date > 1337 > birthdate > with year mode and refDate 1`] = `1957-07-05T09:38:29.057Z`; +exports[`date > 1337 > birthdate > with year mode and refDate 1`] = `1957-03-31T18:18:18.869Z`; exports[`date > 1337 > birthdate > with year range and refDate 1`] = `0046-08-09T19:19:18.047Z`; 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; |
