File size: 3,056 Bytes
23ac194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'use strict'

const { test, mockRequire } = require('tap')
const { kThenifyDoNotWrap } = require('../../lib/symbols')

test('thenify', (t) => {
  t.plan(7)

  t.test('return undefined if booted', (t) => {
    t.plan(2)

    const { thenify } = mockRequire('../../lib/thenify', {
      '../../lib/debug': {
        debug: (message) => { t.equal(message, 'thenify returning undefined because we are already booted') }
      }
    })
    const result = thenify.call({
      booted: true
    })
    t.equal(result, undefined)
  })

  t.test('return undefined if kThenifyDoNotWrap is true', (t) => {
    t.plan(1)

    const { thenify } = require('../../lib/thenify')
    const result = thenify.call({
      [kThenifyDoNotWrap]: true
    })
    t.equal(result, undefined)
  })

  t.test('return PromiseConstructorLike if kThenifyDoNotWrap is false', (t) => {
    t.plan(3)

    const { thenify } = mockRequire('../../lib/thenify', {
      '../../lib/debug': {
        debug: (message) => { t.equal(message, 'thenify') }
      }
    })
    const promiseContructorLike = thenify.call({
      [kThenifyDoNotWrap]: false
    })

    t.type(promiseContructorLike, 'function')
    t.equal(promiseContructorLike.length, 2)
  })

  t.test('return PromiseConstructorLike', (t) => {
    t.plan(3)

    const { thenify } = mockRequire('../../lib/thenify', {
      '../../lib/debug': {
        debug: (message) => { t.equal(message, 'thenify') }
      }
    })
    const promiseContructorLike = thenify.call({})

    t.type(promiseContructorLike, 'function')
    t.equal(promiseContructorLike.length, 2)
  })

  t.test('resolve should return _server', async (t) => {
    t.plan(1)

    const { thenify } = require('../../lib/thenify')

    const server = {
      _loadRegistered: () => {
        return Promise.resolve()
      },
      _server: 'server'
    }
    const promiseContructorLike = thenify.call(server)

    promiseContructorLike(function (value) {
      t.equal(value, 'server')
    }, function (reason) {
      t.error(reason)
    })
  })

  t.test('resolving should set kThenifyDoNotWrap to true', async (t) => {
    t.plan(1)

    const { thenify } = require('../../lib/thenify')

    const server = {
      _loadRegistered: () => {
        return Promise.resolve()
      },
      [kThenifyDoNotWrap]: false,
      _server: 'server'
    }
    const promiseContructorLike = thenify.call(server)

    promiseContructorLike(function (value) {
      t.equal(server[kThenifyDoNotWrap], true)
    }, function (reason) {
      t.error(reason)
    })
  })

  t.test('rejection should pass through to reject', async (t) => {
    t.plan(1)

    const { thenify } = require('../../lib/thenify')

    const server = {
      _loadRegistered: () => {
        return Promise.reject(new Error('Arbitrary rejection'))
      },
      _server: 'server'
    }
    const promiseContructorLike = thenify.call(server)

    promiseContructorLike(function (value) {
      t.error(value)
    }, function (reason) {
      t.equal(reason.message, 'Arbitrary rejection')
    })
  })
})