diff --git a/spec-nylas/tasks/send-draft-spec.coffee b/spec-nylas/tasks/send-draft-spec.coffee index 180ab0e37..e9702d148 100644 --- a/spec-nylas/tasks/send-draft-spec.coffee +++ b/spec-nylas/tasks/send-draft-spec.coffee @@ -72,12 +72,12 @@ describe "SendDraftTask", -> name: 'Dummy' email: 'dummy@nylas.com' @task = new SendDraftTask(@draftClientId) + + spyOn(NylasAPI, 'makeRequest').andCallFake (options) => options.success?(@draft.toJSON()) Promise.resolve(@draft.toJSON()) - spyOn(DatabaseStore, 'findBy').andCallFake (klass, id) => - Promise.resolve(@draft) - spyOn(DatabaseStore, 'find').andCallFake (klass, id) => + spyOn(DatabaseStore, 'run').andCallFake (klass, id) => Promise.resolve(@draft) spyOn(DatabaseStore, 'unpersistModel').andCallFake (draft) -> Promise.resolve() @@ -85,10 +85,6 @@ describe "SendDraftTask", -> spyOn(Actions, "postNotification") spyOn(Actions, "sendDraftSuccess") - it "should unpersist when successfully sent", -> - waitsForPromise => @task.performRemote().then => - expect(DatabaseStore.unpersistModel).toHaveBeenCalledWith(@draft) - it "should notify the draft was sent", -> waitsForPromise => @task.performRemote().then => args = Actions.sendDraftSuccess.calls[0].args[0] @@ -176,7 +172,7 @@ describe "SendDraftTask", -> describe "when the server responds with `Invalid message public ID`", -> it "should resend the draft without the reply_to_message_id key set", -> - spyOn(DatabaseStore, 'findBy').andCallFake => + spyOn(DatabaseStore, 'run').andCallFake => Promise.resolve(@draft) spyOn(NylasAPI, 'makeRequest').andCallFake ({body, success, error}) => if body.reply_to_message_id @@ -195,7 +191,7 @@ describe "SendDraftTask", -> describe "when the server responds with `Invalid thread ID`", -> it "should resend the draft without the thread_id or reply_to_message_id keys set", -> - spyOn(DatabaseStore, 'findBy').andCallFake => Promise.resolve(@draft) + spyOn(DatabaseStore, 'run').andCallFake => Promise.resolve(@draft) spyOn(NylasAPI, 'makeRequest').andCallFake ({body, success, error}) => new Promise (resolve, reject) => if body.thread_id @@ -215,21 +211,21 @@ describe "SendDraftTask", -> console.log(err.trace) it "throws an error if the draft can't be found", -> - spyOn(DatabaseStore, 'findBy').andCallFake (klass, clientId) -> + spyOn(DatabaseStore, 'run').andCallFake (klass, clientId) -> Promise.resolve() waitsForPromise => @task.performRemote().catch (error) -> expect(error.message).toBeDefined() it "throws an error if the draft isn't saved", -> - spyOn(DatabaseStore, 'findBy').andCallFake (klass, clientId) -> + spyOn(DatabaseStore, 'run').andCallFake (klass, clientId) -> Promise.resolve(serverId: null) waitsForPromise => @task.performRemote().catch (error) -> expect(error.message).toBeDefined() it "throws an error if the DB store has issues", -> - spyOn(DatabaseStore, 'findBy').andCallFake (klass, clientId) -> + spyOn(DatabaseStore, 'run').andCallFake (klass, clientId) -> Promise.reject("DB error") waitsForPromise => @task.performRemote().catch (error) -> diff --git a/spec-nylas/tasks/syncback-draft-spec.coffee b/spec-nylas/tasks/syncback-draft-spec.coffee index 661c4c9b8..3c4e26798 100644 --- a/spec-nylas/tasks/syncback-draft-spec.coffee +++ b/spec-nylas/tasks/syncback-draft-spec.coffee @@ -37,10 +37,11 @@ remoteDraft = -> new Message _.extend {}, testData, {clientId: "local-id", serve describe "SyncbackDraftTask", -> beforeEach -> - spyOn(DatabaseStore, "findBy").andCallFake (klass, {clientId}) -> - if klass is Account + spyOn(DatabaseStore, "run").andCallFake (query) -> + if query._klass is Account return Promise.resolve(new Account(clientId: 'local-abc123', serverId: 'abc123')) + clientId = query.matcherValueForModelKey('clientId') if clientId is "localDraftId" then Promise.resolve(localDraft()) else if clientId is "remoteDraftId" then Promise.resolve(remoteDraft()) else if clientId is "missingDraftId" then Promise.resolve() diff --git a/src/flux/models/query.coffee b/src/flux/models/query.coffee index 27fc9a774..4ac2e944a 100644 --- a/src/flux/models/query.coffee +++ b/src/flux/models/query.coffee @@ -251,5 +251,12 @@ class ModelQuery sql += sort.orderBySQL(@_klass) sql + # Introspection + # (These are here to make specs easy) + + matcherValueForModelKey: (key) -> + matcher = _.find @_matchers, (m) -> m.attr.modelKey = key + matcher?.val + module.exports = ModelQuery