forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi-deprecations-spec.js
170 lines (131 loc) · 4.3 KB
/
api-deprecations-spec.js
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const { deprecations, deprecate } = require('electron')
const { expect } = chai
chai.use(dirtyChai)
describe('deprecations', () => {
beforeEach(() => {
deprecations.setHandler(null)
process.throwDeprecation = true
})
it('allows a deprecation handler function to be specified', () => {
const messages = []
deprecations.setHandler(message => {
messages.push(message)
})
deprecate.log('this is deprecated')
expect(messages).to.deep.equal(['this is deprecated'])
})
it('returns a deprecation handler after one is set', () => {
const messages = []
deprecations.setHandler(message => {
messages.push(message)
})
deprecate.log('this is deprecated')
expect(deprecations.getHandler()).to.be.a('function')
})
it('renames a property', () => {
let msg
deprecations.setHandler(m => { msg = m })
const oldProp = 'dingyOldName'
const newProp = 'shinyNewName'
let value = 0
const o = { [newProp]: value }
expect(o).to.not.have.a.property(oldProp)
expect(o).to.have.a.property(newProp).that.is.a('number')
deprecate.renameProperty(o, oldProp, newProp)
o[oldProp] = ++value
expect(msg).to.be.a('string')
expect(msg).to.include(oldProp)
expect(msg).to.include(newProp)
expect(o).to.have.a.property(newProp).that.is.equal(value)
expect(o).to.have.a.property(oldProp).that.is.equal(value)
})
it('doesn\'t deprecate a property not on an object', () => {
const o = {}
expect(() => {
deprecate.removeProperty(o, 'iDoNotExist')
}).to.throw(/iDoNotExist/)
})
it('deprecates a property of an object', () => {
let msg
deprecations.setHandler(m => { msg = m })
const prop = 'itMustGo'
const o = { [prop]: 0 }
deprecate.removeProperty(o, prop)
const temp = o[prop]
expect(temp).to.equal(0)
expect(msg).to.be.a('string')
expect(msg).to.include(prop)
})
it('warns only once per item', () => {
const messages = []
deprecations.setHandler(message => { messages.push(message) })
const key = 'foo'
const val = 'bar'
const o = { [key]: val }
deprecate.removeProperty(o, key)
for (let i = 0; i < 3; ++i) {
expect(o[key]).to.equal(val)
expect(messages).to.have.length(1)
}
})
it('warns if deprecated property is already set', () => {
let msg
deprecations.setHandler(m => { msg = m })
const oldProp = 'dingyOldName'
const newProp = 'shinyNewName'
const o = { [oldProp]: 0 }
deprecate.renameProperty(o, oldProp, newProp)
expect(msg).to.be.a('string')
expect(msg).to.include(oldProp)
expect(msg).to.include(newProp)
})
it('throws an exception if no deprecation handler is specified', () => {
expect(() => {
deprecate.log('this is deprecated')
}).to.throw(/this is deprecated/)
})
describe('promisify', () => {
const expected = 'Hello, world!'
let promiseFunc
let warnings
const enableCallbackWarnings = () => {
warnings = []
deprecations.setHandler(warning => { warnings.push(warning) })
process.enablePromiseAPIs = true
}
beforeEach(() => {
deprecations.setHandler(null)
process.throwDeprecation = true
promiseFunc = param => new Promise((resolve, reject) => { resolve(param) })
})
it('acts as a pass-through for promise-based invocations', async () => {
enableCallbackWarnings()
promiseFunc = deprecate.promisify(promiseFunc, 1)
const actual = await promiseFunc(expected)
expect(actual).to.equal(expected)
expect(warnings).to.have.lengthOf(0)
})
it('warns exactly once for callback-based invocations', (done) => {
enableCallbackWarnings()
promiseFunc = deprecate.promisify(promiseFunc, 1)
let callbackCount = 0
const invocationCount = 3
const callback = (error, actual) => {
expect(error).to.be.null()
expect(actual).to.equal(expected)
expect(warnings).to.have.lengthOf(1)
expect(warnings[0]).to.include('promiseFunc')
callbackCount += 1
if (callbackCount === invocationCount) {
done()
}
}
for (let i = 0; i < invocationCount; i += 1) {
promiseFunc(expected, callback)
}
})
})
})