From dc410efa36c890b0392ecfcf6e940a789b7d625d Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Tue, 14 Nov 2017 14:56:16 -0500 Subject: [PATCH] rename and fix assoc. test --- lib/common/api/deprecate.js | 13 ++--- spec/api-deprecations-spec.js | 92 ++++++++++++++++++----------------- 2 files changed, 54 insertions(+), 51 deletions(-) diff --git a/lib/common/api/deprecate.js b/lib/common/api/deprecate.js index 6d041ca0de..9c0e39c3a4 100644 --- a/lib/common/api/deprecate.js +++ b/lib/common/api/deprecate.js @@ -11,20 +11,21 @@ const deprecate = function (oldName, newName, fn) { } // The method is renamed. -deprecate.rename = (object, oldName, newName) => { - console.log('we are here') +// nota bene: newName should already exist and +// oldName is being injected for compatibility with old code +deprecate.alias = function (object, deprecatedName, existingName) { let warned = false const newMethod = function () { if (!(warned || process.noDeprecation)) { warned = true - deprecate.warn(oldName, newName) + deprecate.warn(deprecatedName, existingName) } - return this[newName].apply(this, arguments) + return this[existingName].apply(this, arguments) } if (typeof object === 'function') { - object.prototype[oldName] = newMethod + object.prototype[deprecatedName] = newMethod } else { - object[oldName] = newMethod + object[deprecatedName] = newMethod } } diff --git a/spec/api-deprecations-spec.js b/spec/api-deprecations-spec.js index 0f83de2622..7a32c7a894 100644 --- a/spec/api-deprecations-spec.js +++ b/spec/api-deprecations-spec.js @@ -1,5 +1,5 @@ const assert = require('assert') -const {deprecations, deprecate, ipcRenderer} = require('electron') +const {deprecations, deprecate, nativeImage} = require('electron') describe.only('deprecations', () => { beforeEach(() => { @@ -7,44 +7,53 @@ describe.only('deprecations', () => { 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') - // assert.deepEqual(messages, ['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') - // assert(typeof deprecations.getHandler() === 'function') - // }) - // - // it('returns a deprecation warning', () => { - // const messages = [] - // - // deprecations.setHandler((message) => { - // messages.push(message) - // }) - // - // deprecate.warn('old', 'new') - // assert.deepEqual(messages, [`'old' is deprecated. Use 'new' instead.`]) - // }) + it('allows a deprecation handler function to be specified', () => { + const messages = [] - // it('throws an exception if no deprecation handler is specified', () => { - // assert.throws(() => { - // deprecate.log('this is deprecated') - // }, /this is deprecated/) - // }) + deprecations.setHandler((message) => { + messages.push(message) + }) + + deprecate.log('this is deprecated') + assert.deepEqual(messages, ['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') + assert(typeof deprecations.getHandler() === 'function') + }) + + it('returns a deprecation warning', () => { + const messages = [] + + deprecations.setHandler((message) => { + messages.push(message) + }) + + deprecate.warn('old', 'new') + assert.deepEqual(messages, [`'old' is deprecated. Use 'new' instead.`]) + }) + + it('renames a method', () => { + assert.equal(typeof nativeImage.createFromDataUrl, 'undefined') + assert.equal(typeof nativeImage.createFromDataURL, 'function') + + deprecate.alias(nativeImage, 'createFromDataUrl', 'createFromDataURL') + + assert.equal(typeof nativeImage.createFromDataUrl, 'function') + }) + + it('throws an exception if no deprecation handler is specified', () => { + assert.throws(() => { + deprecate.log('this is deprecated') + }, /this is deprecated/) + }) // it('deprecates a property', () => { // deprecate.property(object, property, method) @@ -57,11 +66,4 @@ describe.only('deprecations', () => { // it('forwards a method to member', () => { // deprecate.member(object, method, member) // }) - - it('renames a method', () => { - assert(typeof ipcRenderer.sendSync === 'function') - assert(typeof ipcRenderer.sendChannelSync === 'undefined') - deprecate.rename(ipcRenderer, 'sendSync', 'sendChannelSync') - assert(typeof ipcRenderer.sendChannelSync === 'function') - }) })