Skip to content

Commit c653d75

Browse files
committed
fix: datatype.datetime should use static boundaries
1 parent af3f99e commit c653d75

File tree

2 files changed

+52
-22
lines changed

2 files changed

+52
-22
lines changed

src/datatype.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ export class Datatype {
131131
let max = typeof options === 'number' ? options : options?.max;
132132

133133
if (typeof min === 'undefined' || min < minMax * -1) {
134-
min = new Date().setFullYear(1990, 1, 1);
134+
min = Date.UTC(1990, 0);
135135
}
136136

137137
if (typeof max === 'undefined' || max > minMax) {
138-
max = new Date().setFullYear(2100, 1, 1);
138+
max = Date.UTC(2100, 0);
139139
}
140140

141141
return new Date(this.faker.datatype.number({ min, max }));

test/datatype.spec.ts

+50-20
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ const seededRuns = [
2222
withMinAndMaxAndPrecision: -0.4261,
2323
},
2424
datetime: {
25-
// TODO @Shinigami92 2022-01-29: We will fix the deterministic in #343
26-
noArgs: new Date('2092-03-22T16:55:38.644Z'),
25+
noArgs: new Date('2031-03-14T21:33:22.114Z'),
26+
number: new Date('1994-03-20T17:23:00.629Z'),
27+
withMin: new Date('1801-04-11T15:13:06.330Z'),
28+
withMax: new Date('1994-07-11T09:43:47.230Z'),
29+
withMinMax: new Date('1689-09-09T08:39:09.444Z'),
2730
},
2831
string: {
2932
noArgs: 'Cky2eiXX/J',
@@ -91,8 +94,11 @@ const seededRuns = [
9194
withMinAndMaxAndPrecision: -12.9153,
9295
},
9396
datetime: {
94-
// TODO @Shinigami92 2022-01-29: We will fix the deterministic in #343
95-
noArgs: new Date('2092-03-22T16:55:38.644Z'),
97+
noArgs: new Date('2018-10-28T08:46:11.896Z'),
98+
number: new Date('1992-12-13T04:13:59.232Z'),
99+
withMin: new Date('1747-07-16T01:19:54.159Z'),
100+
withMax: new Date('1993-03-02T00:10:04.335Z'),
101+
withMinMax: new Date('1669-06-22T01:21:21.236Z'),
96102
},
97103
string: {
98104
noArgs: '9U/4:SK$>6',
@@ -160,8 +166,11 @@ const seededRuns = [
160166
withMinAndMaxAndPrecision: 61.0658,
161167
},
162168
datetime: {
163-
// TODO @Shinigami92 2022-01-29: We will fix the deterministic in #343
164-
noArgs: new Date('2092-03-22T16:55:38.644Z'),
169+
noArgs: new Date('2092-02-20T03:42:04.341Z'),
170+
number: new Date('2000-06-14T02:54:42.082Z'),
171+
withMin: new Date('2065-11-10T19:27:20.915Z'),
172+
withMax: new Date('2001-03-20T11:14:25.251Z'),
173+
withMinMax: new Date('1789-03-26T15:44:45.218Z'),
165174
},
166175
string: {
167176
noArgs: 'wKti5-}$_/',
@@ -230,17 +239,6 @@ describe('datatype', () => {
230239
for (const { seed, expectations } of seededRuns) {
231240
describe(`seed: ${seed}`, () => {
232241
for (const functionName of functionNames) {
233-
if (functionName === 'datetime') {
234-
// TODO @Shinigami92 2022-01-29: We will fix the deterministic in #343
235-
it(`${functionName}()`, () => {
236-
faker.seed(seed);
237-
238-
const actual = faker.datatype.datetime();
239-
expect(actual).toBeTypeOf('object');
240-
});
241-
continue;
242-
}
243-
244242
it(`${functionName}()`, () => {
245243
faker.seed(seed);
246244

@@ -335,10 +333,42 @@ describe('datatype', () => {
335333
});
336334
});
337335

338-
// TODO @ST-DDT 2022-01-29: #343
339-
describe.todo('datetime', () => {
340-
it('should ... ', () => {
336+
describe('datetime', () => {
337+
it('should return a deterministic date when given a number', () => {
341338
faker.seed(seed);
339+
340+
const actual = faker.datatype.datetime(
341+
Date.parse('2001-04-03T23:21:10.773Z')
342+
);
343+
expect(actual).toEqual(expectations.datetime.number);
344+
});
345+
346+
it('should return a deterministic date when given a min date', () => {
347+
faker.seed(seed);
348+
349+
const actual = faker.datatype.datetime({
350+
min: Date.parse('1622-05-23T13:45:08.843Z'),
351+
});
352+
expect(actual).toEqual(expectations.datetime.withMin);
353+
});
354+
355+
it('should return a deterministic date when given a max date', () => {
356+
faker.seed(seed);
357+
358+
const actual = faker.datatype.datetime({
359+
max: Date.parse('2002-01-29T19:47:52.605Z'),
360+
});
361+
expect(actual).toEqual(expectations.datetime.withMax);
362+
});
363+
364+
it('should return a deterministic date when given a min and max date', () => {
365+
faker.seed(seed);
366+
367+
const actual = faker.datatype.datetime({
368+
min: Date.parse('1622-05-23T13:45:08.843Z'),
369+
max: Date.parse('1802-01-29T19:47:52.605Z'),
370+
});
371+
expect(actual).toEqual(expectations.datetime.withMinMax);
342372
});
343373
});
344374

0 commit comments

Comments
 (0)