electron/spec-main/api-deprecate-spec.ts

227 lines
5.9 KiB
TypeScript
Raw Normal View History

2020-03-20 20:28:31 +00:00
import { expect } from 'chai';
import { deprecate } from 'electron/main';
describe('deprecate', () => {
2020-03-20 20:28:31 +00:00
let throwing: boolean;
2017-10-27 00:35:33 +00:00
beforeEach(() => {
2020-03-20 20:28:31 +00:00
throwing = process.throwDeprecation;
deprecate.setHandler(null);
process.throwDeprecation = true;
});
afterEach(() => {
2020-03-20 20:28:31 +00:00
process.throwDeprecation = throwing;
});
2017-11-14 19:56:16 +00:00
it('allows a deprecation handler function to be specified', () => {
2020-03-20 20:28:31 +00:00
const messages: string[] = [];
2017-11-14 01:21:57 +00:00
deprecate.setHandler(message => {
2020-03-20 20:28:31 +00:00
messages.push(message);
});
2017-11-14 19:56:16 +00:00
2020-03-20 20:28:31 +00:00
deprecate.log('this is deprecated');
expect(messages).to.deep.equal(['this is deprecated']);
});
2017-11-14 19:56:16 +00:00
it('returns a deprecation handler after one is set', () => {
2020-03-20 20:28:31 +00:00
const messages = [];
2017-11-14 19:56:16 +00:00
deprecate.setHandler(message => {
2020-03-20 20:28:31 +00:00
messages.push(message);
});
2017-11-14 19:56:16 +00:00
2020-03-20 20:28:31 +00:00
deprecate.log('this is deprecated');
expect(deprecate.getHandler()).to.be.a('function');
});
2017-11-14 19:56:16 +00:00
it('renames a property', () => {
2020-03-20 20:28:31 +00:00
let msg;
deprecate.setHandler(m => { msg = m; });
2020-03-20 20:28:31 +00:00
const oldProp = 'dingyOldName';
const newProp = 'shinyNewName';
2020-03-20 20:28:31 +00:00
let value = 0;
const o: Record<string, number> = { [newProp]: value };
expect(o).to.not.have.property(oldProp);
expect(o).to.have.property(newProp).that.is.a('number');
2020-03-20 20:28:31 +00:00
deprecate.renameProperty(o, oldProp, newProp);
o[oldProp] = ++value;
2020-03-20 20:28:31 +00:00
expect(msg).to.be.a('string');
expect(msg).to.include(oldProp);
expect(msg).to.include(newProp);
2020-03-20 20:28:31 +00:00
expect(o).to.have.property(newProp).that.is.equal(value);
expect(o).to.have.property(oldProp).that.is.equal(value);
});
it('doesn\'t deprecate a property not on an object', () => {
2020-03-20 20:28:31 +00:00
const o: any = {};
expect(() => {
2020-03-20 20:28:31 +00:00
deprecate.removeProperty(o, 'iDoNotExist');
}).to.throw(/iDoNotExist/);
});
it('deprecates a property of an object', () => {
2020-03-20 20:28:31 +00:00
let msg;
deprecate.setHandler(m => { msg = m; });
2020-03-20 20:28:31 +00:00
const prop = 'itMustGo';
const o = { [prop]: 0 };
2020-03-20 20:28:31 +00:00
deprecate.removeProperty(o, prop);
2020-03-20 20:28:31 +00:00
const temp = o[prop];
2020-03-20 20:28:31 +00:00
expect(temp).to.equal(0);
expect(msg).to.be.a('string');
expect(msg).to.include(prop);
});
it('deprecates a property of an but retains the existing accessors and setters', () => {
2020-03-20 20:28:31 +00:00
let msg;
deprecate.setHandler(m => { msg = m; });
2020-03-20 20:28:31 +00:00
const prop = 'itMustGo';
let i = 1;
const o = {
get itMustGo () {
2020-03-20 20:28:31 +00:00
return i;
},
set itMustGo (thing) {
2020-03-20 20:28:31 +00:00
i = thing + 1;
}
2020-03-20 20:28:31 +00:00
};
2020-03-20 20:28:31 +00:00
deprecate.removeProperty(o, prop);
2020-03-20 20:28:31 +00:00
expect(o[prop]).to.equal(1);
expect(msg).to.be.a('string');
expect(msg).to.include(prop);
o[prop] = 2;
expect(o[prop]).to.equal(3);
});
it('warns exactly once when a function is deprecated with no replacement', () => {
2020-03-20 20:28:31 +00:00
let msg;
deprecate.setHandler(m => { msg = m; });
2020-03-20 20:28:31 +00:00
function oldFn () { return 'hello'; }
const deprecatedFn = deprecate.removeFunction(oldFn, 'oldFn');
deprecatedFn();
2020-03-20 20:28:31 +00:00
expect(msg).to.be.a('string');
expect(msg).to.include('oldFn');
});
it('warns exactly once when a function is deprecated with a replacement', () => {
2020-03-20 20:28:31 +00:00
let msg;
deprecate.setHandler(m => { msg = m; });
2020-03-20 20:28:31 +00:00
function oldFn () { return 'hello'; }
function newFn () { return 'goodbye'; }
const deprecatedFn = deprecate.renameFunction(oldFn, newFn);
deprecatedFn();
2020-03-20 20:28:31 +00:00
expect(msg).to.be.a('string');
expect(msg).to.include('oldFn');
expect(msg).to.include('newFn');
});
it('warns only once per item', () => {
2020-03-20 20:28:31 +00:00
const messages: string[] = [];
deprecate.setHandler(message => messages.push(message));
2020-03-20 20:28:31 +00:00
const key = 'foo';
const val = 'bar';
const o = { [key]: val };
deprecate.removeProperty(o, key);
for (let i = 0; i < 3; ++i) {
2020-03-20 20:28:31 +00:00
expect(o[key]).to.equal(val);
expect(messages).to.have.length(1);
}
2020-03-20 20:28:31 +00:00
});
it('warns if deprecated property is already set', () => {
2020-03-20 20:28:31 +00:00
let msg;
deprecate.setHandler(m => { msg = m; });
2020-03-20 20:28:31 +00:00
const oldProp = 'dingyOldName';
const newProp = 'shinyNewName';
2020-03-20 20:28:31 +00:00
const o: Record<string, number> = { [oldProp]: 0 };
deprecate.renameProperty(o, oldProp, newProp);
2020-03-20 20:28:31 +00:00
expect(msg).to.be.a('string');
expect(msg).to.include(oldProp);
expect(msg).to.include(newProp);
});
2017-11-14 19:56:16 +00:00
it('throws an exception if no deprecation handler is specified', () => {
expect(() => {
2020-03-20 20:28:31 +00:00
deprecate.log('this is deprecated');
}).to.throw(/this is deprecated/);
});
it('warns when a function is deprecated in favor of a property', () => {
2020-03-20 20:28:31 +00:00
const warnings: string[] = [];
deprecate.setHandler(warning => warnings.push(warning));
2020-03-20 20:28:31 +00:00
const newProp = 'newProp';
2019-09-04 18:13:37 +00:00
const mod: any = {
2020-03-20 20:28:31 +00:00
_oldGetterFn () { return 'getter'; },
_oldSetterFn () { return 'setter'; }
};
2020-03-20 20:28:31 +00:00
deprecate.fnToProperty(mod, 'newProp', '_oldGetterFn', '_oldSetterFn');
2020-03-20 20:28:31 +00:00
mod.oldGetterFn();
mod.oldSetterFn();
2020-03-20 20:28:31 +00:00
expect(warnings).to.have.lengthOf(2);
2020-03-20 20:28:31 +00:00
expect(warnings[0]).to.include('oldGetterFn');
expect(warnings[0]).to.include(newProp);
2020-03-20 20:28:31 +00:00
expect(warnings[1]).to.include('oldSetterFn');
expect(warnings[1]).to.include(newProp);
});
describe('moveAPI', () => {
beforeEach(() => {
2020-03-20 20:28:31 +00:00
deprecate.setHandler(null);
});
it('should call the original method', () => {
2020-03-20 20:28:31 +00:00
const warnings = [];
deprecate.setHandler(warning => warnings.push(warning));
2020-03-20 20:28:31 +00:00
let called = false;
const fn = () => {
2020-03-20 20:28:31 +00:00
called = true;
};
const deprecated = deprecate.moveAPI(fn, 'old', 'new');
deprecated();
expect(called).to.equal(true);
});
it('should log the deprecation warning once', () => {
2020-03-20 20:28:31 +00:00
const warnings: string[] = [];
deprecate.setHandler(warning => warnings.push(warning));
const deprecated = deprecate.moveAPI(() => null, 'old', 'new');
deprecated();
expect(warnings).to.have.lengthOf(1);
deprecated();
expect(warnings).to.have.lengthOf(1);
expect(warnings[0]).to.equal('\'old\' is deprecated and will be removed. Please use \'new\' instead.');
});
});
});