2016-03-23 06:47:51 +08:00
|
|
|
import {
|
|
|
|
Thread,
|
|
|
|
Actions,
|
|
|
|
Contact,
|
|
|
|
Message,
|
|
|
|
Account,
|
|
|
|
DraftStore,
|
2016-05-19 07:19:42 +08:00
|
|
|
DraftHelpers,
|
2016-03-23 06:47:51 +08:00
|
|
|
DatabaseStore,
|
|
|
|
SoundRegistry,
|
|
|
|
DestroyDraftTask,
|
|
|
|
ComposerExtension,
|
|
|
|
ExtensionRegistry,
|
|
|
|
FocusedContentStore,
|
|
|
|
DatabaseTransaction,
|
|
|
|
} from 'nylas-exports';
|
|
|
|
|
2016-05-07 05:10:28 +08:00
|
|
|
import {remote} from 'electron';
|
2016-03-23 06:47:51 +08:00
|
|
|
import DraftFactory from '../../src/flux/stores/draft-factory';
|
|
|
|
|
|
|
|
class TestExtension extends ComposerExtension {
|
|
|
|
static prepareNewDraft({draft}) {
|
2016-05-07 05:10:28 +08:00
|
|
|
draft.body = `Edited by TestExtension! ${draft.body}`;
|
2016-03-23 06:47:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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('DraftStore', function draftStore() {
|
2016-03-23 06:47:51 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
this.fakeThread = new Thread({id: 'fake-thread', clientId: 'fake-thread'});
|
|
|
|
this.fakeMessage = new Message({id: 'fake-message', clientId: 'fake-message'});
|
|
|
|
|
|
|
|
spyOn(NylasEnv, 'newWindow').andCallFake(() => {});
|
|
|
|
spyOn(DatabaseTransaction.prototype, "persistModel").andReturn(Promise.resolve());
|
|
|
|
spyOn(DatabaseStore, 'run').andCallFake((query) => {
|
|
|
|
if (query._klass === Thread) { return Promise.resolve(this.fakeThread); }
|
|
|
|
if (query._klass === Message) { return Promise.resolve(this.fakeMessage); }
|
|
|
|
if (query._klass === Contact) { return Promise.resolve(null); }
|
|
|
|
return Promise.reject(new Error(`Not Stubbed for class ${query._klass.name}`));
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const draftClientId of Object.keys(DraftStore._draftSessions)) {
|
|
|
|
const sess = DraftStore._draftSessions[draftClientId];
|
|
|
|
if (sess.teardown) {
|
|
|
|
DraftStore._doneWithSession(sess);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DraftStore._draftSessions = {};
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("creating and opening drafts", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
const draft = new Message({id: "A", subject: "B", clientId: "A", body: "123"});
|
2016-04-05 09:21:39 +08:00
|
|
|
this.newDraft = draft;
|
2016-03-23 06:47:51 +08:00
|
|
|
spyOn(DraftFactory, "createDraftForReply").andReturn(Promise.resolve(draft));
|
|
|
|
spyOn(DraftFactory, "createOrUpdateDraftForReply").andReturn(Promise.resolve(draft));
|
|
|
|
spyOn(DraftFactory, "createDraftForForward").andReturn(Promise.resolve(draft));
|
|
|
|
spyOn(DraftFactory, "createDraft").andReturn(Promise.resolve(draft));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should always attempt to focus the new draft", () => {
|
|
|
|
spyOn(Actions, 'focusDraft')
|
|
|
|
DraftStore._onComposeReply({
|
|
|
|
threadId: this.fakeThread.id,
|
|
|
|
messageId: this.fakeMessage.id,
|
|
|
|
type: 'reply',
|
|
|
|
behavior: 'prefer-existing',
|
|
|
|
});
|
|
|
|
advanceClock();
|
|
|
|
advanceClock();
|
2016-10-17 06:16:42 +08:00
|
|
|
expect(Actions.focusDraft).toHaveBeenCalled();
|
2016-03-23 06:47:51 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("context", () => {
|
|
|
|
it("can accept IDs for thread and message arguments", () => {
|
|
|
|
DraftStore._onComposeReply({
|
|
|
|
threadId: this.fakeThread.id,
|
|
|
|
messageId: this.fakeMessage.id,
|
|
|
|
type: 'reply',
|
|
|
|
behavior: 'prefer-existing',
|
|
|
|
});
|
|
|
|
advanceClock();
|
|
|
|
expect(DraftFactory.createOrUpdateDraftForReply).toHaveBeenCalledWith({
|
|
|
|
thread: this.fakeThread,
|
|
|
|
message: this.fakeMessage,
|
|
|
|
type: 'reply',
|
|
|
|
behavior: 'prefer-existing',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("can accept models for thread and message arguments", () => {
|
|
|
|
DraftStore._onComposeReply({
|
|
|
|
thread: this.fakeThread,
|
|
|
|
message: this.fakeMessage,
|
|
|
|
type: 'reply',
|
|
|
|
behavior: 'prefer-existing',
|
|
|
|
});
|
|
|
|
advanceClock();
|
|
|
|
expect(DraftFactory.createOrUpdateDraftForReply).toHaveBeenCalledWith({
|
|
|
|
thread: this.fakeThread,
|
|
|
|
message: this.fakeMessage,
|
|
|
|
type: 'reply',
|
|
|
|
behavior: 'prefer-existing',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("can accept only a thread / threadId, and use the last message on the thread", () => {
|
|
|
|
DraftStore._onComposeReply({
|
|
|
|
thread: this.fakeThread,
|
|
|
|
type: 'reply',
|
|
|
|
behavior: 'prefer-existing',
|
|
|
|
});
|
|
|
|
advanceClock();
|
|
|
|
expect(DraftFactory.createOrUpdateDraftForReply).toHaveBeenCalledWith({
|
|
|
|
thread: this.fakeThread,
|
|
|
|
message: this.fakeMessage,
|
|
|
|
type: 'reply',
|
|
|
|
behavior: 'prefer-existing',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("popout behavior", () => {
|
|
|
|
it("can popout a reply", () => {
|
|
|
|
runs(() => {
|
|
|
|
DraftStore._onComposeReply({
|
|
|
|
threadId: this.fakeThread.id,
|
|
|
|
messageId: this.fakeMessage.id,
|
|
|
|
type: 'reply',
|
|
|
|
popout: true}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
waitsFor(() => {
|
|
|
|
return DatabaseTransaction.prototype.persistModel.callCount > 0;
|
|
|
|
});
|
|
|
|
runs(() => {
|
|
|
|
expect(NylasEnv.newWindow).toHaveBeenCalledWith({
|
|
|
|
title: 'Message',
|
2016-04-23 04:30:42 +08:00
|
|
|
hidden: true,
|
|
|
|
windowKey: `composer-A`,
|
|
|
|
windowType: "composer-preload",
|
2016-04-05 09:21:39 +08:00
|
|
|
windowProps: { draftClientId: "A", draftJSON: this.newDraft.toJSON() },
|
2016-03-23 06:47:51 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("can popout a forward", () => {
|
|
|
|
runs(() => {
|
|
|
|
DraftStore._onComposeForward({
|
|
|
|
threadId: this.fakeThread.id,
|
|
|
|
messageId: this.fakeMessage.id,
|
|
|
|
popout: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
waitsFor(() => {
|
|
|
|
return DatabaseTransaction.prototype.persistModel.callCount > 0;
|
|
|
|
});
|
|
|
|
runs(() => {
|
|
|
|
expect(NylasEnv.newWindow).toHaveBeenCalledWith({
|
|
|
|
title: 'Message',
|
2016-04-23 04:30:42 +08:00
|
|
|
hidden: true,
|
|
|
|
windowKey: `composer-A`,
|
|
|
|
windowType: "composer-preload",
|
2016-04-05 09:21:39 +08:00
|
|
|
windowProps: { draftClientId: "A", draftJSON: this.newDraft.toJSON() },
|
2016-03-23 06:47:51 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("onDestroyDraft", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
this.draftSessionTeardown = jasmine.createSpy('draft teardown');
|
|
|
|
this.session =
|
2016-11-16 02:20:39 +08:00
|
|
|
{draft() {
|
|
|
|
return {pristine: false};
|
|
|
|
},
|
2016-03-23 06:47:51 +08:00
|
|
|
changes:
|
2016-11-16 02:20:39 +08:00
|
|
|
{commit() { return Promise.resolve(); },
|
2016-03-23 06:47:51 +08:00
|
|
|
teardown() {},
|
2016-11-16 02:20:39 +08:00
|
|
|
},
|
2016-03-23 06:47:51 +08:00
|
|
|
teardown: this.draftSessionTeardown,
|
2016-11-16 02:20:39 +08:00
|
|
|
};
|
2016-05-07 05:10:28 +08:00
|
|
|
DraftStore._draftSessions = {abc: this.session};
|
2016-03-23 06:47:51 +08:00
|
|
|
spyOn(Actions, 'queueTask');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should teardown the draft session, ensuring no more saves are made", () => {
|
|
|
|
DraftStore._onDestroyDraft('abc');
|
|
|
|
expect(this.draftSessionTeardown).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not throw if the draft session is not in the window", () => {
|
|
|
|
expect(() => DraftStore._onDestroyDraft('other')).not.toThrow();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should queue a destroy draft task", () => {
|
|
|
|
DraftStore._onDestroyDraft('abc');
|
|
|
|
expect(Actions.queueTask).toHaveBeenCalled();
|
|
|
|
expect(Actions.queueTask.mostRecentCall.args[0] instanceof DestroyDraftTask).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should clean up the draft session", () => {
|
|
|
|
spyOn(DraftStore, '_doneWithSession');
|
|
|
|
DraftStore._onDestroyDraft('abc');
|
|
|
|
expect(DraftStore._doneWithSession).toHaveBeenCalledWith(this.session);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should close the window if it's a popout", () => {
|
|
|
|
spyOn(NylasEnv, "close");
|
feat(undo-send): Add undo send
Summary:
Add ability to undo send. We decided to make undo send completely client side for a couple of reasons. If we rely on send-later for undo-send, we would be giving /all/ send load to our send-later backend. If this increases the send-later load too much, it might cause delays in the regular send-later functionality and potentially other plugins like snooze that run under the same service. We would also need to rely on the network to be able to cancel a send, which would make it unusable offline or hard to debug if that specific request fails for any given reason.
This commit also refactors the way `ComposerExtension.sendActionConfig` works. The method has been renamed and now must return an array of send actions. Also, all of the business logic to handle different send actions registered by extensions has been pieced apart from the SendActionButton and into a new SendActionStore. This also enables undo send to undo custom send actions registered by extensions.
Along the way, this also fixes a pending TODO to show all registered custom send actions in the preferences for choosing the preferred send action for sending.
Undo send works via a task, so in case N1 closes before send goes through, it will still be persisted to the task queue and restored when opened again. Undoing a send means dequeuing this task.
Test Plan: Manual
Reviewers: jackie, bengotow, halla, evan
Reviewed By: bengotow, halla, evan
Differential Revision: https://phab.nylas.com/D3361
2016-10-22 02:48:04 +08:00
|
|
|
spyOn(NylasEnv, "isComposerWindow").andReturn(true);
|
2016-03-23 06:47:51 +08:00
|
|
|
DraftStore._onDestroyDraft('abc');
|
|
|
|
expect(NylasEnv.close).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should NOT close the window if isn't a popout", () => {
|
|
|
|
spyOn(NylasEnv, "close");
|
feat(undo-send): Add undo send
Summary:
Add ability to undo send. We decided to make undo send completely client side for a couple of reasons. If we rely on send-later for undo-send, we would be giving /all/ send load to our send-later backend. If this increases the send-later load too much, it might cause delays in the regular send-later functionality and potentially other plugins like snooze that run under the same service. We would also need to rely on the network to be able to cancel a send, which would make it unusable offline or hard to debug if that specific request fails for any given reason.
This commit also refactors the way `ComposerExtension.sendActionConfig` works. The method has been renamed and now must return an array of send actions. Also, all of the business logic to handle different send actions registered by extensions has been pieced apart from the SendActionButton and into a new SendActionStore. This also enables undo send to undo custom send actions registered by extensions.
Along the way, this also fixes a pending TODO to show all registered custom send actions in the preferences for choosing the preferred send action for sending.
Undo send works via a task, so in case N1 closes before send goes through, it will still be persisted to the task queue and restored when opened again. Undoing a send means dequeuing this task.
Test Plan: Manual
Reviewers: jackie, bengotow, halla, evan
Reviewed By: bengotow, halla, evan
Differential Revision: https://phab.nylas.com/D3361
2016-10-22 02:48:04 +08:00
|
|
|
spyOn(NylasEnv, "isComposerWindow").andReturn(false);
|
2016-03-23 06:47:51 +08:00
|
|
|
DraftStore._onDestroyDraft('abc');
|
|
|
|
expect(NylasEnv.close).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("before unloading", () => {
|
|
|
|
it("should destroy pristine drafts", () => {
|
2016-05-07 05:10:28 +08:00
|
|
|
DraftStore._draftSessions = {
|
|
|
|
abc: {
|
|
|
|
changes: {},
|
|
|
|
draft() {
|
|
|
|
return {pristine: true};
|
|
|
|
},
|
2016-03-23 06:47:51 +08:00
|
|
|
},
|
2016-05-07 05:10:28 +08:00
|
|
|
};
|
2016-03-23 06:47:51 +08:00
|
|
|
|
|
|
|
spyOn(Actions, 'queueTask');
|
|
|
|
DraftStore._onBeforeUnload();
|
|
|
|
expect(Actions.queueTask).toHaveBeenCalled();
|
|
|
|
expect(Actions.queueTask.mostRecentCall.args[0] instanceof DestroyDraftTask).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when drafts return unresolved commit promises", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
this.resolve = null;
|
|
|
|
DraftStore._draftSessions = {
|
2016-05-07 05:10:28 +08:00
|
|
|
abc: {
|
2016-03-23 06:47:51 +08:00
|
|
|
changes: {
|
2016-05-07 05:10:28 +08:00
|
|
|
commit: () => new Promise((resolve) => { this.resolve = resolve }),
|
2016-03-23 06:47:51 +08:00
|
|
|
},
|
|
|
|
draft() {
|
|
|
|
return {pristine: false};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return false and call window.close itself", () => {
|
|
|
|
const callback = jasmine.createSpy('callback');
|
|
|
|
expect(DraftStore._onBeforeUnload(callback)).toBe(false);
|
|
|
|
expect(callback).not.toHaveBeenCalled();
|
|
|
|
this.resolve();
|
|
|
|
advanceClock(1000);
|
2016-07-29 02:20:02 +08:00
|
|
|
advanceClock(1000);
|
2016-03-23 06:47:51 +08:00
|
|
|
expect(callback).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when drafts return immediately fulfilled commit promises", () => {
|
|
|
|
beforeEach(() => {
|
2016-05-07 05:10:28 +08:00
|
|
|
DraftStore._draftSessions = {
|
|
|
|
abc: {
|
|
|
|
changes:
|
|
|
|
{commit: () => Promise.resolve()},
|
|
|
|
draft() {
|
|
|
|
return {pristine: false};
|
|
|
|
},
|
2016-03-23 06:47:51 +08:00
|
|
|
},
|
2016-05-07 05:10:28 +08:00
|
|
|
};
|
2016-03-23 06:47:51 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should still wait one tick before firing NylasEnv.close again", () => {
|
|
|
|
const callback = jasmine.createSpy('callback');
|
|
|
|
expect(DraftStore._onBeforeUnload(callback)).toBe(false);
|
|
|
|
expect(callback).not.toHaveBeenCalled();
|
|
|
|
advanceClock();
|
2016-07-29 02:20:02 +08:00
|
|
|
advanceClock(1000);
|
2016-03-23 06:47:51 +08:00
|
|
|
expect(callback).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there are no drafts", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
DraftStore._draftSessions = {};
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return true and allow the window to close", () => {
|
|
|
|
expect(DraftStore._onBeforeUnload()).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("sending a draft", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
this.draft = new Message({
|
|
|
|
clientId: "local-123",
|
|
|
|
threadId: "thread-123",
|
|
|
|
replyToMessageId: "message-123",
|
|
|
|
uploads: ['stub'],
|
|
|
|
});
|
|
|
|
DraftStore._draftSessions = {};
|
|
|
|
DraftStore._draftsSending = {};
|
|
|
|
this.forceCommit = false;
|
2016-04-20 07:08:58 +08:00
|
|
|
const session = {
|
2016-03-23 06:47:51 +08:00
|
|
|
prepare() {
|
2016-04-20 07:08:58 +08:00
|
|
|
return Promise.resolve(session);
|
2016-03-23 06:47:51 +08:00
|
|
|
},
|
|
|
|
teardown() {},
|
|
|
|
draft: () => this.draft,
|
|
|
|
changes: {
|
|
|
|
commit: ({force} = {}) => {
|
|
|
|
this.forceCommit = force;
|
|
|
|
return Promise.resolve();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2016-04-20 07:08:58 +08:00
|
|
|
DraftStore._draftSessions[this.draft.clientId] = session;
|
2016-03-23 06:47:51 +08:00
|
|
|
spyOn(DraftStore, "_doneWithSession").andCallThrough();
|
2016-05-19 07:19:42 +08:00
|
|
|
spyOn(DraftHelpers, "prepareDraftForSyncback").andReturn(Promise.resolve());
|
2016-03-23 06:47:51 +08:00
|
|
|
spyOn(DraftStore, "trigger");
|
|
|
|
spyOn(SoundRegistry, "playSound");
|
|
|
|
spyOn(Actions, "queueTask");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("plays a sound immediately when sending draft", () => {
|
|
|
|
spyOn(NylasEnv.config, "get").andReturn(true);
|
|
|
|
DraftStore._onSendDraft(this.draft.clientId);
|
|
|
|
advanceClock();
|
|
|
|
expect(NylasEnv.config.get).toHaveBeenCalledWith("core.sending.sounds");
|
|
|
|
expect(SoundRegistry.playSound).toHaveBeenCalledWith("hit-send");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("doesn't plays a sound if the setting is off", () => {
|
|
|
|
spyOn(NylasEnv.config, "get").andReturn(false);
|
|
|
|
DraftStore._onSendDraft(this.draft.clientId);
|
|
|
|
advanceClock();
|
|
|
|
expect(NylasEnv.config.get).toHaveBeenCalledWith("core.sending.sounds");
|
|
|
|
expect(SoundRegistry.playSound).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sets the sending state when sending", () => {
|
|
|
|
spyOn(NylasEnv, "isMainWindow").andReturn(true);
|
|
|
|
DraftStore._onSendDraft(this.draft.clientId);
|
|
|
|
advanceClock();
|
|
|
|
expect(DraftStore.isSendingDraft(this.draft.clientId)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Since all changes haven't been applied yet, we want to ensure that
|
|
|
|
// no view of the draft renders the draft as if its sending, but with
|
|
|
|
// the wrong text.
|
|
|
|
it("does NOT trigger until the latest changes have been applied", () => {
|
|
|
|
spyOn(NylasEnv, "isMainWindow").andReturn(true);
|
|
|
|
runs(() => {
|
|
|
|
DraftStore._onSendDraft(this.draft.clientId);
|
|
|
|
expect(DraftStore.trigger).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
waitsFor(() => {
|
|
|
|
return Actions.queueTask.calls.length > 0;
|
|
|
|
});
|
|
|
|
runs(() => {
|
|
|
|
// Normally, the session.changes.commit will persist to the
|
|
|
|
// Database. Since that's stubbed out, we need to manually invoke
|
|
|
|
// to database update event to get the trigger (which we want to
|
|
|
|
// test) to fire
|
|
|
|
DraftStore._onDataChanged({
|
|
|
|
objectClass: "Message",
|
|
|
|
objects: [{draft: true}],
|
|
|
|
});
|
|
|
|
expect(DraftStore.isSendingDraft(this.draft.clientId)).toBe(true);
|
|
|
|
expect(DraftStore.trigger).toHaveBeenCalled();
|
|
|
|
expect(DraftStore.trigger.calls.length).toBe(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns false if the draft hasn't been seen", () => {
|
|
|
|
spyOn(NylasEnv, "isMainWindow").andReturn(true);
|
|
|
|
expect(DraftStore.isSendingDraft(this.draft.clientId)).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("closes the window if it's a popout", () => {
|
|
|
|
spyOn(NylasEnv, "getWindowType").andReturn("composer");
|
|
|
|
spyOn(NylasEnv, "isMainWindow").andReturn(false);
|
|
|
|
spyOn(NylasEnv, "close");
|
|
|
|
runs(() => {
|
|
|
|
return DraftStore._onSendDraft(this.draft.clientId);
|
|
|
|
});
|
|
|
|
waitsFor("N1 to close", () => NylasEnv.close.calls.length > 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("doesn't close the window if it's inline", () => {
|
|
|
|
spyOn(NylasEnv, "getWindowType").andReturn("other");
|
|
|
|
spyOn(NylasEnv, "isMainWindow").andReturn(false);
|
|
|
|
spyOn(NylasEnv, "close");
|
feat(undo-send): Add undo send
Summary:
Add ability to undo send. We decided to make undo send completely client side for a couple of reasons. If we rely on send-later for undo-send, we would be giving /all/ send load to our send-later backend. If this increases the send-later load too much, it might cause delays in the regular send-later functionality and potentially other plugins like snooze that run under the same service. We would also need to rely on the network to be able to cancel a send, which would make it unusable offline or hard to debug if that specific request fails for any given reason.
This commit also refactors the way `ComposerExtension.sendActionConfig` works. The method has been renamed and now must return an array of send actions. Also, all of the business logic to handle different send actions registered by extensions has been pieced apart from the SendActionButton and into a new SendActionStore. This also enables undo send to undo custom send actions registered by extensions.
Along the way, this also fixes a pending TODO to show all registered custom send actions in the preferences for choosing the preferred send action for sending.
Undo send works via a task, so in case N1 closes before send goes through, it will still be persisted to the task queue and restored when opened again. Undoing a send means dequeuing this task.
Test Plan: Manual
Reviewers: jackie, bengotow, halla, evan
Reviewed By: bengotow, halla, evan
Differential Revision: https://phab.nylas.com/D3361
2016-10-22 02:48:04 +08:00
|
|
|
spyOn(NylasEnv, "isComposerWindow").andCallThrough();
|
2016-03-23 06:47:51 +08:00
|
|
|
runs(() => {
|
|
|
|
DraftStore._onSendDraft(this.draft.clientId);
|
|
|
|
});
|
feat(undo-send): Add undo send
Summary:
Add ability to undo send. We decided to make undo send completely client side for a couple of reasons. If we rely on send-later for undo-send, we would be giving /all/ send load to our send-later backend. If this increases the send-later load too much, it might cause delays in the regular send-later functionality and potentially other plugins like snooze that run under the same service. We would also need to rely on the network to be able to cancel a send, which would make it unusable offline or hard to debug if that specific request fails for any given reason.
This commit also refactors the way `ComposerExtension.sendActionConfig` works. The method has been renamed and now must return an array of send actions. Also, all of the business logic to handle different send actions registered by extensions has been pieced apart from the SendActionButton and into a new SendActionStore. This also enables undo send to undo custom send actions registered by extensions.
Along the way, this also fixes a pending TODO to show all registered custom send actions in the preferences for choosing the preferred send action for sending.
Undo send works via a task, so in case N1 closes before send goes through, it will still be persisted to the task queue and restored when opened again. Undoing a send means dequeuing this task.
Test Plan: Manual
Reviewers: jackie, bengotow, halla, evan
Reviewed By: bengotow, halla, evan
Differential Revision: https://phab.nylas.com/D3361
2016-10-22 02:48:04 +08:00
|
|
|
waitsFor(() => NylasEnv.isComposerWindow.calls.length > 0);
|
2016-03-23 06:47:51 +08:00
|
|
|
runs(() => {
|
|
|
|
expect(NylasEnv.close).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("resets the sending state if there's an error", () => {
|
|
|
|
spyOn(NylasEnv, "isMainWindow").andReturn(false);
|
|
|
|
DraftStore._draftsSending[this.draft.clientId] = true;
|
2017-01-05 07:42:19 +08:00
|
|
|
Actions.draftDeliveryFailed({errorMessage: "boohoo", draftClientId: this.draft.clientId});
|
2016-03-23 06:47:51 +08:00
|
|
|
expect(DraftStore.isSendingDraft(this.draft.clientId)).toBe(false);
|
|
|
|
expect(DraftStore.trigger).toHaveBeenCalledWith(this.draft.clientId);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("displays a popup in the main window if there's an error", () => {
|
|
|
|
spyOn(NylasEnv, "isMainWindow").andReturn(true);
|
|
|
|
spyOn(FocusedContentStore, "focused").andReturn({id: "t1"});
|
|
|
|
spyOn(remote.dialog, "showMessageBox");
|
|
|
|
spyOn(Actions, "composePopoutDraft");
|
|
|
|
DraftStore._draftsSending[this.draft.clientId] = true;
|
2017-01-05 07:42:19 +08:00
|
|
|
Actions.draftDeliveryFailed({threadId: 't1', errorMessage: "boohoo", draftClientId: this.draft.clientId});
|
2016-06-11 02:32:38 +08:00
|
|
|
advanceClock(400);
|
2016-03-23 06:47:51 +08:00
|
|
|
expect(DraftStore.isSendingDraft(this.draft.clientId)).toBe(false);
|
|
|
|
expect(DraftStore.trigger).toHaveBeenCalledWith(this.draft.clientId);
|
|
|
|
expect(remote.dialog.showMessageBox).toHaveBeenCalled();
|
|
|
|
const dialogArgs = remote.dialog.showMessageBox.mostRecentCall.args[1];
|
|
|
|
expect(dialogArgs.detail).toEqual("boohoo");
|
|
|
|
expect(Actions.composePopoutDraft).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("re-opens the draft if you're not looking at the thread", () => {
|
|
|
|
spyOn(NylasEnv, "isMainWindow").andReturn(true);
|
|
|
|
spyOn(FocusedContentStore, "focused").andReturn({id: "t1"});
|
|
|
|
spyOn(Actions, "composePopoutDraft");
|
|
|
|
DraftStore._draftsSending[this.draft.clientId] = true;
|
2017-01-05 07:42:19 +08:00
|
|
|
Actions.draftDeliveryFailed({threadId: 't2', errorMessage: "boohoo", draftClientId: this.draft.clientId});
|
2016-06-11 02:32:38 +08:00
|
|
|
advanceClock(400);
|
2016-03-23 06:47:51 +08:00
|
|
|
expect(Actions.composePopoutDraft).toHaveBeenCalled();
|
|
|
|
const call = Actions.composePopoutDraft.calls[0];
|
|
|
|
expect(call.args[0]).toBe(this.draft.clientId);
|
|
|
|
expect(call.args[1]).toEqual({errorMessage: "boohoo"});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("re-opens the draft if there is no thread id", () => {
|
|
|
|
spyOn(NylasEnv, "isMainWindow").andReturn(true);
|
|
|
|
spyOn(Actions, "composePopoutDraft");
|
|
|
|
DraftStore._draftsSending[this.draft.clientId] = true;
|
|
|
|
spyOn(FocusedContentStore, "focused").andReturn(null);
|
2017-01-05 07:42:19 +08:00
|
|
|
Actions.draftDeliveryFailed({errorMessage: "boohoo", draftClientId: this.draft.clientId});
|
2016-06-11 02:32:38 +08:00
|
|
|
advanceClock(400);
|
2016-03-23 06:47:51 +08:00
|
|
|
expect(Actions.composePopoutDraft).toHaveBeenCalled();
|
|
|
|
const call = Actions.composePopoutDraft.calls[0];
|
|
|
|
expect(call.args[0]).toBe(this.draft.clientId);
|
|
|
|
expect(call.args[1]).toEqual({errorMessage: "boohoo"});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("session teardown", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
spyOn(NylasEnv, 'isMainWindow').andReturn(true);
|
|
|
|
this.draftTeardown = jasmine.createSpy('draft teardown');
|
|
|
|
this.session = {
|
|
|
|
draftClientId: "abc",
|
|
|
|
draft() {
|
|
|
|
return {pristine: false};
|
|
|
|
},
|
|
|
|
changes: {
|
|
|
|
commit() { return Promise.resolve(); },
|
|
|
|
reset() {},
|
|
|
|
},
|
|
|
|
teardown: this.draftTeardown,
|
|
|
|
};
|
2016-05-07 05:10:28 +08:00
|
|
|
DraftStore._draftSessions = {abc: this.session};
|
2016-03-23 06:47:51 +08:00
|
|
|
DraftStore._doneWithSession(this.session);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("removes from the list of draftSessions", () => {
|
|
|
|
expect(DraftStore._draftSessions.abc).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Calls teardown on the session", () => {
|
|
|
|
expect(this.draftTeardown).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("mailto handling", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
spyOn(NylasEnv, 'isMainWindow').andReturn(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("extensions", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
ExtensionRegistry.Composer.register(TestExtension);
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
ExtensionRegistry.Composer.unregister(TestExtension);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should give extensions a chance to customize the draft via ext.prepareNewDraft", () => {
|
|
|
|
waitsForPromise(() => {
|
|
|
|
return DraftStore._onHandleMailtoLink({}, 'mailto:bengotow@gmail.com').then(() => {
|
|
|
|
const received = DatabaseTransaction.prototype.persistModel.mostRecentCall.args[0];
|
|
|
|
expect(received.body.indexOf("Edited by TestExtension!")).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should call through to DraftFactory and popout a new draft", () => {
|
|
|
|
const draft = new Message({clientId: "A", body: '123'});
|
|
|
|
spyOn(DraftFactory, 'createDraftForMailto').andReturn(Promise.resolve(draft));
|
|
|
|
spyOn(DraftStore, '_onPopoutDraftClientId');
|
|
|
|
waitsForPromise(() => {
|
|
|
|
return DraftStore._onHandleMailtoLink({}, 'mailto:bengotow@gmail.com').then(() => {
|
|
|
|
const received = DatabaseTransaction.prototype.persistModel.mostRecentCall.args[0];
|
|
|
|
expect(received).toEqual(draft);
|
|
|
|
expect(DraftStore._onPopoutDraftClientId).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("mailfiles handling", () => {
|
|
|
|
it("should popout a new draft", () => {
|
|
|
|
const defaultMe = new Contact();
|
|
|
|
spyOn(DraftStore, '_onPopoutDraftClientId');
|
|
|
|
spyOn(Account.prototype, 'defaultMe').andReturn(defaultMe);
|
2016-09-27 07:59:43 +08:00
|
|
|
spyOn(Actions, 'addAttachment').andCallFake(({onUploadCreated}) => onUploadCreated());
|
2016-03-23 06:47:51 +08:00
|
|
|
DraftStore._onHandleMailFiles({}, ['/Users/ben/file1.png', '/Users/ben/file2.png']);
|
|
|
|
waitsFor(() => DatabaseTransaction.prototype.persistModel.callCount > 0);
|
|
|
|
runs(() => {
|
|
|
|
const {body, subject, from} = DatabaseTransaction.prototype.persistModel.calls[0].args[0];
|
|
|
|
expect({body, subject, from}).toEqual({body: '', subject: '', from: [defaultMe]});
|
|
|
|
expect(DraftStore._onPopoutDraftClientId).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should call addAttachment for each provided file path", () => {
|
|
|
|
spyOn(Actions, 'addAttachment');
|
|
|
|
DraftStore._onHandleMailFiles({}, ['/Users/ben/file1.png', '/Users/ben/file2.png']);
|
|
|
|
waitsFor(() => Actions.addAttachment.callCount === 2);
|
|
|
|
runs(() => {
|
|
|
|
expect(Actions.addAttachment.calls[0].args[0].filePath).toEqual('/Users/ben/file1.png');
|
|
|
|
expect(Actions.addAttachment.calls[1].args[0].filePath).toEqual('/Users/ben/file2.png');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|