summaryrefslogtreecommitdiff
path: root/date-fns/src/eachHourOfInterval/test.ts
blob: 4bdaa0577181a7b1ed1a1d55c4fbe715bf73bea2 (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
// @flow
/* eslint-env mocha */

import assert from 'assert'
import eachHourOfInterval from '.'

describe('eachHourOfInterval', () => {
  it('returns an array with starts of hours from the hour of the start date to the hour of the end date', () => {
    const result = eachHourOfInterval({
      start: new Date(2014, 9 /* Oct */, 6, 12),
      end: new Date(2014, 9 /* Oct */, 6, 15)
    })
    assert.deepEqual(result, [
      new Date(2014, 9 /* Oct */, 6, 12),
      new Date(2014, 9 /* Oct */, 6, 13),
      new Date(2014, 9 /* Oct */, 6, 14),
      new Date(2014, 9 /* Oct */, 6, 15)
    ])
  })

  it('accepts timestamps', () => {
    const result = eachHourOfInterval({
      start: new Date(2014, 9 /* Oct */, 6, 12).getTime(),
      end: new Date(2014, 9 /* Oct */, 6, 15).getTime()
    })
    assert.deepEqual(result, [
      new Date(2014, 9 /* Oct */, 6, 12),
      new Date(2014, 9 /* Oct */, 6, 13),
      new Date(2014, 9 /* Oct */, 6, 14),
      new Date(2014, 9 /* Oct */, 6, 15)
    ])
  })

  it('handles the hours that are not starts of hours', () => {
    const result = eachHourOfInterval({
      start: new Date(2014, 9 /* Oct */, 6, 12, 59, 59, 999),
      end: new Date(2014, 9 /* Oct */, 6, 15, 59, 59, 999)
    })
    assert.deepEqual(result, [
      new Date(2014, 9 /* Oct */, 6, 12),
      new Date(2014, 9 /* Oct */, 6, 13),
      new Date(2014, 9 /* Oct */, 6, 14),
      new Date(2014, 9 /* Oct */, 6, 15)
    ])
  })

  it('returns one hour if the both arguments are on the same hour', () => {
    const result = eachHourOfInterval({
      start: new Date(2014, 9 /* Oct */, 6, 12, 35),
      end: new Date(2014, 9 /* Oct */, 6, 12, 47)
    })
    assert.deepEqual(result, [new Date(2014, 9 /* Oct */, 6, 12)])
  })

  it('returns one hour if the both arguments are the same', () => {
    const result = eachHourOfInterval({
      start: new Date(2014, 9 /* Oct */, 6, 12, 35),
      end: new Date(2014, 9 /* Oct */, 6, 12, 35)
    })
    assert.deepEqual(result, [new Date(2014, 9 /* Oct */, 6, 12)])
  })

  it('throws an exception if the start date is after the end date', () => {
    const block = eachHourOfInterval.bind(null, {
      start: new Date(2014, 9 /* Oct */, 12, 35, 0, 0, 1),
      end: new Date(2014, 9 /* Oct */, 12, 35, 0, 0, 0)
    })
    assert.throws(block, RangeError)
  })

  it('throws an exception if the start date is `Invalid Date`', () => {
    const block = eachHourOfInterval.bind(null, {
      start: new Date(NaN),
      end: new Date(2014, 9 /* Oct */, 6, 12)
    })
    assert.throws(block, RangeError)
  })

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

  it('throws an exception if the interval is undefined', () => {
    const block = eachHourOfInterval.bind(
      null,
      // $ExpectedMistake
      undefined
    )
    assert.throws(block, RangeError)
  })

  it('throws TypeError exception if passed less than 1 argument', () => {
    assert.throws(eachHourOfInterval.bind(null), TypeError)
  })

  describe('options.step', () => {
    const interval = {
      start: new Date(2014, 9 /* Oct */, 6, 12),
      end: new Date(2014, 9 /* Oct */, 6, 18)
    }

    const stepError = /^RangeError: `options.step` must be a number greater than 1$/

    it('returns an array with starts of hours from the hour of the start date to the hour of the end date with the given step', () => {
      const result = eachHourOfInterval(interval, { step: 3 })
      assert.deepEqual(result, [
        new Date(2014, 9 /* Oct */, 6, 12),
        new Date(2014, 9 /* Oct */, 6, 15),
        new Date(2014, 9 /* Oct */, 6, 18)
      ])
    })

    it('throws TypeError error if `options.step` is less than 1', () => {
      assert.throws(() => eachHourOfInterval(interval, { step: 0 }), stepError)
      assert.throws(() => eachHourOfInterval(interval, { step: -3 }), stepError)
    })

    it('throws TypeError error if `options.step` is NaN', () => {
      // $ExpectedMistake
      assert.throws(
        () => eachHourOfInterval(interval, { step: 'w' }),
        stepError
      )
      assert.throws(
        () => eachHourOfInterval(interval, { step: NaN }),
        stepError
      )
    })
  })
})