2019-09-04 18:13:37 +00:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import { deprecate } from 'electron'
|
2018-06-19 15:22:08 +00:00
|
|
|
|
2019-05-20 22:40:49 +00:00
|
|
|
describe('deprecate', () => {
|
2017-10-27 00:35:33 +00:00
|
|
|
beforeEach(() => {
|
2019-05-20 22:40:49 +00:00
|
|
|
deprecate.setHandler(null)
|
2016-03-25 20:03:49 +00:00
|
|
|
process.throwDeprecation = true
|
|
|
|
})
|
2016-02-16 23:09:35 +00:00
|
|
|
|
2017-11-14 19:56:16 +00:00
|
|
|
it('allows a deprecation handler function to be specified', () => {
|
2019-09-04 18:13:37 +00:00
|
|
|
const messages: string[] = []
|
2017-11-14 01:21:57 +00:00
|
|
|
|
2019-05-20 22:40:49 +00:00
|
|
|
deprecate.setHandler(message => {
|
2017-11-14 19:56:16 +00:00
|
|
|
messages.push(message)
|
|
|
|
})
|
|
|
|
|
|
|
|
deprecate.log('this is deprecated')
|
2018-06-19 15:22:08 +00:00
|
|
|
expect(messages).to.deep.equal(['this is deprecated'])
|
2017-11-14 19:56:16 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('returns a deprecation handler after one is set', () => {
|
|
|
|
const messages = []
|
|
|
|
|
2019-05-20 22:40:49 +00:00
|
|
|
deprecate.setHandler(message => {
|
2017-11-14 19:56:16 +00:00
|
|
|
messages.push(message)
|
|
|
|
})
|
|
|
|
|
|
|
|
deprecate.log('this is deprecated')
|
2019-05-20 22:40:49 +00:00
|
|
|
expect(deprecate.getHandler()).to.be.a('function')
|
2017-11-14 19:56:16 +00:00
|
|
|
})
|
|
|
|
|
2018-05-29 13:40:48 +00:00
|
|
|
it('renames a property', () => {
|
|
|
|
let msg
|
2019-05-20 22:40:49 +00:00
|
|
|
deprecate.setHandler(m => { msg = m })
|
2018-05-29 13:40:48 +00:00
|
|
|
|
2018-09-11 18:16:49 +00:00
|
|
|
const oldProp = 'dingyOldName'
|
|
|
|
const newProp = 'shinyNewName'
|
2018-05-29 13:40:48 +00:00
|
|
|
|
|
|
|
let value = 0
|
2019-09-04 18:13:37 +00:00
|
|
|
const o: Record<string, number> = { [newProp]: value }
|
|
|
|
expect(o).to.not.have.property(oldProp)
|
|
|
|
expect(o).to.have.property(newProp).that.is.a('number')
|
2018-05-29 13:40:48 +00:00
|
|
|
|
2018-09-11 18:16:49 +00:00
|
|
|
deprecate.renameProperty(o, oldProp, newProp)
|
|
|
|
o[oldProp] = ++value
|
2018-05-29 13:40:48 +00:00
|
|
|
|
2018-06-19 15:22:08 +00:00
|
|
|
expect(msg).to.be.a('string')
|
2018-09-11 18:16:49 +00:00
|
|
|
expect(msg).to.include(oldProp)
|
|
|
|
expect(msg).to.include(newProp)
|
2018-05-29 13:40:48 +00:00
|
|
|
|
2019-09-04 18:13:37 +00:00
|
|
|
expect(o).to.have.property(newProp).that.is.equal(value)
|
|
|
|
expect(o).to.have.property(oldProp).that.is.equal(value)
|
2018-09-11 18:16:49 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('doesn\'t deprecate a property not on an object', () => {
|
2019-09-04 18:13:37 +00:00
|
|
|
const o: any = {}
|
2018-09-11 18:16:49 +00:00
|
|
|
|
|
|
|
expect(() => {
|
2018-09-12 22:13:22 +00:00
|
|
|
deprecate.removeProperty(o, 'iDoNotExist')
|
|
|
|
}).to.throw(/iDoNotExist/)
|
2018-05-29 13:40:48 +00:00
|
|
|
})
|
|
|
|
|
2018-06-27 06:47:01 +00:00
|
|
|
it('deprecates a property of an object', () => {
|
|
|
|
let msg
|
2019-05-20 22:40:49 +00:00
|
|
|
deprecate.setHandler(m => { msg = m })
|
2018-06-27 06:47:01 +00:00
|
|
|
|
2018-09-11 18:16:49 +00:00
|
|
|
const prop = 'itMustGo'
|
2018-10-02 01:56:31 +00:00
|
|
|
const o = { [prop]: 0 }
|
2018-09-11 18:16:49 +00:00
|
|
|
|
|
|
|
deprecate.removeProperty(o, prop)
|
2018-06-27 06:47:01 +00:00
|
|
|
|
2018-09-11 18:16:49 +00:00
|
|
|
const temp = o[prop]
|
2018-06-27 06:47:01 +00:00
|
|
|
|
2018-09-11 18:16:49 +00:00
|
|
|
expect(temp).to.equal(0)
|
2018-06-27 06:47:01 +00:00
|
|
|
expect(msg).to.be.a('string')
|
2018-09-11 18:16:49 +00:00
|
|
|
expect(msg).to.include(prop)
|
2018-06-27 06:47:01 +00:00
|
|
|
})
|
|
|
|
|
2019-02-08 17:50:11 +00:00
|
|
|
it('warns exactly once when a function is deprecated with no replacement', () => {
|
|
|
|
let msg
|
2019-05-20 22:40:49 +00:00
|
|
|
deprecate.setHandler(m => { msg = m })
|
2019-02-08 17:50:11 +00:00
|
|
|
|
|
|
|
function oldFn () { return 'hello' }
|
2019-07-09 01:01:35 +00:00
|
|
|
const deprecatedFn = deprecate.removeFunction(oldFn, 'oldFn')
|
2019-02-08 17:50:11 +00:00
|
|
|
deprecatedFn()
|
|
|
|
|
|
|
|
expect(msg).to.be.a('string')
|
|
|
|
expect(msg).to.include('oldFn')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('warns exactly once when a function is deprecated with a replacement', () => {
|
|
|
|
let msg
|
2019-05-20 22:40:49 +00:00
|
|
|
deprecate.setHandler(m => { msg = m })
|
2019-02-08 17:50:11 +00:00
|
|
|
|
|
|
|
function oldFn () { return 'hello' }
|
|
|
|
function newFn () { return 'goodbye' }
|
2019-07-09 01:01:35 +00:00
|
|
|
const deprecatedFn = deprecate.renameFunction(oldFn, newFn)
|
2019-02-08 17:50:11 +00:00
|
|
|
deprecatedFn()
|
|
|
|
|
|
|
|
expect(msg).to.be.a('string')
|
|
|
|
expect(msg).to.include('oldFn')
|
|
|
|
expect(msg).to.include('newFn')
|
|
|
|
})
|
|
|
|
|
2018-09-12 22:13:22 +00:00
|
|
|
it('warns only once per item', () => {
|
2019-09-04 18:13:37 +00:00
|
|
|
const messages: string[] = []
|
2019-05-20 22:40:49 +00:00
|
|
|
deprecate.setHandler(message => messages.push(message))
|
2018-09-12 22:13:22 +00:00
|
|
|
|
|
|
|
const key = 'foo'
|
|
|
|
const val = 'bar'
|
2018-10-02 01:56:31 +00:00
|
|
|
const o = { [key]: val }
|
2018-09-12 22:13:22 +00:00
|
|
|
deprecate.removeProperty(o, key)
|
|
|
|
|
|
|
|
for (let i = 0; i < 3; ++i) {
|
|
|
|
expect(o[key]).to.equal(val)
|
|
|
|
expect(messages).to.have.length(1)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-05-29 13:58:02 +00:00
|
|
|
it('warns if deprecated property is already set', () => {
|
|
|
|
let msg
|
2019-05-20 22:40:49 +00:00
|
|
|
deprecate.setHandler(m => { msg = m })
|
2018-05-29 13:58:02 +00:00
|
|
|
|
2018-09-11 18:16:49 +00:00
|
|
|
const oldProp = 'dingyOldName'
|
|
|
|
const newProp = 'shinyNewName'
|
2018-05-29 13:58:02 +00:00
|
|
|
|
2019-09-04 18:13:37 +00:00
|
|
|
const o: Record<string, number> = { [oldProp]: 0 }
|
2018-09-11 18:16:49 +00:00
|
|
|
deprecate.renameProperty(o, oldProp, newProp)
|
2018-05-29 13:58:02 +00:00
|
|
|
|
2018-06-19 15:22:08 +00:00
|
|
|
expect(msg).to.be.a('string')
|
2018-09-11 18:16:49 +00:00
|
|
|
expect(msg).to.include(oldProp)
|
|
|
|
expect(msg).to.include(newProp)
|
2018-05-29 13:58:02 +00:00
|
|
|
})
|
|
|
|
|
2017-11-14 19:56:16 +00:00
|
|
|
it('throws an exception if no deprecation handler is specified', () => {
|
2018-06-19 15:22:08 +00:00
|
|
|
expect(() => {
|
2017-11-14 19:56:16 +00:00
|
|
|
deprecate.log('this is deprecated')
|
2018-06-19 15:22:08 +00:00
|
|
|
}).to.throw(/this is deprecated/)
|
2017-11-14 19:56:16 +00:00
|
|
|
})
|
2018-12-10 16:13:09 +00:00
|
|
|
|
2019-03-14 22:19:19 +00:00
|
|
|
it('warns when a function is deprecated in favor of a property', () => {
|
2019-09-04 18:13:37 +00:00
|
|
|
const warnings: string[] = []
|
2019-05-20 22:40:49 +00:00
|
|
|
deprecate.setHandler(warning => warnings.push(warning))
|
2019-03-14 22:19:19 +00:00
|
|
|
|
2019-04-05 02:49:04 +00:00
|
|
|
const newProp = 'newProp'
|
2019-09-04 18:13:37 +00:00
|
|
|
const mod: any = {
|
2019-04-05 02:49:04 +00:00
|
|
|
_oldGetterFn () { return 'getter' },
|
|
|
|
_oldSetterFn () { return 'setter' }
|
|
|
|
}
|
2019-03-14 22:19:19 +00:00
|
|
|
|
2019-04-05 02:49:04 +00:00
|
|
|
deprecate.fnToProperty(mod, 'newProp', '_oldGetterFn', '_oldSetterFn')
|
2019-03-14 22:19:19 +00:00
|
|
|
|
2019-04-05 02:49:04 +00:00
|
|
|
mod['oldGetterFn']()
|
|
|
|
mod['oldSetterFn']()
|
2019-03-14 22:19:19 +00:00
|
|
|
|
|
|
|
expect(warnings).to.have.lengthOf(2)
|
|
|
|
|
|
|
|
expect(warnings[0]).to.include('oldGetterFn')
|
|
|
|
expect(warnings[0]).to.include(newProp)
|
|
|
|
|
|
|
|
expect(warnings[1]).to.include('oldSetterFn')
|
|
|
|
expect(warnings[1]).to.include(newProp)
|
|
|
|
})
|
|
|
|
|
2019-08-14 20:42:55 +00:00
|
|
|
describe('moveAPI', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
deprecate.setHandler(null)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should call the original method', () => {
|
|
|
|
const warnings = []
|
|
|
|
deprecate.setHandler(warning => warnings.push(warning))
|
|
|
|
|
|
|
|
let called = false
|
|
|
|
const fn = () => {
|
|
|
|
called = true
|
|
|
|
}
|
|
|
|
const deprecated = deprecate.moveAPI(fn, 'old', 'new')
|
|
|
|
deprecated()
|
|
|
|
expect(called).to.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should log the deprecation warning once', () => {
|
2019-09-04 18:13:37 +00:00
|
|
|
const warnings: string[] = []
|
2019-08-14 20:42:55 +00:00
|
|
|
deprecate.setHandler(warning => warnings.push(warning))
|
|
|
|
|
|
|
|
const deprecated = deprecate.moveAPI(() => null, 'old', 'new')
|
|
|
|
deprecated()
|
|
|
|
expect(warnings).to.have.lengthOf(1)
|
|
|
|
deprecated()
|
|
|
|
expect(warnings).to.have.lengthOf(1)
|
|
|
|
expect(warnings[0]).to.equal('\'old\' is deprecated and will be removed. Please use \'new\' instead.')
|
|
|
|
})
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|