summaryrefslogtreecommitdiff
path: root/date-fns/src/isWithinInterval/test.ts
blob: b7ebe1f37f93b9f368582ec653adc54be3f13652 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// @flow
/* eslint-env mocha */

import assert from 'assert'
import isWithinInterval from '.'

describe('isWithinInterval', function() {
  it('returns true if the given date in within the given interval', function() {
    const result = isWithinInterval(new Date(2014, 9 /* Oct */, 31), {
      start: new Date(2014, 8 /* Sep */, 1),
      end: new Date(2014, 11 /* Dec */, 31)
    })
    assert(result === true)
  })

  it('returns true if the given date has same time as the left boundary of the interval', function() {
    const result = isWithinInterval(new Date(2014, 8 /* Sep */, 1), {
      start: new Date(2014, 8 /* Sep */, 1),
      end: new Date(2014, 11 /* Dec */, 31)
    })
    assert(result === true)
  })

  it('returns true if the given date has same time as the right boundary of the interval', function() {
    const result = isWithinInterval(new Date(2014, 11 /* Dec */, 31), {
      start: new Date(2014, 8 /* Sep */, 1),
      end: new Date(2014, 11 /* Dec */, 31)
    })
    assert(result === true)
  })

  it('returns true if the given date and the both boundaries are the same', function() {
    const result = isWithinInterval(new Date(2014, 11 /* Dec */, 31), {
      start: new Date(2014, 11 /* Dec */, 31),
      end: new Date(2014, 11 /* Dec */, 31)
    })
    assert(result === true)
  })

  it('returns false if the given date is outside of the interval', function() {
    const result = isWithinInterval(new Date(2014, 1 /* Feb */, 11), {
      start: new Date(2014, 8 /* Sep */, 1),
      end: new Date(2014, 11 /* Dec */, 31)
    })
    assert(result === false)
  })

  it('accepts a timestamp', function() {
    const result = isWithinInterval(new Date(2014, 9 /* Oct */, 31).getTime(), {
      start: new Date(2014, 8 /* Sep */, 1).getTime(),
      end: new Date(2014, 11 /* Dec */, 31).getTime()
    })
    assert(result === true)
  })

  it('throws an exception if the start date is after the end date', function() {
    const block = isWithinInterval.bind(null, new Date(2014, 9 /* Oct */, 31), {
      start: new Date(2014, 11 /* Dec */, 31),
      end: new Date(2014, 8 /* Sep */, 1)
    })
    assert.throws(block, RangeError)
  })

  it('throws an exception if the interval is undefined', function() {
    // @ts-expect-error
    const block = isWithinInterval.bind(
      null,
      new Date(2014, 9 /* Oct */, 31),
      undefined
    )
    assert.throws(block, TypeError)
  })

  it('returns false if the given date is `Invalid Date`', function() {
    const result = isWithinInterval(new Date(NaN), {
      start: new Date(2014, 8 /* Sep */, 1),
      end: new Date(2014, 11 /* Dec */, 31)
    })
    assert(result === false)
  })

  it('throws an exception if the start date is `Invalid Date`', function() {
    const block = isWithinInterval.bind(null, new Date(2014, 9 /* Oct */, 31), {
      start: new Date(NaN),
      end: new Date(2014, 8 /* Sep */, 1)
    })
    assert.throws(block, RangeError)
  })

  it('throws an exception if the end date is `Invalid Date`', function() {
    const block = isWithinInterval.bind(null, new Date(2014, 9 /* Oct */, 31), {
      start: new Date(2014, 11 /* Dec */, 31),
      end: new Date(NaN)
    })
    assert.throws(block, RangeError)
  })

  it('throws TypeError exception if passed less than 2 arguments', function() {
    // @ts-expect-error
    assert.throws(isWithinInterval.bind(null), TypeError)
    // @ts-expect-error
    assert.throws(isWithinInterval.bind(null, 1), TypeError)
  })
})