2015-02-25 08:19:47 +08:00
Thread = require ' ../../src/flux/models/thread '
Message = require ' ../../src/flux/models/message '
Contact = require ' ../../src/flux/models/contact '
2015-03-26 03:41:48 +08:00
ModelQuery = require ' ../../src/flux/models/query '
2015-04-09 10:35:38 +08:00
NamespaceStore = require ' ../../src/flux/stores/namespace-store '
DatabaseStore = require ' ../../src/flux/stores/database-store '
DraftStore = require ' ../../src/flux/stores/draft-store '
2015-05-20 03:07:08 +08:00
TaskQueue = require ' ../../src/flux/stores/task-queue '
2015-03-13 05:48:56 +08:00
SendDraftTask = require ' ../../src/flux/tasks/send-draft '
2015-03-24 07:33:28 +08:00
DestroyDraftTask = require ' ../../src/flux/tasks/destroy-draft '
2015-03-13 05:48:56 +08:00
Actions = require ' ../../src/flux/actions '
2015-06-19 02:58:07 +08:00
Utils = require ' ../../src/flux/models/utils '
2015-05-20 07:06:59 +08:00
_ = require ' underscore '
2015-02-25 08:19:47 +08:00
2015-03-11 02:02:46 +08:00
fakeThread = null
fakeMessage1 = null
fakeMessage2 = null
2015-03-27 10:07:21 +08:00
msgFromMe = null
2015-05-29 09:13:46 +08:00
msgWithReplyTo = null
2015-03-11 02:02:46 +08:00
fakeMessages = null
2015-02-25 08:19:47 +08:00
fix(drafts): Various improvements and fixes to drafts, draft state management
Summary:
This diff contains a few major changes:
1. Scribe is no longer used for the text editor. It's just a plain contenteditable region. The toolbar items (bold, italic, underline) still work. Scribe was causing React inconcistency issues in the following scenario:
- View thread with draft, edit draft
- Move to another thread
- Move back to thread with draft
- Move to another thread. Notice that one or more messages from thread with draft are still there.
There may be a way to fix this, but I tried for hours and there are Github Issues open on it's repository asking for React compatibility, so it may be fixed soon. For now contenteditable is working great.
2. Action.saveDraft() is no longer debounced in the DraftStore. Instead, firing that action causes the save to happen immediately, and the DraftStoreProxy has a new "DraftChangeSet" class which is responsbile for batching saves as the user interacts with the ComposerView. There are a couple big wins here:
- In the future, we may want to be able to call Action.saveDraft() in other situations and it should behave like a normal action. We may also want to expose the DraftStoreProxy as an easy way of backing interactive draft UI.
- Previously, when you added a contact to To/CC/BCC, this happened:
<input> -> Action.saveDraft -> (delay!!) -> Database -> DraftStore -> DraftStoreProxy -> View Updates
Increasing the delay to something reasonable like 200msec meant there was 200msec of lag before you saw the new view state.
To fix this, I created a new class called DraftChangeSet which is responsible for accumulating changes as they're made and firing Action.saveDraft. "Adding" a change to the change set also causes the Draft provided by the DraftStoreProxy to change immediately (the changes are a temporary layer on top of the database object). This means no delay while changes are being applied. There's a better explanation in the source!
This diff includes a few minor fixes as well:
1. Draft.state is gone—use Message.object = draft instead
2. String model attributes should never be null
3. Pre-send checks that can cancel draft send
4. Put the entire curl history and task queue into feedback reports
5. Cache localIds for extra speed
6. Move us up to latest React
Test Plan: No new tests - once we lock down this new design I'll write tests for the DraftChangeSet
Reviewers: evan
Reviewed By: evan
Differential Revision: https://review.inboxapp.com/D1125
2015-02-04 08:24:31 +08:00
describe " DraftStore " , ->
2015-02-25 08:19:47 +08:00
describe " creating drafts " , ->
beforeEach ->
2015-03-11 02:02:46 +08:00
fakeThread = new Thread
id: ' fake-thread-id '
subject: ' Fake Subject '
fakeMessage1 = new Message
id: ' fake-message-1 '
2015-05-15 08:08:30 +08:00
to: [ new Contact ( email: ' ben@nylas.com ' ) , new Contact ( email: ' evan@nylas.com ' ) ]
cc: [ new Contact ( email: ' mg@nylas.com ' ) , new Contact ( email: NamespaceStore . current ( ) . me ( ) . email ) ]
bcc: [ new Contact ( email: ' recruiting@nylas.com ' ) ]
2015-03-11 02:02:46 +08:00
from: [ new Contact ( email: ' customer@example.com ' , name: ' Customer ' ) ]
threadId: ' fake-thread-id '
body: ' Fake Message 1 '
2015-03-12 06:55:04 +08:00
subject: ' Fake Subject '
2015-03-11 02:02:46 +08:00
date: new Date ( 1415814587 )
fakeMessage2 = new Message
id: ' fake-message-2 '
to: [ new Contact ( email: ' customer@example.com ' ) ]
2015-05-15 08:08:30 +08:00
from: [ new Contact ( email: ' ben@nylas.com ' ) ]
2015-03-11 02:02:46 +08:00
threadId: ' fake-thread-id '
body: ' Fake Message 2 '
2015-03-12 06:55:04 +08:00
subject: ' Re: Fake Subject '
2015-03-11 02:02:46 +08:00
date: new Date ( 1415814587 )
2015-03-27 10:07:21 +08:00
msgFromMe = new Message
id: ' fake-message-3 '
to: [ new Contact ( email: ' 1@1.com ' ) , new Contact ( email: ' 2@2.com ' ) ]
cc: [ new Contact ( email: ' 3@3.com ' ) , new Contact ( email: ' 4@4.com ' ) ]
bcc: [ new Contact ( email: ' 5@5.com ' ) , new Contact ( email: ' 6@6.com ' ) ]
from: [ new Contact ( email: NamespaceStore . current ( ) . me ( ) . email ) ]
threadId: ' fake-thread-id '
body: ' Fake Message 2 '
subject: ' Re: Fake Subject '
date: new Date ( 1415814587 )
2015-05-29 09:13:46 +08:00
msgWithReplyTo = new Message
id: ' fake-message-reply-to '
to: [ new Contact ( email: ' 1@1.com ' ) , new Contact ( email: ' 2@2.com ' ) ]
cc: [ new Contact ( email: ' 3@3.com ' ) , new Contact ( email: ' 4@4.com ' ) ]
bcc: [ new Contact ( email: ' 5@5.com ' ) , new Contact ( email: ' 6@6.com ' ) ]
replyTo: [ new Contact ( email: ' reply-to@5.com ' ) , new Contact ( email: ' reply-to@6.com ' ) ]
from: [ new Contact ( email: ' from@5.com ' ) ]
threadId: ' fake-thread-id '
body: ' Fake Message 2 '
subject: ' Re: Fake Subject '
date: new Date ( 1415814587 )
2015-03-11 02:02:46 +08:00
fakeMessages =
' fake-message-1 ' : fakeMessage1
2015-03-27 10:07:21 +08:00
' fake-message-3 ' : msgFromMe
2015-03-11 02:02:46 +08:00
' fake-message-2 ' : fakeMessage2
2015-05-29 09:13:46 +08:00
' fake-message-reply-to ' : msgWithReplyTo
2015-03-11 02:02:46 +08:00
2015-02-25 08:19:47 +08:00
spyOn ( DatabaseStore , ' find ' ) . andCallFake (klass, id) ->
2015-03-26 03:41:48 +08:00
query = new ModelQuery ( klass , { id } )
spyOn ( query , ' then ' ) . andCallFake (fn) ->
return fn ( fakeThread ) if klass is Thread
return fn ( fakeMessages [ id ] ) if klass is Message
return fn ( new Error ( ' Not Stubbed ' ) )
query
2015-02-25 08:19:47 +08:00
spyOn ( DatabaseStore , ' run ' ) . andCallFake (query) ->
return Promise . resolve ( fakeMessage2 ) if query . _klass is Message
return Promise . reject ( new Error ( ' Not Stubbed ' ) )
2015-06-16 09:29:59 +08:00
spyOn ( DatabaseStore , ' persistModel ' ) . andCallFake -> Promise . resolve ( )
2015-06-06 02:38:30 +08:00
spyOn ( DatabaseStore , ' bindToLocalId ' )
2015-02-25 08:19:47 +08:00
2015-06-16 09:29:59 +08:00
afterEach ->
# Have to cleanup the DraftStoreProxy objects or we'll get a memory
# leak error
for id , session of DraftStore . _draftSessions
DraftStore . _doneWithSession ( session )
2015-02-25 08:19:47 +08:00
describe " onComposeReply " , ->
beforeEach ->
runs ->
DraftStore . _onComposeReply ( { threadId: fakeThread . id , messageId: fakeMessage1 . id } )
waitsFor ->
DatabaseStore . persistModel . callCount > 0
runs ->
@model = DatabaseStore . persistModel . mostRecentCall . args [ 0 ]
it " should include quoted text " , ->
expect ( @ model . body . indexOf ( ' blockquote ' ) > 0 ) . toBe ( true )
expect ( @ model . body . indexOf ( fakeMessage1 . body ) > 0 ) . toBe ( true )
it " should address the message to the previous message ' s sender " , ->
expect ( @ model . to ) . toEqual ( fakeMessage1 . from )
it " should set the replyToMessageId to the previous message ' s ids " , ->
expect ( @ model . replyToMessageId ) . toEqual ( fakeMessage1 . id )
2015-05-29 09:13:46 +08:00
describe " onComposeReply " , ->
describe " when the message provided as context has one or more ' ReplyTo ' recipients " , ->
it " addresses the draft to all of the message ' s ' ReplyTo ' recipients " , ->
runs ->
DraftStore . _onComposeReply ( { threadId: fakeThread . id , messageId: msgWithReplyTo . id } )
waitsFor ->
DatabaseStore . persistModel . callCount > 0
runs ->
@model = DatabaseStore . persistModel . mostRecentCall . args [ 0 ]
expect ( @ model . to ) . toEqual ( msgWithReplyTo . replyTo )
expect ( @ model . cc . length ) . toBe 0
expect ( @ model . bcc . length ) . toBe 0
2015-03-27 10:07:21 +08:00
2015-05-29 09:13:46 +08:00
describe " onComposeReply " , ->
describe " when the message provided as context was sent by the current user " , ->
it " addresses the draft to all of the last messages ' s ' To ' recipients " , ->
runs ->
DraftStore . _onComposeReply ( { threadId: fakeThread . id , messageId: msgFromMe . id } )
waitsFor ->
DatabaseStore . persistModel . callCount > 0
runs ->
@model = DatabaseStore . persistModel . mostRecentCall . args [ 0 ]
expect ( @ model . to ) . toEqual ( msgFromMe . to )
expect ( @ model . cc . length ) . toBe 0
expect ( @ model . bcc . length ) . toBe 0
2015-03-27 10:07:21 +08:00
2015-02-25 08:19:47 +08:00
describe " onComposeReplyAll " , ->
beforeEach ->
runs ->
DraftStore . _onComposeReplyAll ( { threadId: fakeThread . id , messageId: fakeMessage1 . id } )
waitsFor ->
DatabaseStore . persistModel . callCount > 0
runs ->
@model = DatabaseStore . persistModel . mostRecentCall . args [ 0 ]
it " should include quoted text " , ->
expect ( @ model . body . indexOf ( ' blockquote ' ) > 0 ) . toBe ( true )
expect ( @ model . body . indexOf ( fakeMessage1 . body ) > 0 ) . toBe ( true )
it " should address the message to the previous message ' s sender " , ->
expect ( @ model . to ) . toEqual ( fakeMessage1 . from )
it " should cc everyone who was on the previous message in to or cc " , ->
ccEmails = @ model . cc . map (cc) -> cc . email
2015-05-15 08:08:30 +08:00
expect ( ccEmails . sort ( ) ) . toEqual ( [ ' ben@nylas.com ' , ' evan@nylas.com ' , ' mg@nylas.com ' ] )
2015-02-25 08:19:47 +08:00
it " should not include people who were bcc ' d on the previous message " , ->
expect ( @ model . bcc ) . toEqual ( [ ] )
expect ( @ model . cc . indexOf ( fakeMessage1 . bcc [ 0 ] ) ) . toEqual ( - 1 )
it " should not include you when you were cc ' d on the previous message " , ->
ccEmails = @ model . cc . map (cc) -> cc . email
2015-03-27 10:07:21 +08:00
expect ( ccEmails . indexOf ( NamespaceStore . current ( ) . me ( ) . email ) ) . toEqual ( - 1 )
2015-02-25 08:19:47 +08:00
it " should set the replyToMessageId to the previous message ' s ids " , ->
expect ( @ model . replyToMessageId ) . toEqual ( fakeMessage1 . id )
2015-05-29 09:13:46 +08:00
describe " onComposeReplyAll " , ->
describe " when the message provided as context has one or more ' ReplyTo ' recipients " , ->
beforeEach ->
runs ->
DraftStore . _onComposeReply ( { threadId: fakeThread . id , messageId: msgWithReplyTo . id } )
waitsFor ->
DatabaseStore . persistModel . callCount > 0
runs ->
@model = DatabaseStore . persistModel . mostRecentCall . args [ 0 ]
it " addresses the draft to all of the message ' s ' ReplyTo ' recipients " , ->
expect ( @ model . to ) . toEqual ( msgWithReplyTo . replyTo )
it " should not include the message ' s ' From ' recipient in any field " , ->
all = [ ] . concat ( @ model . to , @ model . cc , @ model . bcc )
match = _ . find all , (c) -> c . email is msgWithReplyTo . from [ 0 ] . email
expect ( match ) . toEqual ( undefined )
describe " onComposeReplyAll " , ->
describe " when the message provided as context was sent by the current user " , ->
it " addresses the draft to all of the last messages ' s recipients " , ->
runs ->
DraftStore . _onComposeReplyAll ( { threadId: fakeThread . id , messageId: msgFromMe . id } )
waitsFor ->
DatabaseStore . persistModel . callCount > 0
runs ->
@model = DatabaseStore . persistModel . mostRecentCall . args [ 0 ]
expect ( @ model . to ) . toEqual ( msgFromMe . to )
expect ( @ model . cc ) . toEqual ( msgFromMe . cc )
expect ( @ model . bcc . length ) . toBe 0
2015-02-25 08:19:47 +08:00
describe " onComposeForward " , ->
beforeEach ->
runs ->
DraftStore . _onComposeForward ( { threadId: fakeThread . id , messageId: fakeMessage1 . id } )
waitsFor ->
DatabaseStore . persistModel . callCount > 0
runs ->
@model = DatabaseStore . persistModel . mostRecentCall . args [ 0 ]
it " should include quoted text " , ->
expect ( @ model . body . indexOf ( ' blockquote ' ) > 0 ) . toBe ( true )
expect ( @ model . body . indexOf ( fakeMessage1 . body ) > 0 ) . toBe ( true )
it " should not address the message to anyone " , ->
expect ( @ model . to ) . toEqual ( [ ] )
expect ( @ model . cc ) . toEqual ( [ ] )
expect ( @ model . bcc ) . toEqual ( [ ] )
it " should not set the replyToMessageId " , ->
expect ( @ model . replyToMessageId ) . toEqual ( undefined )
describe " _newMessageWithContext " , ->
beforeEach ->
# A helper method that makes it easy to test _newMessageWithContext, which
# is asynchronous and whose output is a model persisted to the database.
@_callNewMessageWithContext = (context, attributesCallback, modelCallback) ->
runs ->
DraftStore . _newMessageWithContext ( context , attributesCallback )
waitsFor ->
DatabaseStore . persistModel . callCount > 0
runs ->
model = DatabaseStore . persistModel . mostRecentCall . args [ 0 ]
modelCallback ( model ) if modelCallback
it " should create a new message " , ->
@ _callNewMessageWithContext { threadId: fakeThread . id }
, (thread, message) ->
{ }
, (model) ->
expect ( model . constructor ) . toBe ( Message )
2015-06-06 02:38:30 +08:00
it " should assign and save a local Id for the new message " , ->
@ _callNewMessageWithContext { threadId: fakeThread . id }
, (thread, message) ->
{ }
, (model) ->
expect ( DatabaseStore . bindToLocalId ) . toHaveBeenCalled ( )
it " should setup a draft session for the draftLocalId, so that a subsequent request for the session ' s draft resolves immediately. " , ->
@ _callNewMessageWithContext { threadId: fakeThread . id }
, (thread, message) ->
{ }
, (model) ->
[ draft , localId ] = DatabaseStore . bindToLocalId . mostRecentCall . args
session = DraftStore . sessionForLocalId ( localId ) . value ( )
expect ( session . draft ( ) ) . toBe ( draft )
2015-03-12 06:55:04 +08:00
it " should set the subject of the new message automatically " , ->
@ _callNewMessageWithContext { threadId: fakeThread . id }
, (thread, message) ->
{ }
, (model) ->
expect ( model . subject ) . toEqual ( " Re: Fake Subject " )
2015-02-25 08:19:47 +08:00
it " should apply attributes provided by the attributesCallback " , ->
@ _callNewMessageWithContext { threadId: fakeThread . id }
, (thread, message) ->
subject: " Fwd: Fake subject "
to: [ new Contact ( email: ' weird@example.com ' ) ]
, (model) ->
expect ( model . subject ) . toEqual ( " Fwd: Fake subject " )
2015-06-06 02:38:30 +08:00
describe " context " , ->
it " should accept `thread` or look up a thread when given `threadId` " , ->
@ _callNewMessageWithContext { thread: fakeThread }
, (thread, message) ->
expect ( thread ) . toBe ( fakeThread )
expect ( DatabaseStore . find ) . not . toHaveBeenCalled ( )
{ }
, (model) ->
@ _callNewMessageWithContext { threadId: fakeThread . id }
, (thread, message) ->
expect ( thread ) . toBe ( fakeThread )
expect ( DatabaseStore . find ) . toHaveBeenCalled ( )
{ }
, (model) ->
it " should accept `message` or look up a message when given `messageId` " , ->
@ _callNewMessageWithContext { thread: fakeThread , message: fakeMessage1 }
, (thread, message) ->
expect ( message ) . toBe ( fakeMessage1 )
expect ( DatabaseStore . find ) . not . toHaveBeenCalled ( )
{ }
, (model) ->
@ _callNewMessageWithContext { thread: fakeThread , messageId: fakeMessage1 . id }
, (thread, message) ->
expect ( message ) . toBe ( fakeMessage1 )
expect ( DatabaseStore . find ) . toHaveBeenCalled ( )
{ }
, (model) ->
2015-03-12 06:55:04 +08:00
describe " when a reply-to message is provided by the attributesCallback " , ->
2015-02-25 08:19:47 +08:00
it " should include quoted text in the new message " , ->
@ _callNewMessageWithContext { threadId: fakeThread . id }
, (thread, message) ->
2015-03-11 09:07:40 +08:00
replyToMessage: fakeMessage1
2015-02-25 08:19:47 +08:00
, (model) ->
expect ( model . body . indexOf ( ' gmail_quote ' ) > 0 ) . toBe ( true )
expect ( model . body . indexOf ( ' Fake Message 1 ' ) > 0 ) . toBe ( true )
it " should include the `On ... wrote:` line " , ->
@ _callNewMessageWithContext { threadId: fakeThread . id }
, (thread, message) ->
2015-03-11 09:07:40 +08:00
replyToMessage: fakeMessage1
2015-02-25 08:19:47 +08:00
, (model) ->
2015-03-25 05:03:30 +08:00
expect ( model . body . search ( /On .+, at .+, Customer <customer@example.com> wrote/ ) > 0 ) . toBe ( true )
2015-02-25 08:19:47 +08:00
2015-03-12 06:55:04 +08:00
it " should make the subject the subject of the message, not the thread " , ->
fakeMessage1.subject = " OLD SUBJECT "
@ _callNewMessageWithContext { threadId: fakeThread . id }
, (thread, message) ->
replyToMessage: fakeMessage1
, (model) ->
expect ( model . subject ) . toEqual ( " Re: OLD SUBJECT " )
it " should change the subject from Fwd: back to Re: if necessary " , ->
fakeMessage1.subject = " Fwd: This is my DRAFT "
@ _callNewMessageWithContext { threadId: fakeThread . id }
, (thread, message) ->
replyToMessage: fakeMessage1
, (model) ->
expect ( model . subject ) . toEqual ( " Re: This is my DRAFT " )
2015-02-25 08:19:47 +08:00
it " should only include the sender ' s name if it was available " , ->
@ _callNewMessageWithContext { threadId: fakeThread . id }
, (thread, message) ->
2015-03-11 09:07:40 +08:00
replyToMessage: fakeMessage2
2015-02-25 08:19:47 +08:00
, (model) ->
2015-05-15 08:08:30 +08:00
expect ( model . body . search ( /On .+, at .+, ben@nylas.com wrote:/ ) > 0 ) . toBe ( true )
2015-02-25 08:19:47 +08:00
2015-03-12 06:55:04 +08:00
describe " when a forward message is provided by the attributesCallback " , ->
it " should include quoted text in the new message " , ->
@ _callNewMessageWithContext { threadId: fakeThread . id }
, (thread, message) ->
forwardMessage: fakeMessage1
, (model) ->
expect ( model . body . indexOf ( ' gmail_quote ' ) > 0 ) . toBe ( true )
expect ( model . body . indexOf ( ' Fake Message 1 ' ) > 0 ) . toBe ( true )
it " should include the `Begin forwarded message:` line " , ->
@ _callNewMessageWithContext { threadId: fakeThread . id }
, (thread, message) ->
forwardMessage: fakeMessage1
, (model) ->
expect ( model . body . indexOf ( ' Begin forwarded message ' ) > 0 ) . toBe ( true )
it " should make the subject the subject of the message, not the thread " , ->
fakeMessage1.subject = " OLD SUBJECT "
@ _callNewMessageWithContext { threadId: fakeThread . id }
, (thread, message) ->
forwardMessage: fakeMessage1
, (model) ->
expect ( model . subject ) . toEqual ( " Fwd: OLD SUBJECT " )
it " should change the subject from Re: back to Fwd: if necessary " , ->
fakeMessage1.subject = " Re: This is my DRAFT "
@ _callNewMessageWithContext { threadId: fakeThread . id }
, (thread, message) ->
forwardMessage: fakeMessage1
, (model) ->
expect ( model . subject ) . toEqual ( " Fwd: This is my DRAFT " )
it " should print the headers of the original message " , ->
@ _callNewMessageWithContext { threadId: fakeThread . id }
, (thread, message) ->
forwardMessage: fakeMessage2
, (model) ->
2015-05-15 08:08:30 +08:00
expect ( model . body . indexOf ( ' From: ben@nylas.com ' ) > 0 ) . toBe ( true )
2015-03-12 06:55:04 +08:00
expect ( model . body . indexOf ( ' Subject: Re: Fake Subject ' ) > 0 ) . toBe ( true )
expect ( model . body . indexOf ( ' To: customer@example.com ' ) > 0 ) . toBe ( true )
2015-02-25 08:19:47 +08:00
describe " attributesCallback " , ->
describe " when a threadId is provided " , ->
it " should receive the thread " , ->
@ _callNewMessageWithContext { threadId: fakeThread . id }
, (thread, message) ->
expect ( thread ) . toEqual ( fakeThread )
{ }
it " should receive the last message in the fakeThread " , ->
@ _callNewMessageWithContext { threadId: fakeThread . id }
, (thread, message) ->
expect ( message ) . toEqual ( fakeMessage2 )
{ }
describe " when a threadId and messageId are provided " , ->
it " should receive the thread " , ->
@ _callNewMessageWithContext { threadId: fakeThread . id , messageId: fakeMessage1 . id }
, (thread, message) ->
expect ( thread ) . toEqual ( fakeThread )
{ }
it " should receive the desired message in the thread " , ->
@ _callNewMessageWithContext { threadId: fakeThread . id , messageId: fakeMessage1 . id }
, (thread, message) ->
expect ( message ) . toEqual ( fakeMessage1 )
2015-03-10 02:17:22 +08:00
{ }
2015-05-20 03:07:08 +08:00
2015-03-24 07:33:28 +08:00
describe " onDestroyDraft " , ->
beforeEach ->
@draftReset = jasmine . createSpy ( ' draft reset ' )
2015-05-20 03:07:08 +08:00
@session =
2015-03-24 07:33:28 +08:00
draft: ->
pristine: false
changes:
commit: -> Promise . resolve ( )
reset: @ draftReset
cleanup: ->
2015-05-20 03:07:08 +08:00
DraftStore._draftSessions = { " abc " : @ session }
spyOn ( Actions , ' queueTask ' )
2015-03-24 07:33:28 +08:00
it " should reset the draft session, ensuring no more saves are made " , ->
DraftStore . _onDestroyDraft ( ' abc ' )
expect ( @ draftReset ) . toHaveBeenCalled ( )
2015-05-20 03:07:08 +08:00
it " should throw if the draft session is not in the window " , ->
2015-03-24 07:33:28 +08:00
expect ->
DraftStore . _onDestroyDraft ( ' other ' )
2015-05-20 03:07:08 +08:00
. toThrow ( )
2015-03-24 07:33:28 +08:00
expect ( @ draftReset ) . not . toHaveBeenCalled ( )
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 " , ->
2015-05-20 03:07:08 +08:00
spyOn ( DraftStore , ' _doneWithSession ' )
DraftStore . _onDestroyDraft ( ' abc ' )
expect ( DraftStore . _doneWithSession ) . toHaveBeenCalledWith ( @ session )
it " should close the window if it ' s a popout " , ->
spyOn ( atom , " close " )
spyOn ( DraftStore , " _isPopout " ) . andReturn true
2015-03-24 07:33:28 +08:00
DraftStore . _onDestroyDraft ( ' abc ' )
2015-05-20 03:07:08 +08:00
expect ( atom . close ) . toHaveBeenCalled ( )
it " should NOT close the window if isn ' t a popout " , ->
spyOn ( atom , " close " )
spyOn ( DraftStore , " _isPopout " ) . andReturn false
DraftStore . _onDestroyDraft ( ' abc ' )
expect ( atom . close ) . not . toHaveBeenCalled ( )
2015-03-24 07:33:28 +08:00
describe " before unloading " , ->
it " should destroy pristine drafts " , ->
DraftStore._draftSessions = { " abc " : {
changes: { }
draft: ->
pristine: true
} }
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 ->
@resolve = null
DraftStore._draftSessions = { " abc " : {
changes:
commit: => new Promise (resolve, reject) => @resolve = resolve
draft: ->
pristine: false
} }
it " should return false and call window.close itself " , ->
2015-05-16 04:44:01 +08:00
spyOn ( atom , ' close ' )
2015-03-24 07:33:28 +08:00
expect ( DraftStore . _onBeforeUnload ( ) ) . toBe ( false )
runs ->
@ resolve ( )
waitsFor ->
2015-05-16 04:44:01 +08:00
atom . close . callCount > 0
2015-03-24 07:33:28 +08:00
runs ->
2015-05-16 04:44:01 +08:00
expect ( atom . close ) . toHaveBeenCalled ( )
2015-03-24 07:33:28 +08:00
describe " when no drafts return unresolved commit promises " , ->
beforeEach ->
DraftStore._draftSessions = { " abc " : {
changes:
commit: -> Promise . resolve ( )
draft: ->
pristine: false
} }
it " should return true and allow the window to close " , ->
expect ( DraftStore . _onBeforeUnload ( ) ) . toBe ( true )
2015-03-13 05:48:56 +08:00
describe " sending a draft " , ->
draftLocalId = " local-123 "
beforeEach ->
DraftStore._draftSessions = { }
2015-05-16 01:45:18 +08:00
proxy =
prepare: -> Promise . resolve ( proxy )
2015-03-24 07:33:28 +08:00
cleanup: ->
draft: -> { }
2015-03-13 05:48:56 +08:00
changes:
commit: -> Promise . resolve ( )
2015-05-16 01:45:18 +08:00
DraftStore . _draftSessions [ draftLocalId ] = proxy
2015-05-20 03:07:08 +08:00
spyOn ( DraftStore , " _doneWithSession " ) . andCallThrough ( )
2015-03-13 05:48:56 +08:00
spyOn ( DraftStore , " trigger " )
2015-05-20 03:07:08 +08:00
TaskQueue._queue = [ ]
2015-03-13 05:48:56 +08:00
it " sets the sending state when sending " , ->
2015-05-20 03:07:08 +08:00
spyOn ( atom , " isMainWindow " ) . andReturn true
spyOn ( TaskQueue , " _update " )
spyOn ( Actions , " queueTask " ) . andCallThrough ( )
runs ->
DraftStore . _onSendDraft ( draftLocalId )
waitsFor ->
Actions . queueTask . calls . length > 0
runs ->
expect ( DraftStore . isSendingDraft ( draftLocalId ) ) . toBe true
expect ( DraftStore . trigger ) . toHaveBeenCalled ( )
2015-03-13 05:48:56 +08:00
it " returns false if the draft hasn ' t been seen " , ->
2015-05-20 03:07:08 +08:00
spyOn ( atom , " isMainWindow " ) . andReturn true
expect ( DraftStore . isSendingDraft ( draftLocalId ) ) . toBe false
2015-03-13 05:48:56 +08:00
it " closes the window if it ' s a popout " , ->
2015-04-25 00:36:19 +08:00
spyOn ( atom , " getWindowType " ) . andReturn " composer "
spyOn ( atom , " isMainWindow " ) . andReturn false
2015-03-13 05:48:56 +08:00
spyOn ( atom , " close " )
2015-05-02 00:58:51 +08:00
runs ->
DraftStore . _onSendDraft ( draftLocalId )
waitsFor " Atom to close " , ->
atom . close . calls . length > 0
2015-03-13 05:48:56 +08:00
it " doesn ' t close the window if it ' s inline " , ->
2015-04-25 00:36:19 +08:00
spyOn ( atom , " getWindowType " ) . andReturn " other "
spyOn ( atom , " isMainWindow " ) . andReturn false
2015-03-13 05:48:56 +08:00
spyOn ( atom , " close " )
2015-05-20 03:07:08 +08:00
spyOn ( DraftStore , " _isPopout " ) . andCallThrough ( )
runs ->
DraftStore . _onSendDraft ( draftLocalId )
waitsFor ->
DraftStore . _isPopout . calls . length > 0
runs ->
expect ( atom . close ) . not . toHaveBeenCalled ( )
2015-03-13 05:48:56 +08:00
it " queues a SendDraftTask " , ->
spyOn ( Actions , " queueTask " )
2015-05-20 03:07:08 +08:00
runs ->
DraftStore . _onSendDraft ( draftLocalId )
waitsFor ->
DraftStore . _doneWithSession . calls . length > 0
runs ->
expect ( Actions . queueTask ) . toHaveBeenCalled ( )
task = Actions . queueTask . calls [ 0 ] . args [ 0 ]
expect ( task instanceof SendDraftTask ) . toBe true
expect ( task . draftLocalId ) . toBe draftLocalId
expect ( task . fromPopout ) . toBe false
2015-03-13 05:48:56 +08:00
it " queues a SendDraftTask with popout info " , ->
2015-04-25 00:36:19 +08:00
spyOn ( atom , " getWindowType " ) . andReturn " composer "
spyOn ( atom , " isMainWindow " ) . andReturn false
2015-03-13 05:48:56 +08:00
spyOn ( atom , " close " )
spyOn ( Actions , " queueTask " )
2015-05-20 03:07:08 +08:00
runs ->
DraftStore . _onSendDraft ( draftLocalId )
waitsFor ->
DraftStore . _doneWithSession . calls . length > 0
runs ->
expect ( Actions . queueTask ) . toHaveBeenCalled ( )
task = Actions . queueTask . calls [ 0 ] . args [ 0 ]
expect ( task . fromPopout ) . toBe true
2015-03-24 07:33:28 +08:00
2015-05-20 03:07:08 +08:00
describe " session cleanup " , ->
beforeEach ->
@draftCleanup = jasmine . createSpy ( ' draft cleanup ' )
@session =
draftLocalId: " abc "
draft: ->
pristine: false
changes:
commit: -> Promise . resolve ( )
reset: ->
cleanup: @ draftCleanup
DraftStore._draftSessions = { " abc " : @ session }
DraftStore . _doneWithSession ( @ session )
2015-03-24 07:33:28 +08:00
2015-05-20 03:07:08 +08:00
it " removes from the list of draftSessions " , ->
expect ( DraftStore . _draftSessions [ " abc " ] ) . toBeUndefined ( )
2015-03-24 07:33:28 +08:00
2015-05-20 03:07:08 +08:00
it " Calls cleanup on the session " , ->
expect ( @ draftCleanup ) . toHaveBeenCalled
2015-06-19 02:58:07 +08:00
describe " mailto handling " , ->
it " should correctly instantiate drafts for a wide range of mailto URLs " , ->
received = null
spyOn ( DatabaseStore , ' persistModel ' ) . andCallFake (draft) ->
received = draft
Promise . resolve ( )
links = [
' mailto: '
' mailto://bengotow@gmail.com '
' mailto:bengotow@gmail.com '
' mailto:?subject=%1z2a ' , # fails uriDecode
' mailto:?subject=%52z2a ' , # passes uriDecode
' mailto:?subject=Martha Stewart ' ,
' mailto:?subject=Martha Stewart&cc=cc@nylas.com ' ,
' mailto:bengotow@gmail.com?subject=Martha%20Stewart&cc=cc@nylas.com&bcc=bcc@nylas.com ' ,
' mailto:bengotow@gmail.com?subject=Martha%20Stewart&cc=cc@nylas.com&bcc=Ben <bcc@nylas.com> ' ,
' mailto:Ben Gotow <bengotow@gmail.com>,Shawn <shawn@nylas.com>?subject=Yes this is really valid '
' mailto:Ben%20Gotow%20<bengotow@gmail.com>,Shawn%20<shawn@nylas.com>?subject=Yes%20this%20is%20really%20valid '
' mailto:Reply <d+AORGpRdj0KXKUPBE1LoI0a30F10Ahj3wu3olS-aDk5_7K5Wu6WqqqG8t1HxxhlZ4KEEw3WmrSdtobgUq57SkwsYAH6tG57IrNqcQR0K6XaqLM2nGNZ22D2k@docs.google.com>?subject=Nilas%20Message%20to%20Customers ' ,
]
expected = [
new Message ( ) ,
new Message (
to: [ new Contact ( name: ' bengotow@gmail.com ' , email: ' bengotow@gmail.com ' ) ]
) ,
new Message (
to: [ new Contact ( name: ' bengotow@gmail.com ' , email: ' bengotow@gmail.com ' ) ]
) ,
new Message (
subject: ' %1z2a '
) ,
new Message (
subject: ' Rz2a '
) ,
new Message (
subject: ' Martha Stewart '
) ,
new Message (
cc: [ new Contact ( name: ' cc@nylas.com ' , email: ' cc@nylas.com ' ) ] ,
subject: ' Martha Stewart '
) ,
new Message (
to: [ new Contact ( name: ' bengotow@gmail.com ' , email: ' bengotow@gmail.com ' ) ] ,
cc: [ new Contact ( name: ' cc@nylas.com ' , email: ' cc@nylas.com ' ) ] ,
bcc: [ new Contact ( name: ' bcc@nylas.com ' , email: ' bcc@nylas.com ' ) ] ,
subject: ' Martha Stewart '
) ,
new Message (
to: [ new Contact ( name: ' bengotow@gmail.com ' , email: ' bengotow@gmail.com ' ) ] ,
cc: [ new Contact ( name: ' cc@nylas.com ' , email: ' cc@nylas.com ' ) ] ,
bcc: [ new Contact ( name: ' Ben ' , email: ' bcc@nylas.com ' ) ] ,
subject: ' Martha Stewart '
) ,
new Message (
to: [ new Contact ( name: ' Ben Gotow ' , email: ' bengotow@gmail.com ' ) , new Contact ( name: ' Shawn ' , email: ' shawn@nylas.com ' ) ] ,
subject: ' Yes this is really valid '
) ,
new Message (
to: [ new Contact ( name: ' Ben Gotow ' , email: ' bengotow@gmail.com ' ) , new Contact ( name: ' Shawn ' , email: ' shawn@nylas.com ' ) ] ,
subject: ' Yes this is really valid '
) ,
new Message (
to: [ new Contact ( name: ' Reply ' , email: ' d+AORGpRdj0KXKUPBE1LoI0a30F10Ahj3wu3olS-aDk5_7K5Wu6WqqqG8t1HxxhlZ4KEEw3WmrSdtobgUq57SkwsYAH6tG57IrNqcQR0K6XaqLM2nGNZ22D2k@docs.google.com ' ) ] ,
subject: ' Nilas Message to Customers '
)
]
for link , idx in links
DraftStore . _onHandleMailtoLink ( link )
expectedDraft = expected [ idx ]
expect ( received [ ' subject ' ] ) . toEqual ( expectedDraft [ ' subject ' ] )
for attr in [ ' to ' , ' cc ' , ' bcc ' , ' subject ' ]
for contact , jdx in received [ attr ]
expectedContact = expectedDraft [ attr ] [ jdx ]
expect ( contact . email ) . toEqual ( expectedContact . email )
expect ( contact . name ) . toEqual ( expectedContact . name )