api-deprecations-spec.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 'use strict'
  2. const chai = require('chai')
  3. const dirtyChai = require('dirty-chai')
  4. const { deprecations, deprecate } = require('electron')
  5. const { expect } = chai
  6. chai.use(dirtyChai)
  7. describe('deprecations', () => {
  8. beforeEach(() => {
  9. deprecations.setHandler(null)
  10. process.throwDeprecation = true
  11. })
  12. it('allows a deprecation handler function to be specified', () => {
  13. const messages = []
  14. deprecations.setHandler(message => {
  15. messages.push(message)
  16. })
  17. deprecate.log('this is deprecated')
  18. expect(messages).to.deep.equal(['this is deprecated'])
  19. })
  20. it('returns a deprecation handler after one is set', () => {
  21. const messages = []
  22. deprecations.setHandler(message => {
  23. messages.push(message)
  24. })
  25. deprecate.log('this is deprecated')
  26. expect(deprecations.getHandler()).to.be.a('function')
  27. })
  28. it('renames a property', () => {
  29. let msg
  30. deprecations.setHandler(m => { msg = m })
  31. const oldProp = 'dingyOldName'
  32. const newProp = 'shinyNewName'
  33. let value = 0
  34. const o = { [newProp]: value }
  35. expect(o).to.not.have.a.property(oldProp)
  36. expect(o).to.have.a.property(newProp).that.is.a('number')
  37. deprecate.renameProperty(o, oldProp, newProp)
  38. o[oldProp] = ++value
  39. expect(msg).to.be.a('string')
  40. expect(msg).to.include(oldProp)
  41. expect(msg).to.include(newProp)
  42. expect(o).to.have.a.property(newProp).that.is.equal(value)
  43. expect(o).to.have.a.property(oldProp).that.is.equal(value)
  44. })
  45. it('doesn\'t deprecate a property not on an object', () => {
  46. const o = {}
  47. expect(() => {
  48. deprecate.removeProperty(o, 'iDoNotExist')
  49. }).to.throw(/iDoNotExist/)
  50. })
  51. it('deprecates a property of an object', () => {
  52. let msg
  53. deprecations.setHandler(m => { msg = m })
  54. const prop = 'itMustGo'
  55. const o = { [prop]: 0 }
  56. deprecate.removeProperty(o, prop)
  57. const temp = o[prop]
  58. expect(temp).to.equal(0)
  59. expect(msg).to.be.a('string')
  60. expect(msg).to.include(prop)
  61. })
  62. it('warns only once per item', () => {
  63. const messages = []
  64. deprecations.setHandler(message => { messages.push(message) })
  65. const key = 'foo'
  66. const val = 'bar'
  67. const o = { [key]: val }
  68. deprecate.removeProperty(o, key)
  69. for (let i = 0; i < 3; ++i) {
  70. expect(o[key]).to.equal(val)
  71. expect(messages).to.have.length(1)
  72. }
  73. })
  74. it('warns if deprecated property is already set', () => {
  75. let msg
  76. deprecations.setHandler(m => { msg = m })
  77. const oldProp = 'dingyOldName'
  78. const newProp = 'shinyNewName'
  79. const o = { [oldProp]: 0 }
  80. deprecate.renameProperty(o, oldProp, newProp)
  81. expect(msg).to.be.a('string')
  82. expect(msg).to.include(oldProp)
  83. expect(msg).to.include(newProp)
  84. })
  85. it('throws an exception if no deprecation handler is specified', () => {
  86. expect(() => {
  87. deprecate.log('this is deprecated')
  88. }).to.throw(/this is deprecated/)
  89. })
  90. })