Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(date): ensures correct range for birthdate #2535

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/modules/date/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand Down
12 changes: 6 additions & 6 deletions test/modules/__snapshots__/date.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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`;

Expand Down Expand Up @@ -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`;

Expand Down Expand Up @@ -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`;

Expand Down
29 changes: 29 additions & 0 deletions test/modules/date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down