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

import assert from 'assert'
import compareDesc from '.'

describe('compareDesc', function() {
  it('returns 0 if the given dates are equal', function() {
    const result = compareDesc(
      new Date(1989, 6 /* Jul */, 10),
      new Date(1989, 6 /* Jul */, 10)
    )
    assert(result === 0)
  })

  it('returns 1 if the first date is before the second one', function() {
    const result = compareDesc(
      new Date(1987, 1 /* Feb */, 11),
      new Date(1989, 6 /* Jul */, 10)
    )
    assert(result === 1)
  })

  it('returns -1 if the first date is after the second one', function() {
    const result = compareDesc(
      new Date(1989, 6 /* Jul */, 10),
      new Date(1987, 1 /* Feb */, 11)
    )
    assert(result === -1)
  })

  it('sorts the dates array in the reverse chronological order when function is passed as the argument to Array.prototype.sort()', function() {
    const unsortedArray = [
      new Date(1995, 6 /* Jul */, 2),
      new Date(1987, 1 /* Feb */, 11),
      new Date(1989, 6 /* Jul */, 10)
    ]

    const sortedArray = [
      new Date(1995, 6 /* Jul */, 2),
      new Date(1989, 6 /* Jul */, 10),
      new Date(1987, 1 /* Feb */, 11)
    ]

    unsortedArray.sort(compareDesc)
    const result = unsortedArray

    assert.deepStrictEqual(result, sortedArray)
  })

  it('accepts timestamps', function() {
    const result = compareDesc(
      new Date(1987, 1 /* Feb */, 11).getTime(),
      new Date(1989, 6 /* Jul */, 10).getTime()
    )
    assert(result === 1)
  })

  it('returns NaN if the first date is `Invalid Date`', function() {
    const result = compareDesc(new Date(NaN), new Date(1989, 6 /* Jul */, 10))
    assert(isNaN(result))
  })

  it('returns NaN if the second date is `Invalid Date`', function() {
    const result = compareDesc(new Date(1989, 6 /* Jul */, 10), new Date(NaN))
    assert(isNaN(result))
  })

  it('returns NaN if the both dates are `Invalid Date`', function() {
    const result = compareDesc(new Date(1989, 6 /* Jul */, 10), new Date(NaN))
    assert(isNaN(result))
  })

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