api-deprecate-spec.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. 'use strict';
  2. const chai = require('chai');
  3. const dirtyChai = require('dirty-chai');
  4. const { deprecate } = require('electron');
  5. const { expect } = chai;
  6. chai.use(dirtyChai);
  7. describe('deprecate', () => {
  8. beforeEach(() => {
  9. deprecate.setHandler(null);
  10. process.throwDeprecation = true;
  11. });
  12. it('allows a deprecation handler function to be specified', () => {
  13. const messages = [];
  14. deprecate.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. deprecate.setHandler(message => {
  23. messages.push(message);
  24. });
  25. deprecate.log('this is deprecated');
  26. expect(deprecate.getHandler()).to.be.a('function');
  27. });
  28. it('renames a property', () => {
  29. let msg;
  30. deprecate.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. deprecate.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 exactly once when a function is deprecated with no replacement', () => {
  63. let msg;
  64. deprecate.setHandler(m => { msg = m; });
  65. function oldFn () { return 'hello'; }
  66. const deprecatedFn = deprecate.removeFunction(oldFn, 'oldFn');
  67. deprecatedFn();
  68. expect(msg).to.be.a('string');
  69. expect(msg).to.include('oldFn');
  70. });
  71. it('warns exactly once when a function is deprecated with a replacement', () => {
  72. let msg;
  73. deprecate.setHandler(m => { msg = m; });
  74. function oldFn () { return 'hello'; }
  75. function newFn () { return 'goodbye'; }
  76. const deprecatedFn = deprecate.renameFunction(oldFn, newFn);
  77. deprecatedFn();
  78. expect(msg).to.be.a('string');
  79. expect(msg).to.include('oldFn');
  80. expect(msg).to.include('newFn');
  81. });
  82. it('warns only once per item', () => {
  83. const messages = [];
  84. deprecate.setHandler(message => messages.push(message));
  85. const key = 'foo';
  86. const val = 'bar';
  87. const o = { [key]: val };
  88. deprecate.removeProperty(o, key);
  89. for (let i = 0; i < 3; ++i) {
  90. expect(o[key]).to.equal(val);
  91. expect(messages).to.have.length(1);
  92. }
  93. });
  94. it('warns if deprecated property is already set', () => {
  95. let msg;
  96. deprecate.setHandler(m => { msg = m; });
  97. const oldProp = 'dingyOldName';
  98. const newProp = 'shinyNewName';
  99. const o = { [oldProp]: 0 };
  100. deprecate.renameProperty(o, oldProp, newProp);
  101. expect(msg).to.be.a('string');
  102. expect(msg).to.include(oldProp);
  103. expect(msg).to.include(newProp);
  104. });
  105. it('throws an exception if no deprecation handler is specified', () => {
  106. expect(() => {
  107. deprecate.log('this is deprecated');
  108. }).to.throw(/this is deprecated/);
  109. });
  110. it('warns when a function is deprecated in favor of a property', () => {
  111. const warnings = [];
  112. deprecate.setHandler(warning => warnings.push(warning));
  113. const newProp = 'newProp';
  114. const mod = {
  115. _oldGetterFn () { return 'getter'; },
  116. _oldSetterFn () { return 'setter'; }
  117. };
  118. deprecate.fnToProperty(mod, 'newProp', '_oldGetterFn', '_oldSetterFn');
  119. mod['oldGetterFn']();
  120. mod['oldSetterFn']();
  121. expect(warnings).to.have.lengthOf(2);
  122. expect(warnings[0]).to.include('oldGetterFn');
  123. expect(warnings[0]).to.include(newProp);
  124. expect(warnings[1]).to.include('oldSetterFn');
  125. expect(warnings[1]).to.include(newProp);
  126. });
  127. describe('moveAPI', () => {
  128. beforeEach(() => {
  129. deprecate.setHandler(null);
  130. });
  131. it('should call the original method', () => {
  132. const warnings = [];
  133. deprecate.setHandler(warning => warnings.push(warning));
  134. let called = false;
  135. const fn = () => {
  136. called = true;
  137. };
  138. const deprecated = deprecate.moveAPI(fn, 'old', 'new');
  139. deprecated();
  140. expect(called).to.equal(true);
  141. });
  142. it('should log the deprecation warning once', () => {
  143. const warnings = [];
  144. deprecate.setHandler(warning => warnings.push(warning));
  145. const deprecated = deprecate.moveAPI(() => null, 'old', 'new');
  146. deprecated();
  147. expect(warnings).to.have.lengthOf(1);
  148. deprecated();
  149. expect(warnings).to.have.lengthOf(1);
  150. expect(warnings[0]).to.equal('\'old\' is deprecated and will be removed. Please use \'new\' instead.');
  151. });
  152. });
  153. describe('promisify', () => {
  154. const expected = 'Hello, world!';
  155. let promiseFunc;
  156. let warnings;
  157. const enableCallbackWarnings = () => {
  158. warnings = [];
  159. deprecate.setHandler(warning => warnings.push(warning));
  160. process.enablePromiseAPIs = true;
  161. };
  162. beforeEach(() => {
  163. deprecate.setHandler(null);
  164. process.throwDeprecation = true;
  165. promiseFunc = param => new Promise((resolve, reject) => resolve(param));
  166. });
  167. it('acts as a pass-through for promise-based invocations', async () => {
  168. enableCallbackWarnings();
  169. promiseFunc = deprecate.promisify(promiseFunc);
  170. const actual = await promiseFunc(expected);
  171. expect(actual).to.equal(expected);
  172. expect(warnings).to.have.lengthOf(0);
  173. });
  174. it('only calls back an error if the callback is called with (err, data)', (done) => {
  175. enableCallbackWarnings();
  176. let erringPromiseFunc = () => new Promise((resolve, reject) => {
  177. reject(new Error('fail'));
  178. });
  179. erringPromiseFunc = deprecate.promisify(erringPromiseFunc);
  180. erringPromiseFunc((err, data) => {
  181. expect(data).to.be.an('undefined');
  182. expect(err).to.be.an.instanceOf(Error).with.property('message', 'fail');
  183. erringPromiseFunc(data => {
  184. expect(data).to.not.be.an.instanceOf(Error);
  185. expect(data).to.be.an('undefined');
  186. done();
  187. });
  188. });
  189. });
  190. it('warns exactly once for callback-based invocations', (done) => {
  191. enableCallbackWarnings();
  192. promiseFunc = deprecate.promisify(promiseFunc);
  193. let callbackCount = 0;
  194. const invocationCount = 3;
  195. const callback = (actual) => {
  196. expect(actual).to.equal(expected);
  197. expect(warnings).to.have.lengthOf(1);
  198. expect(warnings[0]).to.include('promiseFunc');
  199. callbackCount += 1;
  200. if (callbackCount === invocationCount) {
  201. done();
  202. }
  203. };
  204. for (let i = 0; i < invocationCount; i += 1) {
  205. promiseFunc(expected, callback);
  206. }
  207. });
  208. });
  209. });