-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b26de41
commit f2be585
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {Predicate, Context} from './predicate'; | ||
|
||
export class DatePredicate extends Predicate<Date> { | ||
constructor(context?: Context) { | ||
super('date', context); | ||
} | ||
|
||
/** | ||
* Test a date to be before another date. | ||
* | ||
* @param date Maximum value. | ||
*/ | ||
before(date: Date) { | ||
return this.addValidator({ | ||
message: value => `Expected ${value.toISOString()} to be before ${date.toISOString()}`, | ||
validator: value => value.getTime() < date.getTime() | ||
}); | ||
} | ||
|
||
/** | ||
* Test a date to be before another date. | ||
* | ||
* @param date Minimum value. | ||
*/ | ||
after(date: Date) { | ||
return this.addValidator({ | ||
message: value => `Expected ${value.toISOString()} to be after ${date.toISOString()}`, | ||
validator: value => value.getTime() > date.getTime() | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import test from 'ava'; | ||
import m from '..'; | ||
|
||
test('date', t => { | ||
t.notThrows(() => m(new Date(), m.date)); | ||
t.throws(() => m('12', m.date), 'Expected argument to be of type `date` but received type `string`'); | ||
}); | ||
|
||
test('date.before', t => { | ||
t.notThrows(() => m(new Date('2017-11-25'), m.date.before(new Date('2017-11-26')))); | ||
t.notThrows(() => m(new Date('2017-11-25T12:00:00Z'), m.date.before(new Date('2017-11-25T12:00:01Z')))); | ||
t.throws(() => m(new Date('2017-11-25T12:00:00Z'), m.date.before(new Date('2017-11-25T12:00:00Z'))), 'Expected 2017-11-25T12:00:00.000Z to be before 2017-11-25T12:00:00.000Z'); | ||
}); | ||
|
||
test('date.after', t => { | ||
t.notThrows(() => m(new Date('2017-11-26'), m.date.after(new Date('2017-11-25')))); | ||
t.notThrows(() => m(new Date('2017-11-26T12:00:00Z'), m.date.after(new Date('2017-11-26T11:59:59Z')))); | ||
t.throws(() => m(new Date('2017-11-26T12:00:00Z'), m.date.after(new Date('2017-11-26T12:00:00Z'))), 'Expected 2017-11-26T12:00:00.000Z to be after 2017-11-26T12:00:00.000Z'); | ||
}); |