summaryrefslogtreecommitdiff
path: root/date-fns/src/areIntervalsOverlapping/test.ts
blob: 1ff7c32ef70eb3b2187e78169b1c8458f7dadd3c (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// @flow
/* eslint-env mocha */

import assert from 'assert'
import areIntervalsOverlapping from '.'
import context from 'assert'

describe('areIntervalsOverlapping', function () {
  const initialIntervalStart = new Date(2016, 10, 10, 13, 0, 0)
  const initialIntervalEnd = new Date(2016, 11, 3, 15, 0, 0)

  describe("when the time intervals don't overlap", function () {
    it('returns false for a valid non overlapping interval before another interval', function () {
      const earlierIntervalStart = new Date(2016, 9, 25)
      const earlierIntervalEnd = new Date(2016, 10, 9)

      const isOverlapping = areIntervalsOverlapping(
        { start: initialIntervalStart, end: initialIntervalEnd },
        { start: earlierIntervalStart, end: earlierIntervalEnd }
      )
      assert(isOverlapping === false)
    })

    it('returns false for a valid non overlapping interval after another interval', function () {
      const laterIntervalStart = new Date(2016, 11, 4)
      const laterIntervalEnd = new Date(2016, 11, 9)

      const isOverlapping = areIntervalsOverlapping(
        { start: initialIntervalStart, end: initialIntervalEnd },
        { start: laterIntervalStart, end: laterIntervalEnd }
      )
      assert(isOverlapping === false)
    })

    it('returns false for a non overlapping same-day interval', function () {
      const sameDayIntervalStart = new Date(2016, 11, 4, 9, 0, 0)
      const sameDayIntervalEnd = new Date(2016, 11, 4, 18, 0, 0)

      const isOverlapping = areIntervalsOverlapping(
        { start: initialIntervalStart, end: initialIntervalEnd },
        { start: sameDayIntervalStart, end: sameDayIntervalEnd }
      )
      assert(isOverlapping === false)
    })

    it('returns false for an interval differing by a few hours', function () {
      const oneDayOverlappingIntervalStart = new Date(2016, 11, 3, 18, 0, 0)
      const oneDayOverlappingIntervalEnd = new Date(2016, 11, 14, 13, 0, 0)

      const isOverlapping = areIntervalsOverlapping(
        { start: initialIntervalStart, end: initialIntervalEnd },
        {
          start: oneDayOverlappingIntervalStart,
          end: oneDayOverlappingIntervalEnd,
        }
      )
      assert(isOverlapping === false)
    })

    it("returns false for an interval with the same startDateTime as the initial time intervals's endDateTime", function () {
      const oneDayOverlapIntervalStart = new Date(2016, 11, 3, 15, 0, 0)
      const oneDayOverlapIntervalEnd = new Date(2016, 11, 14, 13, 0, 0)

      const isOverlapping = areIntervalsOverlapping(
        { start: initialIntervalStart, end: initialIntervalEnd },
        { start: oneDayOverlapIntervalStart, end: oneDayOverlapIntervalEnd }
      )
      assert(isOverlapping === false)
    })

    it("returns false for an interval with the same endDateTime as the initial time interval's startDateTime", function () {
      const oneDayOverlapIntervalStart = new Date(2016, 10, 3, 15, 0, 0)
      const oneDayOverlapIntervalEnd = new Date(2016, 10, 10, 13, 0, 0)

      const isOverlapping = areIntervalsOverlapping(
        { start: initialIntervalStart, end: initialIntervalEnd },
        { start: oneDayOverlapIntervalStart, end: oneDayOverlapIntervalEnd }
      )
      assert(isOverlapping === false)
    })
  })

  describe('when the time intervals overlap', function () {
    it('returns true for an interval included within another interval', function () {
      const includedIntervalStart = new Date(2016, 10, 14)
      const includedIntervalEnd = new Date(2016, 10, 14)

      const isOverlapping = areIntervalsOverlapping(
        { start: initialIntervalStart, end: initialIntervalEnd },
        { start: includedIntervalStart, end: includedIntervalEnd }
      )
      assert(isOverlapping === true)
    })

    it('returns true for an interval overlapping at the end', function () {
      const endOverlappingIntervalStart = new Date(2016, 10, 5)
      const endOverlappingIntervalEnd = new Date(2016, 10, 14)

      const isOverlapping = areIntervalsOverlapping(
        { start: initialIntervalStart, end: initialIntervalEnd },
        { start: endOverlappingIntervalStart, end: endOverlappingIntervalEnd }
      )
      assert(isOverlapping === true)
    })

    it('returns true for an interval overlapping at the beginning', function () {
      const startOverlappingIntervalStart = new Date(2016, 10, 20)
      const startOverlappingIntervalEnd = new Date(2016, 11, 14)

      const isOverlapping = areIntervalsOverlapping(
        { start: initialIntervalStart, end: initialIntervalEnd },
        {
          start: startOverlappingIntervalStart,
          end: startOverlappingIntervalEnd,
        }
      )
      assert(isOverlapping === true)
    })

    it('returns true for an interval including another interval', function () {
      const includingIntervalStart = new Date(2016, 10, 5)
      const includingIntervalEnd = new Date(2016, 11, 15)

      const isOverlapping = areIntervalsOverlapping(
        { start: initialIntervalStart, end: initialIntervalEnd },
        { start: includingIntervalStart, end: includingIntervalEnd }
      )
      assert(isOverlapping === true)
    })
  })

  it('accepts timestamp', function () {
    const initialIntervalStart = new Date(2016, 10, 10, 13, 0, 0).getTime()
    const initialIntervalEnd = new Date(2016, 11, 3, 15, 0, 0).getTime()

    const endOverlappingIntervalStart = new Date(2016, 10, 5).getTime()
    const endOverlappingIntervalEnd = new Date(2016, 10, 14).getTime()

    const isOverlapping = areIntervalsOverlapping(
      { start: initialIntervalStart, end: initialIntervalEnd },
      { start: endOverlappingIntervalStart, end: endOverlappingIntervalEnd }
    )
    assert(isOverlapping === true)
  })

  it('throws an exception if the start date of the initial time interval is after the end date', function () {
    const block = areIntervalsOverlapping.bind(
      null,
      { start: new Date(2016, 10, 7), end: new Date(2016, 10, 3) },
      { start: new Date(2016, 10, 5), end: new Date(2016, 10, 15) }
    )
    assert.throws(block, RangeError)
  })

  it('throws an exception if the start date of the compared time interval is after the end date', function () {
    const block = areIntervalsOverlapping.bind(
      null,
      { start: new Date(2016, 10, 3), end: new Date(2016, 10, 7) },
      { start: new Date(2016, 10, 15), end: new Date(2016, 10, 5) }
    )
    assert.throws(block, RangeError)
  })

  it('throws an exception if the initial interval is undefined', function () {
    // @ts-expect-error
    const block = areIntervalsOverlapping.bind(null, undefined, {
      start: new Date(2016, 10, 5),
      end: new Date(2016, 10, 15),
    })
    assert.throws(block, RangeError)
  })

  it('throws an exception if the compared interval is undefined', function () {
    // @ts-expect-error
    const block = areIntervalsOverlapping.bind(
      null,
      { start: new Date(2016, 10, 3), end: new Date(2016, 10, 7) },
      undefined
    )
    assert.throws(block, RangeError)
  })

  describe('when the inclusive option is true', function () {
    it("returns true for an interval with the same startDateTime as the initial time interval's endDateTime", function () {
      const oneDayOverlapIntervalStart = new Date(2016, 11, 3, 15, 0, 0)
      const oneDayOverlapIntervalEnd = new Date(2016, 11, 14, 13, 0, 0)

      const isOverlapping = areIntervalsOverlapping(
        { start: initialIntervalStart, end: initialIntervalEnd },
        { start: oneDayOverlapIntervalStart, end: oneDayOverlapIntervalEnd },
        { inclusive: true }
      )
      assert(isOverlapping)
    })

    it("returns true for an interval with the same endDateTime as the initial time interval's startDateTime", function () {
      const oneDayOverlapIntervalStart = new Date(2016, 10, 3, 15, 0, 0)
      const oneDayOverlapIntervalEnd = new Date(2016, 10, 10, 13, 0, 0)

      const isOverlapping = areIntervalsOverlapping(
        { start: initialIntervalStart, end: initialIntervalEnd },
        { start: oneDayOverlapIntervalStart, end: oneDayOverlapIntervalEnd },
        { inclusive: true }
      )
      assert(isOverlapping)
    })
  })

  describe('one of the dates is `Invalid Date`', function () {
    it('throws an exception if the start date of the initial time interval is `Invalid Date`', function () {
      const block = areIntervalsOverlapping.bind(
        null,
        { start: new Date(NaN), end: new Date(2016, 10, 3) },
        { start: new Date(2016, 10, 5), end: new Date(2016, 10, 15) }
      )
      assert.throws(block, RangeError)
    })

    it('throws an exception if the end date of the initial time interval is `Invalid Date`', function () {
      const block = areIntervalsOverlapping.bind(
        null,
        { start: new Date(2016, 10, 3), end: new Date(NaN) },
        { start: new Date(2016, 10, 5), end: new Date(2016, 10, 15) }
      )
      assert.throws(block, RangeError)
    })

    it('throws an exception if the start date of the compared time interval is `Invalid Date`', function () {
      const block = areIntervalsOverlapping.bind(
        null,
        { start: new Date(2016, 10, 3), end: new Date(2016, 10, 7) },
        { start: new Date(NaN), end: new Date(2016, 10, 5) }
      )
      assert.throws(block, RangeError)
    })

    it('throws an exception if the end date of the compared time interval is `Invalid Date`', function () {
      const block = areIntervalsOverlapping.bind(
        null,
        { start: new Date(2016, 10, 3), end: new Date(2016, 10, 7) },
        { start: new Date(2016, 10, 5), end: new Date(NaN) }
      )
      assert.throws(block, RangeError)
    })
  })

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