electron/spec/deprecate-spec.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

203 строки
5.2 KiB
TypeScript
Исходник Обычный вид История

2019-09-04 21:13:37 +03:00
import { expect } from 'chai';
import * as deprecate from '../lib/common/deprecate';
describe('deprecate', () => {
let throwing: boolean;
2017-10-27 03:35:33 +03:00
beforeEach(() => {
throwing = process.throwDeprecation;
deprecate.setHandler(null);
2016-03-25 23:03:49 +03:00
process.throwDeprecation = true;
});
afterEach(() => {
process.throwDeprecation = throwing;
});
2017-11-14 22:56:16 +03:00
it('allows a deprecation handler function to be specified', () => {
2019-09-04 21:13:37 +03:00
const messages: string[] = [];
2017-11-14 04:21:57 +03:00
deprecate.setHandler(message => {
2017-11-14 22:56:16 +03:00
messages.push(message);
});
deprecate.log('this is deprecated');
expect(messages).to.deep.equal(['this is deprecated']);
2017-11-14 22:56:16 +03:00
});
it('returns a deprecation handler after one is set', () => {
const messages = [];
deprecate.setHandler(message => {
2017-11-14 22:56:16 +03:00
messages.push(message);
});
deprecate.log('this is deprecated');
expect(deprecate.getHandler()).to.be.a('function');
2017-11-14 22:56:16 +03:00
});
it('renames a property', () => {
let msg;
deprecate.setHandler(m => { msg = m; });
const oldProp = 'dingyOldName';
const newProp = 'shinyNewName';
let value = 0;
2019-09-04 21:13:37 +03:00
const o: Record<string, number> = { [newProp]: value };
expect(o).to.not.have.property(oldProp);
expect(o).to.have.property(newProp).that.is.a('number');
deprecate.renameProperty(o, oldProp, newProp);
o[oldProp] = ++value;
expect(msg).to.be.a('string');
expect(msg).to.include(oldProp);
expect(msg).to.include(newProp);
2019-09-04 21:13:37 +03: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', () => {
2019-09-04 21:13:37 +03:00
const o: any = {};
expect(() => {
deprecate.removeProperty(o, 'iDoNotExist');
}).to.throw(/iDoNotExist/);
});
it('deprecates a property of an object', () => {
let msg;
deprecate.setHandler(m => { msg = m; });
const prop = 'itMustGo';
const o = { [prop]: 0 };
deprecate.removeProperty(o, prop);
const temp = o[prop];
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', () => {
let msg;
deprecate.setHandler(m => { msg = m; });
const prop = 'itMustGo';
let i = 1;
const o = {
get itMustGo () {
return i;
},
set itMustGo (thing) {
i = thing + 1;
}
};
deprecate.removeProperty(o, prop);
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', () => {
let msg;
deprecate.setHandler(m => { msg = m; });
function oldFn () { return 'hello'; }
const deprecatedFn = deprecate.removeFunction(oldFn, 'oldFn');
deprecatedFn();
expect(msg).to.be.a('string');
expect(msg).to.include('oldFn');
});
it('warns exactly once when a function is deprecated with a replacement', () => {
let msg;
deprecate.setHandler(m => { msg = m; });
function oldFn () { return 'hello'; }
const deprecatedFn = deprecate.renameFunction(oldFn, 'newFn');
deprecatedFn();
expect(msg).to.be.a('string');
expect(msg).to.include('oldFn');
expect(msg).to.include('newFn');
});
it('warns only once per item', () => {
2019-09-04 21:13:37 +03:00
const messages: string[] = [];
deprecate.setHandler(message => messages.push(message));
const key = 'foo';
const val = 'bar';
const o = { [key]: val };
deprecate.removeProperty(o, key);
for (let i = 0; i < 3; ++i) {
expect(o[key]).to.equal(val);
expect(messages).to.have.length(1);
}
});
it('warns if deprecated property is already set', () => {
let msg;
deprecate.setHandler(m => { msg = m; });
const oldProp = 'dingyOldName';
const newProp = 'shinyNewName';
2019-09-04 21:13:37 +03:00
const o: Record<string, number> = { [oldProp]: 0 };
deprecate.renameProperty(o, oldProp, newProp);
expect(msg).to.be.a('string');
expect(msg).to.include(oldProp);
expect(msg).to.include(newProp);
});
2017-11-14 22:56:16 +03:00
it('throws an exception if no deprecation handler is specified', () => {
expect(() => {
2017-11-14 22:56:16 +03:00
deprecate.log('this is deprecated');
}).to.throw(/this is deprecated/);
2017-11-14 22:56:16 +03:00
});
describe('moveAPI', () => {
beforeEach(() => {
deprecate.setHandler(null);
});
it('should call the original method', () => {
const warnings = [];
deprecate.setHandler(warning => warnings.push(warning));
let called = false;
const fn = () => {
called = true;
};
const deprecated = deprecate.moveAPI(fn, 'old', 'new');
deprecated();
expect(called).to.equal(true);
});
it('should log the deprecation warning once', () => {
2019-09-04 21:13:37 +03:00
const warnings: string[] = [];
deprecate.setHandler(warning => warnings.push(warning));
2020-03-20 23:28:31 +03:00
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.');
});
});
2016-03-25 23:03:49 +03:00
});