Mailspring/packages/client-app/internal_packages/composer/spec/send-action-button-spec.jsx

137 lines
4.7 KiB
React
Raw Normal View History

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
import React from 'react';
import {mount} from 'enzyme';
import {ButtonDropdown, RetinaImg} from 'nylas-component-kit';
import {Actions, Message, SendActionsStore} from 'nylas-exports';
import SendActionButton from '../lib/send-action-button';
const {UndecoratedSendActionButton} = SendActionButton;
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
const {DefaultSendAction} = SendActionsStore
const GoodSendAction = {
title: "Good Send Action",
configKey: 'good-send-action',
isAvailableForDraft: () => true,
performSendAction: () => {},
}
const SecondSendAction = {
title: "Second Send Action",
configKey: 'second-send-action',
isAvailableForDraft: () => true,
performSendAction: () => {},
}
const NoIconUrl = {
title: "No Icon",
configKey: 'no-icon',
iconUrl: null,
isAvailableForDraft: () => true,
performSendAction() {},
}
describe('SendActionButton', function describeBlock() {
beforeEach(() => {
spyOn(NylasEnv, 'reportError')
spyOn(Actions, 'sendDraft')
this.isValidDraft = jasmine.createSpy('isValidDraft')
this.clientId = "client-23"
this.draft = new Message({clientId: this.clientId, draft: true})
})
const render = (draft, {isValid = true, sendActions = [], ordered = {}} = {}) => {
this.isValidDraft.andReturn(isValid)
return mount(
<UndecoratedSendActionButton
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
draft={draft}
isValidDraft={this.isValidDraft}
sendActions={[DefaultSendAction].concat(sendActions)}
orderedSendActions={{
preferred: ordered.preferred || DefaultSendAction,
rest: ordered.rest || [],
}}
/>
)
}
it("renders without error", () => {
const sendActionButton = render(this.draft);
expect(sendActionButton.is(UndecoratedSendActionButton)).toBe(true);
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
});
it("initializes with the default and shows the standard Send option", () => {
const sendActionButton = render(this.draft);
const button = sendActionButton.find('button').first();
expect(button.text()).toEqual('Send');
});
it("is a single button when there are no send actions", () => {
const sendActionButton = render(this.draft, {sendActions: []});
const dropdowns = sendActionButton.find(ButtonDropdown);
const buttons = sendActionButton.find('button');
expect(buttons.length).toBe(1);
expect(dropdowns.length).toBe(0);
expect(buttons.first().text()).toBe('Send');
});
it("is a dropdown when there's more than one send action", () => {
const sendActionButton = render(this.draft, {
sendActions: [GoodSendAction],
});
const dropdowns = sendActionButton.find(ButtonDropdown);
const buttons = sendActionButton.find('button');
expect(buttons.length).toBe(0);
expect(dropdowns.length).toBe(1);
expect(dropdowns.first().prop('primaryTitle')).toBe('Send');
});
it("has the correct primary item", () => {
const sendActionButton = render(this.draft, {
sendActions: [GoodSendAction, SecondSendAction],
ordered: {preferred: SecondSendAction, rest: [DefaultSendAction, GoodSendAction]},
});
const dropdown = sendActionButton.find(ButtonDropdown).first();
expect(dropdown.prop('primaryTitle')).toBe("Second Send Action");
});
it("still renders with a null iconUrl and doesn't show the image", () => {
const sendActionButton = render(this.draft, {
sendActions: [NoIconUrl],
ordered: {preferred: NoIconUrl, rest: [DefaultSendAction]},
});
const dropdowns = sendActionButton.find(ButtonDropdown);
const buttons = sendActionButton.find('button');
const icons = sendActionButton.find(RetinaImg)
expect(buttons.length).toBe(0);
expect(dropdowns.length).toBe(1);
expect(icons.length).toBe(3);
});
it("sends a draft by default if no extra actions present", () => {
const sendActionButton = render(this.draft);
const button = sendActionButton.find('button').first();
button.simulate('click')
expect(this.isValidDraft).toHaveBeenCalled();
expect(Actions.sendDraft).toHaveBeenCalledWith(this.draft.clientId, 'send');
});
it("doesn't send a draft if the isValidDraft fails", () => {
const sendActionButton = render(this.draft, {isValid: false});
const button = sendActionButton.find('button').first();
button.simulate('click')
expect(this.isValidDraft).toHaveBeenCalled();
expect(Actions.sendDraft).not.toHaveBeenCalled();
});
it("does the preferred action when more than one action present", () => {
const sendActionButton = render(this.draft, {
sendActions: [GoodSendAction],
ordered: {preferred: GoodSendAction, rest: [DefaultSendAction]},
});
const button = sendActionButton.find('.primary-item').first();
button.simulate('click')
expect(this.isValidDraft).toHaveBeenCalled();
expect(Actions.sendDraft).toHaveBeenCalledWith(this.draft.clientId, 'good-send-action');
});
});