Skip to content

Commit a041237

Browse files
test(faker.date): number inputs
1 parent 918bde6 commit a041237

File tree

1 file changed

+96
-117
lines changed

1 file changed

+96
-117
lines changed

test/date.spec.ts

Lines changed: 96 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ const seededRuns = [
103103
},
104104
];
105105

106+
const converterMap = [
107+
(d: Date) => d,
108+
(d: Date) => d.toISOString(),
109+
(d: Date) => d.valueOf(),
110+
];
111+
106112
const NON_SEEDED_BASED_RUN = 5;
107113

108114
describe('date', () => {
@@ -384,20 +390,18 @@ describe('date', () => {
384390
expect(date).lessThan(refDate);
385391
});
386392

387-
it('should return a past date relative to given refDate', () => {
388-
const refDate = new Date();
389-
refDate.setFullYear(refDate.getFullYear() + 5);
390-
391-
let date = faker.date.past(5, refDate);
392-
393-
expect(date).lessThan(refDate);
394-
expect(date).greaterThan(new Date());
393+
it.each(converterMap)(
394+
'should return a past date relative to given refDate',
395+
(converter) => {
396+
const refDate = new Date();
397+
refDate.setFullYear(refDate.getFullYear() + 5);
395398

396-
date = faker.date.past(5, refDate.toISOString());
399+
const date = faker.date.past(5, converter(refDate));
397400

398-
expect(date).lessThan(refDate);
399-
expect(date).greaterThan(new Date());
400-
});
401+
expect(date).lessThan(refDate);
402+
expect(date).greaterThan(new Date());
403+
}
404+
);
401405
});
402406

403407
describe('future()', () => {
@@ -414,59 +418,50 @@ describe('date', () => {
414418
expect(date).greaterThan(refDate); // date should be after the date given
415419
});
416420

417-
it('should return a date 75 years after the date given', () => {
418-
const refDate = new Date(1880, 11, 9, 10, 0, 0, 0); // set the date beyond the usual calculation (to make sure this is working correctly)
421+
it.each(converterMap)(
422+
'should return a date 75 years after the date given',
423+
(converter) => {
424+
const refDate = new Date(1880, 11, 9, 10, 0, 0, 0); // set the date beyond the usual calculation (to make sure this is working correctly)
419425

420-
let date = faker.date.future(75, refDate);
426+
const date = faker.date.future(75, converter(refDate));
421427

422-
// date should be after the date given, but before the current time
423-
expect(date).greaterThan(refDate);
424-
expect(date).lessThan(new Date());
425-
426-
date = faker.date.future(75, refDate.toISOString());
427-
428-
// date should be after the date given, but before the current time
429-
expect(date).greaterThan(refDate);
430-
expect(date).lessThan(new Date());
431-
});
428+
// date should be after the date given, but before the current time
429+
expect(date).greaterThan(refDate);
430+
expect(date).lessThan(new Date());
431+
}
432+
);
432433
});
433434

434435
describe('between()', () => {
435-
it('should return a random date between the dates given', () => {
436-
const from = new Date(1990, 5, 7, 9, 11, 0, 0);
437-
const to = new Date(2000, 6, 8, 10, 12, 0, 0);
438-
439-
let date = faker.date.between(from, to);
440-
441-
expect(date).greaterThan(from);
442-
expect(date).lessThan(to);
443-
444-
date = faker.date.between(from.toISOString(), to.toISOString());
445-
446-
expect(date).greaterThan(from);
447-
expect(date).lessThan(to);
448-
});
436+
it.each(converterMap)(
437+
'should return a random date between the dates given',
438+
(converter) => {
439+
const from = new Date(1990, 5, 7, 9, 11, 0, 0);
440+
const to = new Date(2000, 6, 8, 10, 12, 0, 0);
441+
442+
const date = faker.date.between(converter(from), converter(to));
443+
444+
expect(date).greaterThan(from);
445+
expect(date).lessThan(to);
446+
}
447+
);
449448
});
450449

451450
describe('betweens()', () => {
452-
it('should return an array of 3 dates ( by default ) of sorted randoms dates between the dates given', () => {
453-
const from = new Date(1990, 5, 7, 9, 11, 0, 0);
454-
const to = new Date(2000, 6, 8, 10, 12, 0, 0);
455-
456-
let dates = faker.date.betweens(from, to);
457-
458-
expect(dates[0]).greaterThan(from);
459-
expect(dates[0]).lessThan(to);
460-
expect(dates[1]).greaterThan(dates[0]);
461-
expect(dates[2]).greaterThan(dates[1]);
462-
463-
dates = faker.date.betweens(from.toISOString(), to.toISOString());
464-
465-
expect(dates[0]).greaterThan(from);
466-
expect(dates[0]).lessThan(to);
467-
expect(dates[1]).greaterThan(dates[0]);
468-
expect(dates[2]).greaterThan(dates[1]);
469-
});
451+
it.each(converterMap)(
452+
'should return an array of 3 dates ( by default ) of sorted randoms dates between the dates given',
453+
(converter) => {
454+
const from = new Date(1990, 5, 7, 9, 11, 0, 0);
455+
const to = new Date(2000, 6, 8, 10, 12, 0, 0);
456+
457+
const dates = faker.date.betweens(converter(from), converter(to));
458+
459+
expect(dates[0]).greaterThan(from);
460+
expect(dates[0]).lessThan(to);
461+
expect(dates[1]).greaterThan(dates[0]);
462+
expect(dates[2]).greaterThan(dates[1]);
463+
}
464+
);
470465
});
471466

