Mailspring/app/spec/spec-runner/time-override.ts
Ben Gotow 149b389508
Replace Babel with TypeScript compiler, switch entire app to TypeScript 🎉 (#1404)
* Switch to using Typescript instead of Babel

* Switch all es6 / jsx file extensions to ts / tsx

* Convert Utils to a TS module from module.exports style module

* Move everything from module.exports to typescript exports

* Define .d.ts files for mailspring-exports and component kit… Yes it seems this is the best option :(

* Load up on those @types

* Synthesize TS types from PropTypes for standard components

* Add types to Model classes and move constructor constants to instance vars

* 9800 => 7700 TS errors

* 7700 => 5600 TS errors

* 5600 => 5330 TS errors

* 5330 => 4866 TS errors

* 4866 => 4426 TS errors

* 4426 => 2411 TS errors

* 2411 > 1598 TS errors

* 1598 > 769 TS errors

* 769 > 129 TS errors

* 129 > 22 TS errors

* Fix runtime errors

* More runtime error fixes

* Remove support for custom .es6 file extension

* Remove a few odd remaining references to Nylas

* Don’t ship Typescript support in the compiled app for now

* Fix issues in compiled app - module resolution in TS is case sensitive?

* README updates

* Fix a few more TS errors

* Make “No Signature” option clickable + selectable

* Remove flicker when saving file and reloading keymaps

* Fix mail rule item height in preferences

* Fix missing spacing in thread sharing popover

* Fix scrollbar ticks being nested incorrectly

* Add Japanese as a manually reviewed language

* Prevent the thread list from “sticking”

* Re-use Sheet when switching root tabs, prevent sidebar from resetting

* Ensure specs run

* Update package configuration to avoid shpping types

* Turn eslint back on - we will opt-in to the TS rules one by one
2019-03-04 11:03:12 -08:00

130 lines
3.8 KiB
TypeScript

/*
* decaffeinate suggestions:
* DS201: Simplify complex destructure assignments
* DS205: Consider reworking code to avoid use of IIFEs
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
import _ from 'underscore';
// Public: To make specs easier to test, we make all asynchronous behavior
// actually synchronous. We do this by overriding all global timeout and
// Promise functions.
//
// You must now manually call `advanceClock()` in order to move the "clock"
// forward.
class TimeOverride {
static initClass() {
this.advanceClock = (delta = 1) => {
this.now += delta;
const callbacks = [];
if (this.timeouts == null) {
this.timeouts = [];
}
this.timeouts = this.timeouts.filter((...args) => {
let id, strikeTime;
let callback;
[id, strikeTime, callback] = Array.from(args[0]);
if (strikeTime <= this.now) {
callbacks.push(callback);
return false;
} else {
return true;
}
});
for (let callback of callbacks) {
callback();
}
};
this.resetTime = () => {
this.now = 0;
this.timeoutCount = 0;
this.intervalCount = 0;
this.timeouts = [];
this.intervalTimeouts = {};
this.originalPromiseScheduler = null;
};
this.enableSpies = () => {
window.advanceClock = this.advanceClock;
window.originalSetTimeout = window.setTimeout;
window.originalSetInterval = window.setInterval;
spyOn(window, 'setTimeout').andCallFake(this._fakeSetTimeout);
spyOn(window, 'clearTimeout').andCallFake(this._fakeClearTimeout);
spyOn(window, 'setInterval').andCallFake(this._fakeSetInterval);
spyOn(window, 'clearInterval').andCallFake(this._fakeClearInterval);
spyOn(_._, 'now').andCallFake(() => this.now);
};
// spyOn(Date, "now").andCallFake => @now
// spyOn(Date.prototype, "getTime").andCallFake => @now
this.disableSpies = () => {
window.advanceClock = null;
jasmine.unspy(window, 'setTimeout');
jasmine.unspy(window, 'clearTimeout');
jasmine.unspy(window, 'setInterval');
jasmine.unspy(window, 'clearInterval');
jasmine.unspy(_._, 'now');
};
this._fakeSetTimeout = (callback, ms) => {
const id = ++this.timeoutCount;
this.timeouts.push([id, this.now + ms, callback]);
return id;
};
this._fakeClearTimeout = idToClear => {
if (this.timeouts == null) {
this.timeouts = [];
}
this.timeouts = this.timeouts.filter(function(...args) {
const [id] = args[0];
return id !== idToClear;
});
};
this._fakeSetInterval = (callback, ms) => {
const id = ++this.intervalCount;
var action = () => {
callback();
this.intervalTimeouts[id] = this._fakeSetTimeout(action, ms);
};
this.intervalTimeouts[id] = this._fakeSetTimeout(action, ms);
return id;
};
this._fakeClearInterval = idToClear => {
this._fakeClearTimeout(this.intervalTimeouts[idToClear]);
};
}
static resetSpyData() {
if (typeof window.setTimeout.reset === 'function') {
window.setTimeout.reset();
}
if (typeof window.clearTimeout.reset === 'function') {
window.clearTimeout.reset();
}
if (typeof window.setInterval.reset === 'function') {
window.setInterval.reset();
}
if (typeof window.clearInterval.reset === 'function') {
window.clearInterval.reset();
}
if (typeof Date.now.reset === 'function') {
Date.now.reset();
}
if (typeof Date.prototype.getTime.reset === 'function') {
Date.prototype.getTime.reset();
}
}
}
TimeOverride.initClass();
export default TimeOverride;