electron/spec/api-deprecations-spec.js

121 lines
2.9 KiB
JavaScript
Raw Normal View History

'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
2018-09-13 16:10:51 +00:00
const { deprecations, deprecate } = require('electron')
2018-09-13 16:10:51 +00:00
const { expect } = chai
chai.use(dirtyChai)
describe('deprecations', () => {
2017-10-27 00:35:33 +00:00
beforeEach(() => {
2016-03-25 20:03:49 +00:00
deprecations.setHandler(null)
process.throwDeprecation = true
})
2017-11-14 19:56:16 +00:00
it('allows a deprecation handler function to be specified', () => {
const messages = []
2017-11-14 01:21:57 +00:00
deprecations.setHandler(message => {
2017-11-14 19:56:16 +00:00
messages.push(message)
})
deprecate.log('this is deprecated')
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 = []
deprecations.setHandler(message => {
2017-11-14 19:56:16 +00:00
messages.push(message)
})
deprecate.log('this is deprecated')
expect(deprecations.getHandler()).to.be.a('function')
2017-11-14 19:56:16 +00:00
})
it('renames a property', () => {
let msg
deprecations.setHandler(m => { msg = m })
const oldProp = 'dingyOldName'
const newProp = 'shinyNewName'
let value = 0
2018-09-13 16:10:51 +00:00
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'
2018-09-13 16:10:51 +00:00
let 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'
2018-09-13 16:10:51 +00:00
let 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'
2018-09-13 16:10:51 +00:00
let o = { [oldProp]: 0 }
deprecate.renameProperty(o, oldProp, newProp)
expect(msg).to.be.a('string')
expect(msg).to.include(oldProp)
expect(msg).to.include(newProp)
})
2017-11-14 19:56:16 +00:00
it('throws an exception if no deprecation handler is specified', () => {
expect(() => {
2017-11-14 19:56:16 +00:00
deprecate.log('this is deprecated')
}).to.throw(/this is deprecated/)
2017-11-14 19:56:16 +00:00
})
2016-03-25 20:03:49 +00:00
})