fix(draft): fix draft not sending if it wasn't saved first

This commit is contained in:
Evan Morikawa 2015-02-20 17:50:39 -08:00
parent 344524d259
commit 742d38c4ef
2 changed files with 36 additions and 1 deletions

View file

@ -138,3 +138,37 @@ describe "SendDraftTask", ->
expect(atom.inbox.makeRequest.calls.length).toBe(1)
options = atom.inbox.makeRequest.mostRecentCall.args[0]
expect(options.returnsModel).toBe(true)
describe "failing performRemote", ->
beforeEach ->
@draft = new Message
version: '1'
id: '1233123AEDF1'
namespaceId: 'A12ADE'
subject: 'New Draft'
draft: true
to:
name: 'Dummy'
email: 'dummy@inboxapp.com'
@task = new SendDraftTask(@draft)
it "throws an error if the draft can't be found", ->
spyOn(DatabaseStore, 'findByLocalId').andCallFake (klass, localId) ->
Promise.resolve()
waitsForPromise =>
@task.performRemote().catch (error) ->
expect(error.message).toBeDefined()
it "throws an error if the draft isn't saved", ->
spyOn(DatabaseStore, 'findByLocalId').andCallFake (klass, localId) ->
Promise.resolve(isSaved: false)
waitsForPromise =>
@task.performRemote().catch (error) ->
expect(error.message).toBeDefined()
it "throws an error if the DB store has issues", ->
spyOn(DatabaseStore, 'findByLocalId').andCallFake (klass, localId) ->
Promise.reject("DB error")
waitsForPromise =>
@task.performRemote().catch (error) ->
expect(error).toBe "DB error"

View file

@ -31,7 +31,7 @@ class SendDraftTask extends Task
# recent draft version
DatabaseStore.findByLocalId(Message, @draftLocalId).then (draft) ->
# The draft may have been deleted by another task. Nothing we can do.
return resolve() unless draft
return reject(new Error("We couldn't find the saved draft. Please try again in a couple seconds")) unless draft
return reject(new Error("Cannot send draft that is not saved!")) unless draft.isSaved()
atom.inbox.makeRequest
@ -46,6 +46,7 @@ class SendDraftTask extends Task
Actions.postNotification({message: "Sent!", type: 'success'})
DatabaseStore.unpersistModel(draft).then(resolve)
error: reject
.catch(reject)
onAPIError: ->
msg = "Our server is having problems. Your messages has NOT been sent"