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

import assert from 'power-assert'
import set from '.'

describe('set', function() {
  it('sets all values', function() {
    const result = set(new Date(2013, 0 /* Jan */), {
      year: 2014,
      month: 8, // Sep
      date: 20,
      hours: 12,
      minutes: 12,
      seconds: 12,
      milliseconds: 12
    })
    assert.deepEqual(
      result.toString(),
      new Date(2014, 8 /* Sep */, 20, 12, 12, 12, 12).toString()
    )
  })

  it('sets year', function() {
    const result = set(new Date(2013, 8 /* Sep */), { year: 2014 })
    assert.deepEqual(result, new Date(2014, 8 /* Sep */))
  })

  it('sets month', function() {
    const result = set(new Date(2014, 8 /* Sep */), { month: 9 /* Oct */ })
    assert.deepEqual(result, new Date(2014, 9 /* Oct */))
  })

  it('sets day of month', function() {
    const result = set(new Date(2014, 8 /* Sep */), { date: 20 })
    assert.deepEqual(result, new Date(2014, 8 /* Sep */, 20))
  })

  it('sets hours', function() {
    const result = set(new Date(2014, 8 /* Sep */, 1), { hours: 12 })
    assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 12))
  })

  it('sets minutes', function() {
    const result = set(new Date(2014, 8 /* Sep */, 1, 1), { minutes: 12 })
    assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 1, 12))
  })

  it('sets seconds', function() {
    const result = set(new Date(2014, 8 /* Sep */, 1, 1, 1), { seconds: 12 })
    assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 1, 1, 12))
  })

  it('sets milliseconds', function() {
    const result = set(new Date(2014, 8 /* Sep */, 1, 1, 1, 1), {
      milliseconds: 500
    })
    assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 1, 1, 1, 500))
  })

  describe('value overflow', function() {
    it('months overflow into years', function() {
      const result = set(new Date(2014, 8 /* Sep */, 1), {
        month: 12 /* 13th month */
      })
      assert.deepEqual(result, new Date(2015, 0 /* Jan */, 1))
    })

    it('days of months overflow into months', function() {
      const result = set(new Date(2014, 8 /* Sep */, 1), { date: 31 })
      assert.deepEqual(result, new Date(2014, 9 /* Oct */, 1))
    })

    it('hours overflow into days', function() {
      const result = set(new Date(2014, 8 /* Sep */, 19), { hours: 24 })
      assert.deepEqual(result, new Date(2014, 8 /* Sep */, 20))
    })

    it('minutes overflow into hours', function() {
      const result = set(new Date(2014, 8 /* Sep */, 20, 11), { minutes: 60 })
      assert.deepEqual(result, new Date(2014, 8 /* Sep */, 20, 12))
    })

    it('seconds overflow into minutes', function() {
      const result = set(new Date(2014, 8 /* Sep */, 20, 12, 58), { seconds: 60 })
      assert.deepEqual(result, new Date(2014, 8 /* Sep */, 20, 12, 59))
    })

    it('milliseconds overflow into seconds', function() {
      const result = set(new Date(2014, 8 /* Sep */, 20, 12, 58, 30), {
        milliseconds: 1000
      })
      assert.deepEqual(result, new Date(2014, 8 /* Sep */, 20, 12, 58, 31))
    })
  })

  describe('edge cases', function() {
    it('sets January', function() {
      const result = set(new Date(2014, 8 /* Sep */), { month: 0 /* Jan */ })
      assert.deepEqual(result, new Date(2014, 0 /* Jan */))
    })

    it('sets the last day of new month if the initial date was the last day of a longer month', function() {
      const result = set(new Date(2014, 7 /* Aug */, 31), { month: 8 /* Sep */ })
      assert.deepEqual(result, new Date(2014, 8 /* Sep */, 30))
    })

    it('ignores undefined values', function() {
      const result = set(new Date(2014, 8 /* Sep */), { year: undefined })
      assert.deepEqual(result, new Date(2014, 8 /* Sep */))
    })

    it('ignores null values', function() {
      // @ts-expect-error
      const result = set(new Date(2014, 8 /* Sep */), { year: null })
      assert.deepEqual(result, new Date(2014, 8 /* Sep */))
    })

    it('throws TypeError exception if passed less than 2 arguments', function() {
      assert.throws(set.bind(null), TypeError)
    })

    it('returns Invalid Date if any value in values is NaN', function() {
      const result = set(new Date(2014, 8 /* Sep */), { year: NaN })
      assert.deepEqual(isNaN(result.getTime()), isNaN((new Date(NaN)).getTime()))
    })

    it('returns Invalid Date the initial date was Invalid Date as well', function() {
      const result = set(new Date(NaN), { year: 2019 })
      assert.deepEqual(isNaN(result.getTime()), isNaN((new Date(NaN)).getTime()))
    })

    it('throws RangeError exception if `values` is not an object', function() {
      // @ts-expect-error
      assert.throws(set.bind(null, new Date(), true), RangeError)
    })

    it('throws RangeError exception if `values` is null', function() {
      // @ts-expect-error
      assert.throws(set.bind(null, new Date(), null), RangeError)
    })
  })
})