From 022fa540106e25818936c6ebfe8d10fe9ee15b30 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Thu, 9 Nov 2023 23:05:38 +0100 Subject: [PATCH] test(date): additional birthday tests --- src/modules/date/index.ts | 25 +++++++++++-------------- test/modules/date.spec.ts | 29 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/modules/date/index.ts b/src/modules/date/index.ts index 69854688a5b..762aac849e9 100644 --- a/src/modules/date/index.ts +++ b/src/modules/date/index.ts @@ -864,13 +864,14 @@ 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(); @@ -878,23 +879,19 @@ export class SimpleDateModule extends SimpleModuleBase { // 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 })); } } diff --git a/test/modules/date.spec.ts b/test/modules/date.spec.ts index 715d714af5d..6179a5ce549 100644 --- a/test/modules/date.spec.ts +++ b/test/modules/date.spec.ts @@ -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;