From e5efa80216b25a76eacc1650ebaaabde5692c4a4 Mon Sep 17 00:00:00 2001 From: Evan Morikawa Date: Fri, 8 Jan 2016 10:57:48 -0800 Subject: [PATCH] test(draft): new tests for promise chain in send draft --- spec/tasks/send-draft-spec.coffee | 48 +++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/spec/tasks/send-draft-spec.coffee b/spec/tasks/send-draft-spec.coffee index ca4649aa2..7fc1587ea 100644 --- a/spec/tasks/send-draft-spec.coffee +++ b/spec/tasks/send-draft-spec.coffee @@ -314,6 +314,54 @@ describe "SendDraftTask", -> expect(@task._notifyUserOfError).toHaveBeenCalled() expect(@task._notifyUserOfError.calls.length).toBe 1 + describe "checking the promise chain halts on errors", -> + beforeEach -> + spyOn(@task,"_makeSendRequest").andCallThrough() + spyOn(@task,"_saveNewMessage").andCallThrough() + spyOn(@task,"_deleteRemoteDraft").andCallThrough() + spyOn(@task,"_notifySuccess").andCallThrough() + spyOn(@task,"_onError").andCallThrough() + + @expectBlockedChain = => + expect(@task._makeSendRequest).toHaveBeenCalled() + expect(@task._saveNewMessage).not.toHaveBeenCalled() + expect(@task._deleteRemoteDraft).not.toHaveBeenCalled() + expect(@task._notifySuccess).not.toHaveBeenCalled() + expect(@task._onError).toHaveBeenCalled() + + it "halts on 500s", -> + thrownError = new APIError(statusCode: 500, body: "err") + spyOn(NylasAPI, 'makeRequest').andCallFake (options) => + Promise.reject(thrownError) + waitsForPromise => @task.performRemote().then (status) => + @expectBlockedChain() + + it "halts on 400s", -> + thrownError = new APIError(statusCode: 400, body: "err") + spyOn(NylasAPI, 'makeRequest').andCallFake (options) => + Promise.reject(thrownError) + waitsForPromise => @task.performRemote().then (status) => + @expectBlockedChain() + + it "halts on other errors", -> + thrownError = new Error("oh no") + spyOn(NylasAPI, 'makeRequest').andCallFake (options) => + Promise.reject(thrownError) + waitsForPromise => @task.performRemote().then (status) => + @expectBlockedChain() + + it "dosn't halt on success", -> + spyOn(NylasAPI, 'makeRequest').andCallFake (options) => + options.success?(@response) + Promise.resolve(@response) + waitsForPromise => @task.performRemote().then (status) => + expect(@task._makeSendRequest).toHaveBeenCalled() + expect(@task._saveNewMessage).toHaveBeenCalled() + expect(@task._deleteRemoteDraft).toHaveBeenCalled() + expect(@task._notifySuccess).toHaveBeenCalled() + expect(@task._onError).not.toHaveBeenCalled() + + describe "with a new draft", -> beforeEach -> @draft = new Message