aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorST-DDT <[email protected]>2024-04-01 10:36:36 +0200
committerGitHub <[email protected]>2024-04-01 08:36:36 +0000
commitb498d1f794e6d682e9d9fedebff194664c2a3f1d (patch)
treeac29f61f77af12e1308ce8f954d8016dfcf63894 /test
parent6191a5d883048b694404dbf42527caba395828ea (diff)
downloadfaker-b498d1f794e6d682e9d9fedebff194664c2a3f1d.tar.xz
faker-b498d1f794e6d682e9d9fedebff194664c2a3f1d.zip
refactor(date)!: birthdate improvements (#2756)
Diffstat (limited to 'test')
-rw-r--r--test/modules/__snapshots__/date.spec.ts.snap30
-rw-r--r--test/modules/date.spec.ts115
2 files changed, 78 insertions, 67 deletions
diff --git a/test/modules/__snapshots__/date.spec.ts.snap b/test/modules/__snapshots__/date.spec.ts.snap
index f67a339d..d73d48cd 100644
--- a/test/modules/__snapshots__/date.spec.ts.snap
+++ b/test/modules/__snapshots__/date.spec.ts.snap
@@ -67,17 +67,13 @@ exports[`date > 42 > betweens > with string dates and count 1`] = `
exports[`date > 42 > birthdate > with age and refDate 1`] = `1980-07-07T19:06:53.165Z`;
-exports[`date > 42 > birthdate > with age mode and refDate 1`] = `1963-09-27T06:10:42.813Z`;
-
exports[`date > 42 > birthdate > with age range and refDate 1`] = `1962-12-27T20:14:08.437Z`;
-exports[`date > 42 > birthdate > with only refDate 1`] = `1964-03-22T08:05:48.849Z`;
-
-exports[`date > 42 > birthdate > with year and refDate 1`] = `0020-07-07T19:06:53.165Z`;
+exports[`date > 42 > birthdate > with only refDate 1`] = `1963-09-27T06:10:42.813Z`;
-exports[`date > 42 > birthdate > with year mode and refDate 1`] = `1964-03-22T08:05:48.849Z`;
+exports[`date > 42 > birthdate > with year 1`] = `2000-05-16T22:59:36.655Z`;
-exports[`date > 42 > birthdate > with year range and refDate 1`] = `0057-12-20T11:59:38.353Z`;
+exports[`date > 42 > birthdate > with year range 1`] = `1937-10-30T15:52:21.843Z`;
exports[`date > 42 > future > with only Date refDate 1`] = `2021-07-08T10:07:33.524Z`;
@@ -195,17 +191,13 @@ exports[`date > 1211 > betweens > with string dates and count 1`] = `
exports[`date > 1211 > birthdate > with age and refDate 1`] = `1981-01-26T13:16:31.426Z`;
-exports[`date > 1211 > birthdate > with age mode and refDate 1`] = `1998-08-21T21:24:31.101Z`;
-
exports[`date > 1211 > birthdate > with age range and refDate 1`] = `1996-10-13T01:44:07.954Z`;
-exports[`date > 1211 > birthdate > with only refDate 1`] = `1998-07-25T13:16:47.251Z`;
+exports[`date > 1211 > birthdate > with only refDate 1`] = `1998-08-21T21:24:31.101Z`;
-exports[`date > 1211 > birthdate > with year and refDate 1`] = `0021-01-26T13:16:31.426Z`;
+exports[`date > 1211 > birthdate > with year 1`] = `2000-12-04T01:16:03.291Z`;
-exports[`date > 1211 > birthdate > with year mode and refDate 1`] = `1998-07-25T13:16:47.251Z`;
-
-exports[`date > 1211 > birthdate > with year range and refDate 1`] = `0113-12-03T19:45:28.165Z`;
+exports[`date > 1211 > birthdate > with year range 1`] = `1993-10-11T07:45:00.030Z`;
exports[`date > 1211 > future > with only Date refDate 1`] = `2022-01-26T14:59:27.356Z`;
@@ -321,17 +313,13 @@ exports[`date > 1337 > betweens > with string dates and count 1`] = `
exports[`date > 1337 > birthdate > with age and refDate 1`] = `1980-05-27T14:46:44.794Z`;
-exports[`date > 1337 > birthdate > with age mode and refDate 1`] = `1956-08-25T03:56:58.153Z`;
-
exports[`date > 1337 > birthdate > with age range and refDate 1`] = `1956-02-15T21:16:37.850Z`;
-exports[`date > 1337 > birthdate > with only refDate 1`] = `1957-03-31T18:18:16.563Z`;
-
-exports[`date > 1337 > birthdate > with year and refDate 1`] = `0020-05-27T14:46:44.794Z`;
+exports[`date > 1337 > birthdate > with only refDate 1`] = `1956-08-25T03:56:58.153Z`;
-exports[`date > 1337 > birthdate > with year mode and refDate 1`] = `1957-03-31T18:18:16.563Z`;
+exports[`date > 1337 > birthdate > with year 1`] = `2000-04-06T02:45:32.287Z`;
-exports[`date > 1337 > birthdate > with year range and refDate 1`] = `0046-08-09T19:19:14.289Z`;
+exports[`date > 1337 > birthdate > with year range 1`] = `1926-06-20T07:18:01.782Z`;
exports[`date > 1337 > future > with only Date refDate 1`] = `2021-05-28T08:29:26.600Z`;
diff --git a/test/modules/date.spec.ts b/test/modules/date.spec.ts
index 432573c7..d459db33 100644
--- a/test/modules/date.spec.ts
+++ b/test/modules/date.spec.ts
@@ -106,37 +106,27 @@ describe('date', () => {
t.describe('birthdate', (t) => {
t.it('with only refDate', { refDate })
- .it('with age mode and refDate', {
- mode: 'age',
- refDate,
- })
.it('with age and refDate', {
+ mode: 'age',
min: 40,
max: 40,
- mode: 'age',
refDate,
})
.it('with age range and refDate', {
+ mode: 'age',
min: 20,
max: 80,
- mode: 'age',
refDate,
})
- .it('with year mode and refDate', {
+ .it('with year', {
mode: 'year',
- refDate,
- })
- .it('with year and refDate', {
min: 2000,
max: 2000,
- mode: 'age',
- refDate,
})
- .it('with year range and refDate', {
+ .it('with year range', {
+ mode: 'year',
min: 1900,
max: 2000,
- mode: 'age',
- refDate,
});
});
});
@@ -543,9 +533,9 @@ describe('date', () => {
expect(birthdate).toBeInstanceOf(Date);
});
- it('returns a random birthdate between two years', () => {
+ it('returns a random birthdate in one year', () => {
const min = 1990;
- const max = 2000;
+ const max = 1990;
const birthdate = faker.date.birthdate({ min, max, mode: 'year' });
@@ -557,53 +547,72 @@ 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', () => {
+ it('returns a random birthdate between two years', () => {
const min = 1990;
- const max = 1990;
+ const max = 2000;
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 for specific age', () => {
+ const min = 21;
+ const max = 21;
+ const refDate = new Date();
+
+ const birthdate = faker.date.birthdate({
+ min,
+ max,
+ refDate,
+ mode: 'age',
+ });
+
+ 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).toBe(21);
+ });
+
it('returns a random birthdate between two ages', () => {
- const min = 4;
- const max = 5;
+ const min = 21;
+ const max = 22;
+ const refDate = new Date();
const birthdate = faker.date.birthdate({ min, max, mode: 'age' });
- // birthdate is a date object
expect(birthdate).toBeInstanceOf(Date);
-
- // Generated date is between min and max
- expect(birthdate.getUTCFullYear()).toBeGreaterThanOrEqual(
- new Date().getUTCFullYear() - max - 1
- );
- expect(birthdate.getUTCFullYear()).toBeLessThanOrEqual(
- new Date().getUTCFullYear() - min
- );
+ const value = birthdate.valueOf();
+ const refDateValue = refDate.valueOf();
+ expect(value).toBeLessThanOrEqual(refDateValue);
+ const deltaDate = new Date(refDateValue - value);
+ expect(deltaDate.getUTCFullYear() - 1970).toBeGreaterThanOrEqual(21);
+ expect(deltaDate.getUTCFullYear() - 1970).toBeLessThanOrEqual(22);
});
+ it.each(['min', 'max', 'mode'] as const)(
+ "should throw an error when '%s' is not provided",
+ (key) => {
+ const options = { min: 18, max: 80, mode: 'age' } as const;
+
+ // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
+ delete options[key];
+
+ expect(() => faker.date.birthdate(options)).toThrow(
+ new FakerError(
+ `The 'min', 'max', and 'mode' options must be set together.`
+ )
+ );
+ }
+ );
+
it('should throw an error when the min > max year', () => {
const min = 2000;
const max = 1990;
@@ -612,7 +621,21 @@ describe('date', () => {
faker.date.birthdate({ min, max, mode: 'year' })
).toThrow(
new FakerError(
- `Max 1990 should be larger than or equal to min 2000.`
+ `Max year 1990 should be greater than or equal to min year 2000.`
+ )
+ );
+ });
+
+ it('should throw an error when the min > max age', () => {
+ const min = 31;
+ const max = 25;
+ const refDate = Date.UTC(2020, 0, 1);
+
+ expect(() =>
+ faker.date.birthdate({ min, max, refDate, mode: 'age' })
+ ).toThrow(
+ new FakerError(
+ `Max age 25 should be greater than or equal to min age 31.`
)
);
});