472467
describe('recent()', () => {
@@ -476,36 +471,28 @@ describe('date', () => {
476471
expect(date).lessThanOrEqual(new Date());
477472
});
478473

479-
it('should return a date N days from the recent past, starting from refDate', () => {
480-
const days = 30;
481-
const refDate = new Date(2120, 11, 9, 10, 0, 0, 0); // set the date beyond the usual calculation (to make sure this is working correctly)
482-
483-
const lowerBound = new Date(
484-
refDate.getTime() - days * 24 * 60 * 60 * 1000
485-
);
486-
487-
let date = faker.date.recent(days, refDate);
488-
489-
expect(
490-
lowerBound,
491-
'`recent()` date should not be further back than `n` days ago'
492-
).lessThanOrEqual(date);
493-
expect(
494-
date,
495-
'`recent()` date should not be ahead of the starting date reference'
496-
).lessThanOrEqual(refDate);
497-
498-
date = faker.date.recent(days, refDate.toISOString());
499-
500-
expect(
501-
lowerBound,
502-
'`recent()` date should not be further back than `n` days ago'
503-
).lessThanOrEqual(date);
504-
expect(
505-
date,
506-
'`recent()` date should not be ahead of the starting date reference'
507-
).lessThanOrEqual(refDate);
508-
});
474+
it.each(converterMap)(
475+
'should return a date N days from the recent past, starting from refDate',
476+
(converter) => {
477+
const days = 30;
478+
const refDate = new Date(2120, 11, 9, 10, 0, 0, 0); // set the date beyond the usual calculation (to make sure this is working correctly)
479+
480+
const lowerBound = new Date(
481+
refDate.getTime() - days * 24 * 60 * 60 * 1000
482+
);
483+
484+
const date = faker.date.recent(days, converter(refDate));
485+
486+
expect(
487+
lowerBound,
488+
'`recent()` date should not be further back than `n` days ago'
489+
).lessThanOrEqual(date);
490+
expect(
491+
date,
492+
'`recent()` date should not be ahead of the starting date reference'
493+
).lessThanOrEqual(refDate);
494+
}
495+
);
509496
});
510497

511498
describe('soon()', () => {
@@ -515,36 +502,28 @@ describe('date', () => {
515502
expect(date).greaterThanOrEqual(new Date());
516503
});
517504

518-
it('should return a date N days from the recent future, starting from refDate', () => {
519-
const days = 30;
520-
const refDate = new Date(1880, 11, 9, 10, 0, 0, 0); // set the date beyond the usual calculation (to make sure this is working correctly)
521-
522-
const upperBound = new Date(
523-
refDate.getTime() + days * 24 * 60 * 60 * 1000
524-
);
525-
526-
let date = faker.date.soon(days, refDate);
527-
528-
expect(
529-
date,
530-
'`soon()` date should not be further ahead than `n` days ago'
531-
).lessThanOrEqual(upperBound);
532-
expect(
533-
refDate,
534-
'`soon()` date should not be behind the starting date reference'
535-
).lessThanOrEqual(date);
536-
537-
date = faker.date.soon(days, refDate.toISOString());
538-
539-
expect(
540-
date,
541-
'`soon()` date should not be further ahead than `n` days ago'
542-
).lessThanOrEqual(upperBound);
543-
expect(
544-
refDate,
545-
'`soon()` date should not be behind the starting date reference'
546-
).lessThanOrEqual(date);
547-
});
505+
it.each(converterMap)(
506+
'should return a date N days from the recent future, starting from refDate',
507+
(converter) => {
508+
const days = 30;
509+
const refDate = new Date(1880, 11, 9, 10, 0, 0, 0); // set the date beyond the usual calculation (to make sure this is working correctly)
510+
511+
const upperBound = new Date(
512+
refDate.getTime() + days * 24 * 60 * 60 * 1000
513+
);
514+
515+
const date = faker.date.soon(days, converter(refDate));
516+
517+
expect(
518+
date,
519+
'`soon()` date should not be further ahead than `n` days ago'
520+
).lessThanOrEqual(upperBound);
521+
expect(
522+
refDate,
523+
'`soon()` date should not be behind the starting date reference'
524+
).lessThanOrEqual(date);
525+
}
526+
);
548527
});
549528

550529
describe('month()', () => {

0 commit comments

Comments
 (0)