Mailspring/spec/nylas-env-spec.es6

219 lines
7.8 KiB
Plaintext
Raw Normal View History

import { remote } from 'electron';
fix(spec): add support for async specs and disable misbehaving ones More spec fixes replace process.nextTick with setTimeout(fn, 0) for specs Also added an unspy in the afterEach Temporarily disable specs fix(spec): start fixing specs Summary: This is the WIP fix to our spec runner. Several tests have been completely commented out that will require substantially more work to fix. These have been added to our sprint backlog. Other tests have been fixed to update to new APIs or to deal with genuine bugs that were introduced without our knowing! The most common non-trivial change relates to observing the `NylasAPI` and `NylasAPIRequest`. We used to observe the arguments to `makeRequest`. Unfortunately `NylasAPIRequest.run` is argumentless. Instead you can do: `NylasAPIRequest.prototype.run.mostRecentCall.object.options` to get the `options` passed into the object. the `.object` property grabs the context of the spy when it was last called. Fixing these tests uncovered several concerning issues with our test runner. I spent a while tracking down why our participant-text-field-spec was failling every so often. I chose that spec because it was the first spec to likely fail, thereby requiring looking at the least number of preceding files. I tried binary searching, turning on and off, several files beforehand only to realize that the failure rate was not determined by a particular preceding test, but rather the existing and quantity of preceding tests, AND the number of console.log statements I had. There is some processor-dependent race condition going on that needs further investigation. I also discovered an issue with the file-download-spec. We were getting errors about it accessing a file, which was very suspicious given the code stubs out all fs access. This was caused due to a spec that called an async function outside ot a `waitsForPromise` block or a `waitsFor` block. The test completed, the spies were cleaned up, but the downstream async chain was still running. By the time the async chain finished the runner was already working on the next spec and the spies had been restored (causing the real fs access to run). Juan had an idea to kill the specs once one fails to prevent cascading failures. I'll implement this in the next diff update Test Plan: npm test Reviewers: juan, halla, jackie Differential Revision: https://phab.nylas.com/D3501 Disable other specs Disable more broken specs All specs turned off till passing state Use async-safe versions of spec functions Add async test spec Remove unused package code Remove canary spec
2016-12-13 04:12:20 +08:00
xdescribe("the `NylasEnv` global", function nylasEnvSpec() {
describe('window sizing methods', () => {
describe('::getPosition and ::setPosition', () =>
it('sets the position of the window, and can retrieve the position just set', () => {
NylasEnv.setPosition(22, 45);
2016-05-17 04:26:33 +08:00
expect(NylasEnv.getPosition()).toEqual({x: 22, y: 45});
})
);
describe('::getSize and ::setSize', () => {
beforeEach(() => {
this.originalSize = NylasEnv.getSize()
});
afterEach(() => NylasEnv.setSize(this.originalSize.width, this.originalSize.height));
2016-05-17 04:26:33 +08:00
it('sets the size of the window, and can retrieve the size just set', () => {
NylasEnv.setSize(100, 400);
2016-05-17 04:26:33 +08:00
expect(NylasEnv.getSize()).toEqual({width: 100, height: 400});
});
});
describe('::setMinimumWidth', () => {
const win = NylasEnv.getCurrentWindow();
it("sets the minimum width", () => {
const inputMinWidth = 500;
win.setMinimumSize(1000, 1000);
NylasEnv.setMinimumWidth(inputMinWidth);
const [actualMinWidth] = win.getMinimumSize();
2016-05-17 04:26:33 +08:00
expect(actualMinWidth).toBe(inputMinWidth);
});
2016-05-17 04:26:33 +08:00
it("sets the current size if minWidth > current width", () => {
const inputMinWidth = 1000;
win.setSize(500, 500);
NylasEnv.setMinimumWidth(inputMinWidth);
const [actualWidth] = win.getMinimumSize();
2016-05-17 04:26:33 +08:00
expect(actualWidth).toBe(inputMinWidth);
});
});
2016-05-17 04:26:33 +08:00
describe('::getDefaultWindowDimensions', () => {
it("returns primary display's work area size if it's small enough", () => {
spyOn(remote.screen, 'getPrimaryDisplay').andReturn({workAreaSize: { width: 1440, height: 900}});
const out = NylasEnv.getDefaultWindowDimensions();
2016-05-17 04:26:33 +08:00
expect(out).toEqual({x: 0, y: 0, width: 1440, height: 900});
});
it("caps width at 1440 and centers it, if wider", () => {
spyOn(remote.screen, 'getPrimaryDisplay').andReturn({workAreaSize: { width: 1840, height: 900}});
const out = NylasEnv.getDefaultWindowDimensions();
2016-05-17 04:26:33 +08:00
expect(out).toEqual({x: 200, y: 0, width: 1440, height: 900});
});
it("caps height at 900 and centers it, if taller", () => {
spyOn(remote.screen, 'getPrimaryDisplay').andReturn({workAreaSize: { width: 1440, height: 1100}});
const out = NylasEnv.getDefaultWindowDimensions();
2016-05-17 04:26:33 +08:00
expect(out).toEqual({x: 0, y: 100, width: 1440, height: 900});
});
it("returns only the max viewport size if it's smaller than the defaults", () => {
spyOn(remote.screen, 'getPrimaryDisplay').andReturn({workAreaSize: { width: 1000, height: 800}});
const out = NylasEnv.getDefaultWindowDimensions();
2016-05-17 04:26:33 +08:00
expect(out).toEqual({x: 0, y: 0, width: 1000, height: 800});
});
2016-05-17 04:26:33 +08:00
it("always rounds X and Y", () => {
spyOn(remote.screen, 'getPrimaryDisplay').andReturn({workAreaSize: { width: 1845, height: 955}});
const out = NylasEnv.getDefaultWindowDimensions();
2016-05-17 04:26:33 +08:00
expect(out).toEqual({x: 202, y: 27, width: 1440, height: 900});
});
});
});
describe(".isReleasedVersion()", () =>
it("returns false if the version is a SHA and true otherwise", () => {
let version = '0.1.0';
spyOn(NylasEnv, 'getVersion').andCallFake(() => version);
expect(NylasEnv.isReleasedVersion()).toBe(true);
version = '36b5518';
2016-05-17 04:26:33 +08:00
expect(NylasEnv.isReleasedVersion()).toBe(false);
})
);
2016-05-17 04:26:33 +08:00
describe("when an update becomes available", () => {
let subscription = null;
afterEach(() => {
if (subscription) { subscription.dispose(); }
});
2016-05-17 04:26:33 +08:00
it("invokes onUpdateAvailable listeners", () => {
if (process.platform === "linux") {
return;
}
const updateAvailableHandler = jasmine.createSpy("update-available-handler");
subscription = NylasEnv.onUpdateAvailable(updateAvailableHandler);
remote.autoUpdater.emit('update-downloaded', null, "notes", "version");
waitsFor(() => updateAvailableHandler.callCount > 0);
2016-05-17 04:26:33 +08:00
runs(() => {
const {releaseVersion, releaseNotes} = updateAvailableHandler.mostRecentCall.args[0];
expect(releaseVersion).toBe('version');
2016-05-17 04:26:33 +08:00
expect(releaseNotes).toBe('notes');
});
});
});
2016-05-17 04:26:33 +08:00
describe("error handling", () => {
beforeEach(() => {
2016-05-17 04:26:33 +08:00
spyOn(NylasEnv, "inSpecMode").andReturn(false)
spyOn(NylasEnv, "inDevMode").andReturn(false);
spyOn(NylasEnv, "openDevTools")
spyOn(NylasEnv, "executeJavaScriptInDevTools")
spyOn(NylasEnv.errorLogger, "reportError");
});
2016-05-17 04:26:33 +08:00
it("Catches errors that make it to window.onerror", () => {
spyOn(NylasEnv, "reportError");
const e = new Error("Test Error")
window.onerror.call(window, e.toString(), 'abc', 2, 3, e);
expect(NylasEnv.reportError).toHaveBeenCalled();
expect(NylasEnv.reportError.calls[0].args[0]).toBe(e);
const extra = NylasEnv.reportError.calls[0].args[1]
expect(extra.url).toBe("abc")
expect(extra.line).toBe(2)
expect(extra.column).toBe(3)
});
2016-05-17 04:26:33 +08:00
it("Catches unhandled rejections", () => {
spyOn(NylasEnv, "reportError");
const err = new Error("TEST");
runs(() => {
const p = new Promise((resolve, reject) => {
setTimeout(() => {
reject(err);
}, 10)
})
p.then(() => {
throw new Error("Shouldn't resolve")
})
advanceClock(10);
})
waitsFor(() => NylasEnv.reportError.callCount > 0);
runs(() => {
expect(NylasEnv.reportError.callCount).toBe(1);
expect(NylasEnv.reportError.calls[0].args[0]).toBe(err);
expect(NylasEnv.reportError.calls[0].args[1].promise).toBeDefined();
})
});
2016-05-17 04:26:33 +08:00
describe("reportError", () => {
beforeEach(() => {
2016-05-17 04:26:33 +08:00
this.testErr = new Error("Test");
spyOn(console, "error")
});
2016-05-17 04:26:33 +08:00
it("emits will-throw-error", () => {
spyOn(NylasEnv.emitter, "emit")
NylasEnv.reportError(this.testErr);
expect(NylasEnv.emitter.emit).toHaveBeenCalled();
expect(NylasEnv.emitter.emit.callCount).toBe(2);
expect(NylasEnv.emitter.emit.calls[0].args[0]).toBe("will-throw-error")
expect(NylasEnv.emitter.emit.calls[1].args[0]).toBe("did-throw-error")
});
it("returns if the event has its default prevented", () => {
spyOn(NylasEnv.emitter, "emit").andCallFake((name, event) => {
event.preventDefault()
})
NylasEnv.reportError(this.testErr);
expect(NylasEnv.emitter.emit).toHaveBeenCalled();
expect(NylasEnv.emitter.emit.callCount).toBe(1);
expect(NylasEnv.emitter.emit.calls[0].args[0]).toBe("will-throw-error")
});
it("opens dev tools in dev mode", () => {
jasmine.unspy(NylasEnv, "inDevMode")
spyOn(NylasEnv, "inDevMode").andReturn(true);
NylasEnv.reportError(this.testErr);
expect(NylasEnv.openDevTools).toHaveBeenCalled();
expect(NylasEnv.executeJavaScriptInDevTools).toHaveBeenCalled();
});
it("sends the error report to the error logger", () => {
NylasEnv.reportError(this.testErr);
expect(NylasEnv.errorLogger.reportError).toHaveBeenCalled();
expect(NylasEnv.errorLogger.reportError.callCount).toBe(1);
expect(NylasEnv.errorLogger.reportError.calls[0].args[0]).toBe(this.testErr);
});
it("emits did-throw-error", () => {
spyOn(NylasEnv.emitter, "emit")
NylasEnv.reportError(this.testErr);
expect(NylasEnv.openDevTools).not.toHaveBeenCalled();
expect(NylasEnv.executeJavaScriptInDevTools).not.toHaveBeenCalled();
expect(NylasEnv.emitter.emit.callCount).toBe(2);
expect(NylasEnv.emitter.emit.calls[0].args[0]).toBe("will-throw-error")
expect(NylasEnv.emitter.emit.calls[1].args[0]).toBe("did-throw-error")
});
});
});
});