summaryrefslogtreecommitdiff
path: root/date-fns/src/isDate/test.ts
blob: 7d877891b8c276e4d30b29a6df8a1befac38a5c1 (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
// @flow
/* eslint-env mocha */
/* global HTMLIFrameElement */

import assert from 'assert'
import { assertType } from '../_lib/test'
import isDate from '.'

describe('isDate', () => {
  it('returns true if the given value is a date object', () => {
    assert(isDate(new Date()))
  })

  it('returns true if the given value is an Invalid Date', () => {
    assert(isDate(new Date(NaN)))
  })

  it('ensures that the passed argument is an instance of Date', () => {
    const date: unknown = new Date()

    if (isDate(date)) {
      assertType<Date>(date)
    } else {
      assertType<unknown>(date)
    }
  })

  describe('with date passed from another iframe', () => {
    afterEach(() => {
      const iframe = document.getElementById('iframe')
      iframe && iframe.remove()
    })

    // Emulate web browser
    if (!process.env.JEST_WORKER_ID) {
      it('returns true for a date passed from another iframe', (done) => {
        const iframe = document.createElement('iframe')
        iframe.id = 'iframe'
        iframe.addEventListener('load', () => {
          execScript('window.date = new Date()') // eslint-disable-line no-implied-eval
          // @ts-expect-error
          assert(isDate(iframe.contentWindow.date))
          // $ExpectedMistake sadly, but Flow doesn't know about Mocha's done
          done()
        })
        if (!document.body) throw new Error('document.body is not defined')
        document.body.appendChild(iframe)
      })
    }

    function execScript(scriptText: string) {
      const iframe = document.querySelector('iframe#iframe')
      if (!iframe || !(iframe instanceof HTMLIFrameElement)) {
        throw new Error("Can't execute the script because iframe isn't found")
      }
      const doc = iframe.contentDocument!
      const script = doc.createElement('script')
      script.innerText = scriptText
      // @ts-expect-error
      if (!(doc.body instanceof iframe.contentWindow.HTMLBodyElement)) {
        throw new Error(
          "Can't execute the script because iframe does not have body"
        )
      }
      doc.body.append(script)
    }
  })

  it('returns false if the given value is not a date object', () => {
    assert(!isDate(new Date().getTime()))
    assert(!isDate(new Date().toISOString()))
    assert(!isDate({}))
    assert(!isDate(null))
    assert(!isDate(0))
  })

  it('throws TypeError exception if passed less than 1 argument', () => {
    // @ts-expect-error
    assert.throws(isDate.bind(null), TypeError)
  })
})