From 3dbdd5cfd67a56a5188c67ad2d7d0e36cc61223d Mon Sep 17 00:00:00 2001 From: Evan Morikawa Date: Tue, 22 Dec 2015 10:36:25 -0800 Subject: [PATCH] fix(spec): fix send-draft spec to test for include --- spec/tasks/send-draft-spec.coffee | 30 +++++++++++++++++++++++------- src/flux/tasks/send-draft.coffee | 2 +- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/spec/tasks/send-draft-spec.coffee b/spec/tasks/send-draft-spec.coffee index db071242d..ca4649aa2 100644 --- a/spec/tasks/send-draft-spec.coffee +++ b/spec/tasks/send-draft-spec.coffee @@ -71,13 +71,20 @@ describe "SendDraftTask", -> serverId: "server-123" draft: true - spyOn(DatabaseStore, "findBy").andReturn Promise.resolve(draft) + calledBody = "ERROR: The body wasn't included!" + spyOn(DatabaseStore, "findBy").andCallFake -> + then: -> throw new Error("You must include the body!") + include: (body) -> + calledBody = body + return Promise.resolve(draft) + task = new SendDraftTask('local-123') waitsForPromise => task.performLocal().then => expect(task.backupDraft).toBeDefined() expect(task.backupDraft.clientId).toBe "local-123" expect(task.backupDraft.serverId).toBe "server-123" expect(task.backupDraft).not.toBe draft # It's a clone + expect(calledBody).toBe Message.attributes.body describe "performRemote", -> beforeEach -> @@ -118,8 +125,7 @@ describe "SendDraftTask", -> it "throws a `NotFoundError` if the model is blank", -> spyOn(@task, "_notifyUserOfError") spyOn(@task, "_permanentError").andCallThrough() - jasmine.unspy(DatabaseStore, "findBy") - spyOn(DatabaseStore, 'findBy').andReturn Promise.resolve(null) + @draftResolver = -> Promise.resolve(null) waitsForPromise => @task.performRemote().then => expect(DBt.persistModel.callCount).toBe 1 expect(DBt.persistModel).toHaveBeenCalledWith(@backupDraft) @@ -128,8 +134,7 @@ describe "SendDraftTask", -> it "throws a `NotFoundError` if findBy fails", -> spyOn(@task, "_notifyUserOfError") spyOn(@task, "_permanentError").andCallThrough() - jasmine.unspy(DatabaseStore, "findBy") - spyOn(DatabaseStore, 'findBy').andReturn Promise.reject(new Error("Problem")) + @draftResolver = -> Promise.reject(new Error("Test Problem")) waitsForPromise => @task.performRemote().then => expect(DBt.persistModel.callCount).toBe 1 expect(DBt.persistModel).toHaveBeenCalledWith(@backupDraft) @@ -321,7 +326,12 @@ describe "SendDraftTask", -> @task = new SendDraftTask(@draftClientId) @backupDraft = @draft.clone() @task.backupDraft = @backupDraft # Since performLocal doesn't run - spyOn(DatabaseStore, 'findBy').andReturn Promise.resolve(@draft) + @draftResolver = -> Promise.resolve(@draft) + @calledBody = "ERROR: The body wasn't included!" + spyOn(DatabaseStore, "findBy").andCallFake => + include: (body) => + @calledBody = body + return @draftResolver() it "can complete a full performRemote", -> waitsForPromise => @task.performRemote().then (status) -> @@ -367,7 +377,13 @@ describe "SendDraftTask", -> @task = new SendDraftTask(@draftClientId) @backupDraft = @draft.clone() @task.backupDraft = @backupDraft # Since performLocal doesn't run - spyOn(DatabaseStore, 'findBy').andReturn Promise.resolve(@draft) + @draftResolver = -> Promise.resolve(@draft) + @calledBody = "ERROR: The body wasn't included!" + spyOn(DatabaseStore, "findBy").andCallFake => + then: -> throw new Error("You must include the body!") + include: (body) => + @calledBody = body + return @draftResolver() it "can complete a full performRemote", -> waitsForPromise => @task.performRemote().then (status) -> diff --git a/src/flux/tasks/send-draft.coffee b/src/flux/tasks/send-draft.coffee index 13dd62061..b4ed42f76 100644 --- a/src/flux/tasks/send-draft.coffee +++ b/src/flux/tasks/send-draft.coffee @@ -45,7 +45,7 @@ class SendDraftTask extends Task # In this scenario, we don't want to send, but want to restore the # draft and notify the user to try again. In order to safely do this # we need to keep a backup to restore. - DatabaseStore.findBy(Message, clientId: @draftClientId).then (draftModel) => + DatabaseStore.findBy(Message, clientId: @draftClientId).include(Message.attributes.body).then (draftModel) => @backupDraft = draftModel.clone() performRemote: ->