@@ -44,7 +44,8 @@ describe('date', () => {
4444 . it ( 'with only number refDate' , {
4545 refDate : new Date ( refDate ) . getTime ( ) ,
4646 } )
47- . it ( 'with value' , { years : 10 , refDate } ) ;
47+ . it ( 'with value numeric' , { years : 10 , refDate } )
48+ . it ( 'with value range' , { years : { min : 3 , max : 12 } , refDate } ) ;
4849 } ) ;
4950
5051 t . describeEach (
@@ -190,13 +191,60 @@ describe('date', () => {
190191 expect ( date ) . greaterThanOrEqual ( yearsAgo ) ;
191192 } ) ;
192193
194+ it ( 'should return a date between 20 and 40 years in the past' , ( ) => {
195+ const today = new Date ( ) ;
196+ const yearsAgoMax = 40 ;
197+ const yearAgoMax = new Date ( today ) ;
198+ yearAgoMax . setFullYear ( yearAgoMax . getFullYear ( ) - yearsAgoMax ) ;
199+
200+ const yearsAgoMin = 20 ;
201+ const yearAgoMin = new Date ( today ) ;
202+ yearAgoMin . setFullYear ( yearAgoMin . getFullYear ( ) - yearsAgoMin ) ;
203+
204+ const date = faker . date . past ( {
205+ years : { min : yearsAgoMin , max : yearsAgoMax } ,
206+ } ) ;
207+
208+ expect ( date ) . lessThan ( today ) ;
209+ expect ( date ) . lessThan ( yearAgoMin ) ;
210+ expect ( date ) . greaterThanOrEqual ( yearAgoMax ) ;
211+ } ) ;
212+
193213 it ( 'should throw an error when years = 0' , ( ) => {
194214 const refDate = new Date ( ) ;
195215 expect ( ( ) =>
196216 faker . date . past ( { years : 0 , refDate : refDate . toISOString ( ) } )
197217 ) . toThrow ( new FakerError ( 'Years must be greater than 0.' ) ) ;
198218 } ) ;
199219
220+ it ( 'should throw an error when years.min > years.max' , ( ) => {
221+ const refDate = new Date ( ) ;
222+ expect ( ( ) =>
223+ faker . date . past ( {
224+ years : { min : 3 , max : 2 } ,
225+ refDate : refDate . toISOString ( ) ,
226+ } )
227+ ) . toThrow (
228+ new FakerError (
229+ 'The maximum amount of years must be greater than the minimum amount of years.'
230+ )
231+ ) ;
232+ } ) ;
233+
234+ it ( 'should throw an error when years.min = years.max' , ( ) => {
235+ const refDate = new Date ( ) ;
236+ expect ( ( ) =>
237+ faker . date . past ( {
238+ years : { min : 6 , max : 6 } ,
239+ refDate : refDate . toISOString ( ) ,
240+ } )
241+ ) . toThrow (
242+ new FakerError (
243+ 'The maximum amount of years must be greater than the minimum amount of years.'
244+ )
245+ ) ;
246+ } ) ;
247+
200248 it . each ( converterMap ) (
201249 'should return a past date relative to given refDate' ,
202250 ( converter ) => {
@@ -216,9 +264,34 @@ describe('date', () => {
216264
217265 describe ( 'future()' , ( ) => {
218266 it ( 'should return a date 75 years into the future' , ( ) => {
219- const date = faker . date . future ( { years : 75 } ) ;
267+ const today = new Date ( ) ;
268+ const yearsUntilMax = 75 ;
269+ const yearUntilMax = new Date ( today ) ;
270+ yearUntilMax . setFullYear ( yearUntilMax . getFullYear ( ) + yearsUntilMax ) ;
220271
221- expect ( date ) . greaterThan ( new Date ( ) ) ;
272+ const date = faker . date . future ( { years : yearsUntilMax } ) ;
273+
274+ expect ( date ) . greaterThan ( today ) ;
275+ expect ( date ) . lessThanOrEqual ( yearUntilMax ) ;
276+ } ) ;
277+
278+ it ( 'should return a date between 20 and 40 years in the future' , ( ) => {
279+ const today = new Date ( ) ;
280+ const yearsUntilMin = 20 ;
281+ const yearUntilMin = new Date ( today ) ;
282+ yearUntilMin . setFullYear ( yearUntilMin . getFullYear ( ) + yearsUntilMin ) ;
283+
284+ const yearsUntilMax = 40 ;
285+ const yearUntilMax = new Date ( today ) ;
286+ yearUntilMax . setFullYear ( yearUntilMax . getFullYear ( ) + yearsUntilMax ) ;
287+
288+ const date = faker . date . future ( {
289+ years : { min : yearsUntilMin , max : yearsUntilMax } ,
290+ } ) ;
291+
292+ expect ( date ) . greaterThan ( today ) ;
293+ expect ( date ) . greaterThan ( yearUntilMin ) ;
294+ expect ( date ) . lessThanOrEqual ( yearUntilMax ) ;
222295 } ) ;
223296
224297 it ( 'should throw an error when years = 0' , ( ) => {
@@ -228,6 +301,34 @@ describe('date', () => {
228301 ) . toThrow ( new FakerError ( 'Years must be greater than 0.' ) ) ;
229302 } ) ;
230303
304+ it ( 'should throw an error when years.min > years.max' , ( ) => {
305+ const refDate = new Date ( ) ;
306+ expect ( ( ) =>
307+ faker . date . future ( {
308+ years : { min : 3 , max : 2 } ,
309+ refDate : refDate . toISOString ( ) ,
310+ } )
311+ ) . toThrow (
312+ new FakerError (
313+ 'The maximum amount of years must be greater than the minimum amount of years.'
314+ )
315+ ) ;
316+ } ) ;
317+
318+ it ( 'should throw an error when years.min = years.max' , ( ) => {
319+ const refDate = new Date ( ) ;
320+ expect ( ( ) =>
321+ faker . date . future ( {
322+ years : { min : 6 , max : 6 } ,
323+ refDate : refDate . toISOString ( ) ,
324+ } )
325+ ) . toThrow (
326+ new FakerError (
327+ 'The maximum amount of years must be greater than the minimum amount of years.'
328+ )
329+ ) ;
330+ } ) ;
331+
231332 it . each ( converterMap ) (
232333 'should return a date 75 years after the date given' ,
233334 ( converter ) => {
0 commit comments