aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorLeyla Jähnig <[email protected]>2022-03-25 09:02:10 +0100
committerGitHub <[email protected]>2022-03-25 09:02:10 +0100
commitc1c60649ee554f81887ab618ae8b2e2bd5a27d55 (patch)
tree5f70fa6cfe1c1a27b8f19921db4e3b6e0d5a8819 /test
parent7635dc9a63b24b681f88d4029573d0caf8e652b5 (diff)
downloadfaker-c1c60649ee554f81887ab618ae8b2e2bd5a27d55.tar.xz
faker-c1c60649ee554f81887ab618ae8b2e2bd5a27d55.zip
refactor: date accept number input (#670)
Diffstat (limited to 'test')
-rw-r--r--test/date.spec.ts213
1 files changed, 96 insertions, 117 deletions
diff --git a/test/date.spec.ts b/test/date.spec.ts
index 1d6f8832..99141a89 100644
--- a/test/date.spec.ts
+++ b/test/date.spec.ts
@@ -103,6 +103,12 @@ const seededRuns = [
},
];
+const converterMap = [
+ (d: Date) => d,
+ (d: Date) => d.toISOString(),
+ (d: Date) => d.valueOf(),
+];
+
const NON_SEEDED_BASED_RUN = 5;
describe('date', () => {
@@ -384,20 +390,18 @@ describe('date', () => {
expect(date).lessThan(refDate);
});
- it('should return a past date relative to given refDate', () => {
- const refDate = new Date();
- refDate.setFullYear(refDate.getFullYear() + 5);
-
- let date = faker.date.past(5, refDate);
-
- expect(date).lessThan(refDate);
- expect(date).greaterThan(new Date());
+ it.each(converterMap)(
+ 'should return a past date relative to given refDate',
+ (converter) => {
+ const refDate = new Date();
+ refDate.setFullYear(refDate.getFullYear() + 5);
- date = faker.date.past(5, refDate.toISOString());
+ const date = faker.date.past(5, converter(refDate));
- expect(date).lessThan(refDate);
- expect(date).greaterThan(new Date());
- });
+ expect(date).lessThan(refDate);
+ expect(date).greaterThan(new Date());
+ }
+ );
});
describe('future()', () => {
@@ -414,59 +418,50 @@ describe('date', () => {
expect(date).greaterThan(refDate); // date should be after the date given
});
- it('should return a date 75 years after the date given', () => {
- 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)
+ it.each(converterMap)(
+ 'should return a date 75 years after the date given',
+ (converter) => {
+ 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)
- let date = faker.date.future(75, refDate);
+ const date = faker.date.future(75, converter(refDate));
- // date should be after the date given, but before the current time
- expect(date).greaterThan(refDate);
- expect(date).lessThan(new Date());
-
- date = faker.date.future(75, refDate.toISOString());
-
- // date should be after the date given, but before the current time
- expect(date).greaterThan(refDate);
- expect(date).lessThan(new Date());
- });
+ // date should be after the date given, but before the current time
+ expect(date).greaterThan(refDate);
+ expect(date).lessThan(new Date());
+ }
+ );
});
describe('between()', () => {
- it('should return a random date between the dates given', () => {
- const from = new Date(1990, 5, 7, 9, 11, 0, 0);
- const to = new Date(2000, 6, 8, 10, 12, 0, 0);
-
- let date = faker.date.between(from, to);
-
- expect(date).greaterThan(from);
- expect(date).lessThan(to);
-
- date = faker.date.between(from.toISOString(), to.toISOString());
-
- expect(date).greaterThan(from);
- expect(date).lessThan(to);
- });
+ it.each(converterMap)(
+ 'should return a random date between the dates given',
+ (converter) => {
+ const from = new Date(1990, 5, 7, 9, 11, 0, 0);
+ const to = new Date(2000, 6, 8, 10, 12, 0, 0);
+
+ const date = faker.date.between(converter(from), converter(to));
+
+ expect(date).greaterThan(from);
+ expect(date).lessThan(to);
+ }
+ );
});
describe('betweens()', () => {
- it('should return an array of 3 dates ( by default ) of sorted randoms dates between the dates given', () => {
- const from = new Date(1990, 5, 7, 9, 11, 0, 0);
- const to = new Date(2000, 6, 8, 10, 12, 0, 0);
-
- let dates = faker.date.betweens(from, to);
-
- expect(dates[0]).greaterThan(from);
- expect(dates[0]).lessThan(to);
- expect(dates[1]).greaterThan(dates[0]);
- expect(dates[2]).greaterThan(dates[1]);
-
- dates = faker.date.betweens(from.toISOString(), to.toISOString());
-
- expect(dates[0]).greaterThan(from);
- expect(dates[0]).lessThan(to);
- expect(dates[1]).greaterThan(dates[0]);
- expect(dates[2]).greaterThan(dates[1]);
- });
+ it.each(converterMap)(
+ 'should return an array of 3 dates ( by default ) of sorted randoms dates between the dates given',
+ (converter) => {
+ const from = new Date(1990, 5, 7, 9, 11, 0, 0);
+ const to = new Date(2000, 6, 8, 10, 12, 0, 0);
+
+ const dates = faker.date.betweens(converter(from), converter(to));
+
+ expect(dates[0]).greaterThan(from);
+ expect(dates[0]).lessThan(to);
+ expect(dates[1]).greaterThan(dates[0]);
+ expect(dates[2]).greaterThan(dates[1]);
+ }
+ );
});
describe('recent()', () => {
@@ -476,36 +471,28 @@ describe('date', () => {
expect(date).lessThanOrEqual(new Date());
});
- it('should return a date N days from the recent past, starting from refDate', () => {
- const days = 30;
- 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)
-
- const lowerBound = new Date(
- refDate.getTime() - days * 24 * 60 * 60 * 1000
- );
-
- let date = faker.date.recent(days, refDate);
-
- expect(
- lowerBound,
- '`recent()` date should not be further back than `n` days ago'
- ).lessThanOrEqual(date);
- expect(
- date,
- '`recent()` date should not be ahead of the starting date reference'
- ).lessThanOrEqual(refDate);
-
- date = faker.date.recent(days, refDate.toISOString());
-
- expect(
- lowerBound,
- '`recent()` date should not be further back than `n` days ago'
- ).lessThanOrEqual(date);
- expect(
- date,
- '`recent()` date should not be ahead of the starting date reference'
- ).lessThanOrEqual(refDate);
- });
+ it.each(converterMap)(
+ 'should return a date N days from the recent past, starting from refDate',
+ (converter) => {
+ const days = 30;
+ 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)
+
+ const lowerBound = new Date(
+ refDate.getTime() - days * 24 * 60 * 60 * 1000
+ );
+
+ const date = faker.date.recent(days, converter(refDate));
+
+ expect(
+ lowerBound,
+ '`recent()` date should not be further back than `n` days ago'
+ ).lessThanOrEqual(date);
+ expect(
+ date,
+ '`recent()` date should not be ahead of the starting date reference'
+ ).lessThanOrEqual(refDate);
+ }
+ );
});
describe('soon()', () => {
@@ -515,36 +502,28 @@ describe('date', () => {
expect(date).greaterThanOrEqual(new Date());
});
- it('should return a date N days from the recent future, starting from refDate', () => {
- const days = 30;
- 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)
-
- const upperBound = new Date(
- refDate.getTime() + days * 24 * 60 * 60 * 1000
- );
-
- let date = faker.date.soon(days, refDate);
-
- expect(
- date,
- '`soon()` date should not be further ahead than `n` days ago'
- ).lessThanOrEqual(upperBound);
- expect(
- refDate,
- '`soon()` date should not be behind the starting date reference'
- ).lessThanOrEqual(date);
-
- date = faker.date.soon(days, refDate.toISOString());
-
- expect(
- date,
- '`soon()` date should not be further ahead than `n` days ago'
- ).lessThanOrEqual(upperBound);
- expect(
- refDate,
- '`soon()` date should not be behind the starting date reference'
- ).lessThanOrEqual(date);
- });
+ it.each(converterMap)(
+ 'should return a date N days from the recent future, starting from refDate',
+ (converter) => {
+ const days = 30;
+ 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)
+
+ const upperBound = new Date(
+ refDate.getTime() + days * 24 * 60 * 60 * 1000
+ );
+
+ const date = faker.date.soon(days, converter(refDate));
+
+ expect(
+ date,
+ '`soon()` date should not be further ahead than `n` days ago'
+ ).lessThanOrEqual(upperBound);
+ expect(
+ refDate,
+ '`soon()` date should not be behind the starting date reference'
+ ).lessThanOrEqual(date);
+ }
+ );
});
describe('month()', () => {