api-deprecate-spec.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import { expect } from 'chai';
  2. import { deprecate } from 'electron';
  3. describe('deprecate', () => {
  4. let throwing: boolean;
  5. beforeEach(() => {
  6. throwing = process.throwDeprecation;
  7. deprecate.setHandler(null);
  8. process.throwDeprecation = true;
  9. });
  10. afterEach(() => {
  11. process.throwDeprecation = throwing;
  12. });
  13. it('allows a deprecation handler function to be specified', () => {
  14. const messages: string[] = [];
  15. deprecate.setHandler(message => {
  16. messages.push(message);
  17. });
  18. deprecate.log('this is deprecated');
  19. expect(messages).to.deep.equal(['this is deprecated']);
  20. });
  21. it('returns a deprecation handler after one is set', () => {
  22. const messages = [];
  23. deprecate.setHandler(message => {
  24. messages.push(message);
  25. });
  26. deprecate.log('this is deprecated');
  27. expect(deprecate.getHandler()).to.be.a('function');
  28. });
  29. it('renames a property', () => {
  30. let msg;
  31. deprecate.setHandler(m => { msg = m; });
  32. const oldProp = 'dingyOldName';
  33. const newProp = 'shinyNewName';
  34. let value = 0;
  35. const o: Record<string, number> = { [newProp]: value };
  36. expect(o).to.not.have.property(oldProp);
  37. expect(o).to.have.property(newProp).that.is.a('number');
  38. deprecate.renameProperty(o, oldProp, newProp);
  39. o[oldProp] = ++value;
  40. expect(msg).to.be.a('string');
  41. expect(msg).to.include(oldProp);
  42. expect(msg).to.include(newProp);
  43. expect(o).to.have.property(newProp).that.is.equal(value);
  44. expect(o).to.have.property(oldProp).that.is.equal(value);
  45. });
  46. it('doesn\'t deprecate a property not on an object', () => {
  47. const o: any = {};
  48. expect(() => {
  49. deprecate.removeProperty(o, 'iDoNotExist');
  50. }).to.throw(/iDoNotExist/);
  51. });
  52. it('deprecates a property of an object', () => {
  53. let msg;
  54. deprecate.setHandler(m => { msg = m; });
  55. const prop = 'itMustGo';
  56. const o = { [prop]: 0 };
  57. deprecate.removeProperty(o, prop);
  58. const temp = o[prop];
  59. expect(temp).to.equal(0);
  60. expect(msg).to.be.a('string');
  61. expect(msg).to.include(prop);
  62. });
  63. it('warns exactly once when a function is deprecated with no replacement', () => {
  64. let msg;
  65. deprecate.setHandler(m => { msg = m; });
  66. function oldFn () { return 'hello'; }
  67. const deprecatedFn = deprecate.removeFunction(oldFn, 'oldFn');
  68. deprecatedFn();
  69. expect(msg).to.be.a('string');
  70. expect(msg).to.include('oldFn');
  71. });
  72. it('warns exactly once when a function is deprecated with a replacement', () => {
  73. let msg;
  74. deprecate.setHandler(m => { msg = m; });
  75. function oldFn () { return 'hello'; }
  76. function newFn () { return 'goodbye'; }
  77. const deprecatedFn = deprecate.renameFunction(oldFn, newFn);
  78. deprecatedFn();
  79. expect(msg).to.be.a('string');
  80. expect(msg).to.include('oldFn');
  81. expect(msg).to.include('newFn');
  82. });
  83. it('warns only once per item', () => {
  84. const messages: string[] = [];
  85. deprecate.setHandler(message => messages.push(message));
  86. const key = 'foo';
  87. const val = 'bar';
  88. const o = { [key]: val };
  89. deprecate.removeProperty(o, key);
  90. for (let i = 0; i < 3; ++i) {
  91. expect(o[key]).to.equal(val);
  92. expect(messages).to.have.length(1);
  93. }
  94. });
  95. it('warns if deprecated property is already set', () => {
  96. let msg;
  97. deprecate.setHandler(m => { msg = m; });
  98. const oldProp = 'dingyOldName';
  99. const newProp = 'shinyNewName';
  100. const o: Record<string, number> = { [oldProp]: 0 };
  101. deprecate.renameProperty(o, oldProp, newProp);
  102. expect(msg).to.be.a('string');
  103. expect(msg).to.include(oldProp);
  104. expect(msg).to.include(newProp);
  105. });
  106. it('throws an exception if no deprecation handler is specified', () => {
  107. expect(() => {
  108. deprecate.log('this is deprecated');
  109. }).to.throw(/this is deprecated/);
  110. });
  111. it('warns when a function is deprecated in favor of a property', () => {
  112. const warnings: string[] = [];
  113. deprecate.setHandler(warning => warnings.push(warning));
  114. const newProp = 'newProp';
  115. const mod: any = {
  116. _oldGetterFn () { return 'getter'; },
  117. _oldSetterFn () { return 'setter'; }
  118. };
  119. deprecate.fnToProperty(mod, 'newProp', '_oldGetterFn', '_oldSetterFn');
  120. mod['oldGetterFn']();
  121. mod['oldSetterFn']();
  122. expect(warnings).to.have.lengthOf(2);
  123. expect(warnings[0]).to.include('oldGetterFn');
  124. expect(warnings[0]).to.include(newProp);
  125. expect(warnings[1]).to.include('oldSetterFn');
  126. expect(warnings[1]).to.include(newProp);
  127. });
  128. describe('moveAPI', () => {
  129. beforeEach(() => {
  130. deprecate.setHandler(null);
  131. });
  132. it('should call the original method', () => {
  133. const warnings = [];
  134. deprecate.setHandler(warning => warnings.push(warning));
  135. let called = false;
  136. const fn = () => {
  137. called = true;
  138. };
  139. const deprecated = deprecate.moveAPI(fn, 'old', 'new');
  140. deprecated();
  141. expect(called).to.equal(true);
  142. });
  143. it('should log the deprecation warning once', () => {
  144. const warnings: string[] = [];
  145. deprecate.setHandler(warning => warnings.push(warning));
  146. const deprecated = deprecate.moveAPI(() => null, 'old', 'new');
  147. deprecated();
  148. expect(warnings).to.have.lengthOf(1);
  149. deprecated();
  150. expect(warnings).to.have.lengthOf(1);
  151. expect(warnings[0]).to.equal('\'old\' is deprecated and will be removed. Please use \'new\' instead.');
  152. });
  153. });
  154. });