Mailspring/spec/stores/send-actions-store-spec.es6
Juan Tejada 5d837ffd02 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-26 20:40:10 -07:00

204 lines
6.9 KiB
JavaScript

import {Message, SendActionsStore, ExtensionRegistry} from 'nylas-exports'
const SendAction1 = {
title: "Send Action 1",
isAvailableForDraft: () => true,
performSendAction: () => {},
}
const SendAction2 = {
title: "Send Action 2",
isAvailableForDraft: () => true,
performSendAction: () => {},
}
const SendAction3 = {
title: "Send Action 3",
isAvailableForDraft: () => true,
performSendAction: () => {},
}
const NoTitleAction = {
isAvailableForDraft: () => true,
performSendAction: () => {},
}
const NoPerformAction = {
title: "No Perform",
isAvailableForDraft: () => true,
}
const NotAvailableAction = {
title: "Not Available",
isAvailableForDraft: () => false,
performSendAction: () => {},
}
const GoodExtension = {
name: 'GoodExtension',
sendActions() {
return [SendAction1]
},
}
const BadExtension = {
name: 'BadExtension',
sendActions() {
return [null]
},
}
const NoTitleExtension = {
name: 'NoTitleExtension',
sendActions() {
return [NoTitleAction]
},
}
const NoPerformExtension = {
name: 'NoPerformExtension',
sendActions() {
return [NoPerformAction]
},
}
const NotAvailableExtension = {
name: 'NotAvailableExtension',
sendActions() {
return [NotAvailableAction]
},
}
const NullExtension = {
name: 'NullExtension',
sendActions() {
return null
},
}
const OtherExtension = {
name: 'OtherExtension',
sendActions() {
return [SendAction2, SendAction3]
},
}
const {DefaultSendActionKey} = SendActionsStore
function sendActionKeys() {
return SendActionsStore.sendActions().map(({configKey}) => configKey)
}
describe('SendActionsStore', function describeBlock() {
beforeEach(() => {
this.clientId = "client-23"
this.draft = new Message({clientId: this.clientId, draft: true})
spyOn(NylasEnv, 'reportError')
});
describe('sendActions', () => {
it("returns default action when no extensions registered", () => {
spyOn(ExtensionRegistry.Composer, 'extensions').andReturn([]);
SendActionsStore._onComposerExtensionsChanged()
expect(sendActionKeys()).toEqual([DefaultSendActionKey])
});
it('returns correct send actions', () => {
spyOn(ExtensionRegistry.Composer, 'extensions').andReturn([GoodExtension, OtherExtension]);
SendActionsStore._onComposerExtensionsChanged()
expect(sendActionKeys()).toEqual([DefaultSendActionKey, 'send-action-1', 'send-action-2', 'send-action-3'])
});
it('handles extensions that return null for `sendActions`', () => {
spyOn(ExtensionRegistry.Composer, 'extensions').andReturn([GoodExtension, NullExtension]);
SendActionsStore._onComposerExtensionsChanged()
expect(sendActionKeys()).toEqual([DefaultSendActionKey, 'send-action-1'])
});
it('handles extensions that return null actions', () => {
spyOn(ExtensionRegistry.Composer, 'extensions').andReturn([GoodExtension, BadExtension]);
SendActionsStore._onComposerExtensionsChanged()
expect(sendActionKeys()).toEqual([DefaultSendActionKey, 'send-action-1'])
});
it('omits and reports when action is missing a title', () => {
spyOn(ExtensionRegistry.Composer, 'extensions').andReturn([GoodExtension, NoTitleExtension]);
SendActionsStore._onComposerExtensionsChanged()
expect(sendActionKeys()).toEqual([DefaultSendActionKey, 'send-action-1'])
expect(NylasEnv.reportError).toHaveBeenCalled()
});
it('omits reports when action is missing performSendAction', () => {
spyOn(ExtensionRegistry.Composer, 'extensions').andReturn([GoodExtension, NoPerformExtension]);
SendActionsStore._onComposerExtensionsChanged()
expect(sendActionKeys()).toEqual([DefaultSendActionKey, 'send-action-1'])
expect(NylasEnv.reportError).toHaveBeenCalled()
});
it('includes not available actions', () => {
spyOn(ExtensionRegistry.Composer, 'extensions').andReturn([GoodExtension, NotAvailableExtension]);
SendActionsStore._onComposerExtensionsChanged()
expect(sendActionKeys()).toEqual([DefaultSendActionKey, 'send-action-1', 'not-available'])
});
});
describe('availableSendActionsForDraft', () => {
it('excludes not available actions', () => {
spyOn(ExtensionRegistry.Composer, 'extensions').andReturn([GoodExtension, NotAvailableExtension]);
SendActionsStore._onComposerExtensionsChanged()
const actions = SendActionsStore.availableSendActionsForDraft()
expect(actions.map(({configKey}) => configKey)).toEqual([DefaultSendActionKey, 'send-action-1'])
});
});
describe('orderedSendActionsForDraft', () => {
it("returns default action when no extensions registered", () => {
spyOn(ExtensionRegistry.Composer, 'extensions').andReturn([]);
SendActionsStore._onComposerExtensionsChanged()
const {preferred, rest} = SendActionsStore.orderedSendActionsForDraft()
expect(preferred.configKey).toBe(DefaultSendActionKey)
expect(rest).toEqual([])
});
it('returns actions in correct grouping', () => {
spyOn(ExtensionRegistry.Composer, 'extensions').andReturn([GoodExtension, OtherExtension, NotAvailableExtension]);
spyOn(NylasEnv.config, 'get').andReturn('send-action-1');
SendActionsStore._onComposerExtensionsChanged()
const {preferred, rest} = SendActionsStore.orderedSendActionsForDraft()
const restKeys = rest.map(({configKey}) => configKey)
expect(preferred.configKey).toBe('send-action-1')
expect(restKeys).toEqual([DefaultSendActionKey, 'send-action-2', 'send-action-3'])
});
it("falls back to a default if value in config not present", () => {
spyOn(ExtensionRegistry.Composer, 'extensions').andReturn([GoodExtension, OtherExtension]);
spyOn(NylasEnv.config, 'get').andReturn(null);
SendActionsStore._onComposerExtensionsChanged()
const {preferred} = SendActionsStore.orderedSendActionsForDraft()
expect(preferred.configKey).toBe(DefaultSendActionKey)
});
it("falls back to a default if the primary item can't be found", () => {
spyOn(ExtensionRegistry.Composer, 'extensions').andReturn([GoodExtension, OtherExtension]);
spyOn(NylasEnv.config, 'get').andReturn('does-not-exist');
SendActionsStore._onComposerExtensionsChanged()
const {preferred} = SendActionsStore.orderedSendActionsForDraft()
expect(preferred.configKey).toBe(DefaultSendActionKey)
});
it("falls back to a default if the primary item is not available for draft", () => {
spyOn(ExtensionRegistry.Composer, 'extensions').andReturn([GoodExtension, NotAvailableExtension]);
spyOn(NylasEnv.config, 'get').andReturn('not-available');
SendActionsStore._onComposerExtensionsChanged()
const {preferred} = SendActionsStore.orderedSendActionsForDraft()
expect(preferred.configKey).toBe(DefaultSendActionKey)
});
});
// TODO Should go Task spec
it("catches any errors in an extension performSendAction method", () => {
});
});