Mailspring/internal_packages/send-later/spec/send-later-button-spec.jsx
Ben Gotow 552b66fbaf fix(syncback): Bidirectional transforms, ready-to-send saved state
Summary:
This diff replaces "finalizeSessionBeforeSending" with a
plugin hook that is bidirectional and allows us to put the draft in
the "ready to send" state every time we save it, and restore it to
the "ready to edit" state every time a draft session is created to
edit it.

This diff also significantly restructures the draft tasks:

1. SyncbackDraftUploadsTask:
   - ensures that `uploads` are converted to `files` and that any
     existing files on the draft are part of the correct account.

1. SyncbackDraftTask:
   - saves the draft, nothing else.

3. SendDraftTask
   - sends the draft, nothing else.
   - deletes the entire uploads directory for the draft

Test Plan: WIP

Reviewers: juan, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D2753
2016-03-16 19:27:12 -07:00

104 lines
3.4 KiB
JavaScript

import React, {addons} from 'react/addons';
import {Rx, DatabaseStore, DateUtils, Actions} from 'nylas-exports'
import SendLaterButton from '../lib/send-later-button';
import SendLaterActions from '../lib/send-later-actions';
import {renderIntoDocument} from '../../../spec/nylas-test-utils'
const {findDOMNode} = React;
const {TestUtils: {
findRenderedDOMComponentWithClass,
}} = addons;
const makeButton = (props = {})=> {
const button = renderIntoDocument(<SendLaterButton {...props} draftClientId="1" />);
if (props.initialState) {
button.setState(props.initialState)
}
return button
};
describe('SendLaterButton', ()=> {
beforeEach(()=> {
spyOn(DatabaseStore, 'findBy')
spyOn(Rx.Observable, 'fromQuery').andReturn(Rx.Observable.empty())
spyOn(DateUtils, 'format').andReturn('formatted')
spyOn(SendLaterActions, 'sendLater')
});
describe('onMessageChanged', ()=> {
it('sets scheduled date correctly', ()=> {
const button = makeButton({initialState: {scheduledDate: 'old'}})
const message = {
metadataForPluginId: ()=> ({sendLaterDate: 'date'}),
}
spyOn(button, 'setState')
spyOn(NylasEnv, 'isComposerWindow').andReturn(false)
button.onMessageChanged(message)
expect(button.setState).toHaveBeenCalledWith({scheduledDate: 'date'})
});
it('closes window if window is composer window and saving has finished', ()=> {
const button = makeButton({initialState: {scheduledDate: 'saving'}})
const message = {
metadataForPluginId: ()=> ({sendLaterDate: 'date'}),
}
spyOn(button, 'setState')
spyOn(NylasEnv, 'close')
spyOn(NylasEnv, 'isComposerWindow').andReturn(true)
button.onMessageChanged(message)
expect(button.setState).toHaveBeenCalledWith({scheduledDate: 'date'})
expect(NylasEnv.close).toHaveBeenCalled()
});
it('does nothing if new date is the same as current date', ()=> {
const button = makeButton({initialState: {scheduledDate: 'date'}})
const message = {
metadataForPluginId: ()=> ({sendLaterDate: 'date'}),
}
spyOn(button, 'setState')
button.onMessageChanged(message)
expect(button.setState).not.toHaveBeenCalled()
});
});
describe('onSendLater', ()=> {
it('sets scheduled date to "saving" and dispatches action', ()=> {
const button = makeButton()
spyOn(button, 'setState')
button.onSendLater({utc: ()=> 'utc'})
expect(SendLaterActions.sendLater).toHaveBeenCalled()
expect(button.setState).toHaveBeenCalledWith({scheduledDate: 'saving'})
});
});
describe('render', ()=> {
it('renders spinner if saving', ()=> {
const button = findDOMNode(
makeButton({initialState: {scheduledDate: 'saving'}})
)
expect(button.title).toEqual('Saving send date...')
});
it('renders date if message is scheduled', ()=> {
spyOn(DateUtils, 'futureDateFromString').andReturn({fromNow: ()=> '5 minutes'})
const button = makeButton({initialState: {scheduledDate: 'date'}})
const span = findDOMNode(findRenderedDOMComponentWithClass(button, 'at'))
expect(span.textContent).toEqual('Sending in 5 minutes')
});
it('does not render date if message is not scheduled', ()=> {
const button = makeButton({initialState: {scheduledDate: null}})
expect(()=> {
findRenderedDOMComponentWithClass(button, 'at')
}).toThrow()
});
});
});