Skip to content

Commit

Permalink
test(date): additional birthday tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Nov 9, 2023
1 parent fa26a44 commit 022fa54
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
25 changes: 11 additions & 14 deletions src/modules/date/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -864,37 +864,34 @@ export class SimpleDateModule extends SimpleModuleBase {
refDate?: string | Date | number;
} = {}
): Date {
if (options.max < options.min) {
const { min, max, mode = 'year' } = options;

if (max < min) {
throw new FakerError(
`Max ${options.max} should be larger than or equal to min ${options.min}.`
`Max ${max} should be larger than or equal to min ${min}.`
);
}

const mode = options.mode === 'age' ? 'age' : 'year';
const refDate = toDate(options.refDate, this.faker.defaultRefDate);
const refYear = refDate.getUTCFullYear();

// If no min or max is specified, generate a random date between (now - 80) years and (now - 18) years respectively
// So that people can still be considered as adults in most cases

// Convert to epoch timestamps
let min: number;
let max: number;
let start: number;
let end: number;
if (mode === 'age') {
min = new Date(refDate).setUTCFullYear(refYear - (options.max ?? 80) - 1);
max = new Date(refDate).setUTCFullYear(refYear - (options.min ?? 18));
start = new Date(refDate).setUTCFullYear(refYear - (max ?? 80) - 1);
end = new Date(refDate).setUTCFullYear(refYear - (min ?? 18));
} else {
// Avoid generating dates the first and last date of the year
// to avoid running into other years depending on the timezone.
min = new Date(Date.UTC(0, 0, 2)).setUTCFullYear(
options.min ?? refYear - 80
);
max = new Date(Date.UTC(0, 11, 30)).setUTCFullYear(
options.max ?? refYear - 18
);
start = Date.UTC(min ?? refYear - 80, 0, 2);
end = Date.UTC(max ?? refYear - 18, 11, 30);
}

return new Date(this.faker.number.int({ min, max }));
return new Date(this.faker.number.int({ min: start, max: end }));
}
}

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 @@ -573,6 +573,35 @@ describe('date', () => {
expect(birthdate).toBeInstanceOf(Date);
});

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 years', () => {
const min = 1990;
const max = 2000;
Expand Down

0 comments on commit 022fa54

Please sign in to comment.