2016-05-17 02:43:48 +08:00
|
|
|
import { remote } from 'electron';
|
|
|
|
|
|
|
|
describe("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});
|
2016-05-17 02:43:48 +08:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
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', () => {
|
2016-05-17 02:43:48 +08:00
|
|
|
NylasEnv.setSize(100, 400);
|
2016-05-17 04:26:33 +08:00
|
|
|
expect(NylasEnv.getSize()).toEqual({width: 100, height: 400});
|
2016-05-17 02:43:48 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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 02:43:48 +08:00
|
|
|
});
|
|
|
|
|
2016-05-17 04:26:33 +08:00
|
|
|
it("sets the current size if minWidth > current width", () => {
|
2016-05-17 02:43:48 +08:00
|
|
|
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 02:43:48 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-17 04:26:33 +08:00
|
|
|
describe('::getDefaultWindowDimensions', () => {
|
2016-05-17 02:43:48 +08:00
|
|
|
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});
|
2016-05-17 02:43:48 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
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});
|
2016-05-17 02:43:48 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
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});
|
2016-05-17 02:43:48 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
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 02:43:48 +08:00
|
|
|
});
|
|
|
|
|
2016-05-17 04:26:33 +08:00
|
|
|
it("always rounds X and Y", () => {
|
2016-05-17 02:43:48 +08:00
|
|
|
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});
|
2016-05-17 02:43:48 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
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 02:43:48 +08:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2016-05-17 04:26:33 +08:00
|
|
|
describe("when an update becomes available", () => {
|
2016-05-17 02:43:48 +08:00
|
|
|
let subscription = null;
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
if (subscription) { subscription.dispose(); }
|
|
|
|
});
|
|
|
|
|
2016-05-17 04:26:33 +08:00
|
|
|
it("invokes onUpdateAvailable listeners", () => {
|
2016-06-16 07:27:37 +08:00
|
|
|
if (process.platform === "linux") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-17 02:43:48 +08:00
|
|
|
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(() => {
|
2016-05-17 02:43:48 +08:00
|
|
|
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 02:43:48 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-17 04:26:33 +08:00
|
|
|
describe("error handling", () => {
|
2016-05-17 02:43:48 +08:00
|
|
|
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 02:43:48 +08:00
|
|
|
});
|
|
|
|
|
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 02:43:48 +08:00
|
|
|
|
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 02:43:48 +08:00
|
|
|
});
|
|
|
|
|
2016-05-17 04:26:33 +08:00
|
|
|
describe("reportError", () => {
|
2016-05-17 02:43:48 +08:00
|
|
|
beforeEach(() => {
|
2016-05-17 04:26:33 +08:00
|
|
|
this.testErr = new Error("Test");
|
|
|
|
spyOn(console, "error")
|
2016-05-17 02:43:48 +08:00
|
|
|
});
|
|
|
|
|
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")
|
2016-05-17 02:43:48 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|