api-deprecations-spec.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. const assert = require('assert')
  2. const {deprecations, deprecate, nativeImage} = require('electron')
  3. describe('deprecations', () => {
  4. beforeEach(() => {
  5. deprecations.setHandler(null)
  6. process.throwDeprecation = true
  7. })
  8. it('allows a deprecation handler function to be specified', () => {
  9. const messages = []
  10. deprecations.setHandler((message) => {
  11. messages.push(message)
  12. })
  13. deprecate.log('this is deprecated')
  14. assert.deepEqual(messages, ['this is deprecated'])
  15. })
  16. it('returns a deprecation handler after one is set', () => {
  17. const messages = []
  18. deprecations.setHandler((message) => {
  19. messages.push(message)
  20. })
  21. deprecate.log('this is deprecated')
  22. assert(typeof deprecations.getHandler() === 'function')
  23. })
  24. it('returns a deprecation warning', () => {
  25. const messages = []
  26. deprecations.setHandler((message) => {
  27. messages.push(message)
  28. })
  29. deprecate.warn('old', 'new')
  30. assert.deepEqual(messages, [`'old' is deprecated. Use 'new' instead.`])
  31. })
  32. it('renames a method', () => {
  33. assert.equal(typeof nativeImage.createFromDataUrl, 'undefined')
  34. assert.equal(typeof nativeImage.createFromDataURL, 'function')
  35. deprecate.alias(nativeImage, 'createFromDataUrl', 'createFromDataURL')
  36. assert.equal(typeof nativeImage.createFromDataUrl, 'function')
  37. })
  38. it('renames a property', () => {
  39. let msg
  40. deprecations.setHandler((m) => { msg = m })
  41. const oldPropertyName = 'dingyOldName'
  42. const newPropertyName = 'shinyNewName'
  43. let value = 0
  44. let o = { [newPropertyName]: value }
  45. assert.strictEqual(typeof o[oldPropertyName], 'undefined')
  46. assert.strictEqual(typeof o[newPropertyName], 'number')
  47. deprecate.property(o, oldPropertyName, newPropertyName)
  48. assert.notEqual(typeof msg, 'string')
  49. o[oldPropertyName] = ++value
  50. assert.strictEqual(typeof msg, 'string')
  51. assert.ok(msg.includes(oldPropertyName))
  52. assert.ok(msg.includes(newPropertyName))
  53. assert.strictEqual(o[newPropertyName], value)
  54. assert.strictEqual(o[oldPropertyName], value)
  55. })
  56. it('warns if deprecated property is already set', () => {
  57. let msg
  58. deprecations.setHandler((m) => { msg = m })
  59. const oldPropertyName = 'dingyOldName'
  60. const newPropertyName = 'shinyNewName'
  61. const value = 0
  62. let o = { [oldPropertyName]: value }
  63. deprecate.property(o, oldPropertyName, newPropertyName)
  64. assert.strictEqual(typeof msg, 'string')
  65. assert.ok(msg.includes(oldPropertyName))
  66. assert.ok(msg.includes(newPropertyName))
  67. })
  68. it('throws an exception if no deprecation handler is specified', () => {
  69. assert.throws(() => {
  70. deprecate.log('this is deprecated')
  71. }, /this is deprecated/)
  72. })
  73. })