Mailspring/spec-inbox/stores/draft-store-spec.coffee

267 lines
11 KiB
CoffeeScript
Raw Normal View History

Thread = require '../../src/flux/models/thread'
Message = require '../../src/flux/models/message'
Contact = require '../../src/flux/models/contact'
NamespaceStore = require '../../src/flux/stores/namespace-store.coffee'
DatabaseStore = require '../../src/flux/stores/database-store.coffee'
DraftStore = require '../../src/flux/stores/draft-store.coffee'
_ = require 'underscore-plus'
fakeThread = null
fakeMessage1 = null
fakeMessage2 = null
fakeMessages = null
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", ->
describe "creating drafts", ->
beforeEach ->
fakeThread = new Thread
id: 'fake-thread-id'
subject: 'Fake Subject'
fakeMessage1 = new Message
id: 'fake-message-1'
to: [new Contact(email: 'ben@nilas.com'), new Contact(email: 'evan@nilas.com')]
cc: [new Contact(email: 'mg@nilas.com'), new Contact(email: NamespaceStore.current().me().email)]
bcc: [new Contact(email: 'recruiting@nilas.com')]
from: [new Contact(email: 'customer@example.com', name: 'Customer')]
threadId: 'fake-thread-id'
body: 'Fake Message 1'
subject: 'Fake Subject'
date: new Date(1415814587)
fakeMessage2 = new Message
id: 'fake-message-2'
to: [new Contact(email: 'customer@example.com')]
from: [new Contact(email: 'ben@nilas.com')]
threadId: 'fake-thread-id'
body: 'Fake Message 2'
subject: 'Re: Fake Subject'
date: new Date(1415814587)
fakeMessages =
'fake-message-1': fakeMessage1
'fake-message-2': fakeMessage2
spyOn(DatabaseStore, 'find').andCallFake (klass, id) ->
return Promise.resolve(fakeThread) if klass is Thread
return Promise.resolve(fakeMessages[id]) if klass is Message
return Promise.reject(new Error('Not Stubbed'))
spyOn(DatabaseStore, 'run').andCallFake (query) ->
return Promise.resolve(fakeMessage2) if query._klass is Message
return Promise.reject(new Error('Not Stubbed'))
spyOn(DatabaseStore, 'persistModel')
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)
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
expect(ccEmails.sort()).toEqual([ 'ben@nilas.com', 'evan@nilas.com', 'mg@nilas.com'])
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
myEmail = NamespaceStore.current().me().email
expect(ccEmails.indexOf(myEmail)).toEqual(-1)
it "should set the replyToMessageId to the previous message's ids", ->
expect(@model.replyToMessageId).toEqual(fakeMessage1.id)
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)
it "should set the subject of the new message automatically", ->
@_callNewMessageWithContext {threadId: fakeThread.id}
, (thread, message) ->
{}
, (model) ->
expect(model.subject).toEqual("Re: Fake Subject")
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")
describe "when a reply-to message is provided by the attributesCallback", ->
it "should include quoted text in the new message", ->
@_callNewMessageWithContext {threadId: fakeThread.id}
, (thread, message) ->
replyToMessage: 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 `On ... wrote:` line", ->
@_callNewMessageWithContext {threadId: fakeThread.id}
, (thread, message) ->
replyToMessage: fakeMessage1
, (model) ->
expect(model.body.indexOf('On Jan 17 1970, at 1:16 am, Customer &lt;customer@example.com&gt; wrote') > 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) ->
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")
it "should only include the sender's name if it was available", ->
@_callNewMessageWithContext {threadId: fakeThread.id}
, (thread, message) ->
replyToMessage: fakeMessage2
, (model) ->
expect(model.body.indexOf('On Jan 17 1970, at 1:16 am, ben@nilas.com wrote:') > 0).toBe(true)
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) ->
expect(model.body.indexOf('From: ben@nilas.com') > 0).toBe(true)
expect(model.body.indexOf('Subject: Re: Fake Subject') > 0).toBe(true)
expect(model.body.indexOf('To: customer@example.com') > 0).toBe(true)
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)
{}