fix(specs): Future specs should stub DatabaseStore.run, not find/findBy

This commit is contained in:
Ben Gotow 2015-08-28 12:41:56 -07:00
parent d76766fa38
commit 944a5838d8
3 changed files with 18 additions and 14 deletions

View file

@ -72,12 +72,12 @@ describe "SendDraftTask", ->
name: 'Dummy' name: 'Dummy'
email: 'dummy@nylas.com' email: 'dummy@nylas.com'
@task = new SendDraftTask(@draftClientId) @task = new SendDraftTask(@draftClientId)
spyOn(NylasAPI, 'makeRequest').andCallFake (options) => spyOn(NylasAPI, 'makeRequest').andCallFake (options) =>
options.success?(@draft.toJSON()) options.success?(@draft.toJSON())
Promise.resolve(@draft.toJSON()) Promise.resolve(@draft.toJSON())
spyOn(DatabaseStore, 'findBy').andCallFake (klass, id) => spyOn(DatabaseStore, 'run').andCallFake (klass, id) =>
Promise.resolve(@draft)
spyOn(DatabaseStore, 'find').andCallFake (klass, id) =>
Promise.resolve(@draft) Promise.resolve(@draft)
spyOn(DatabaseStore, 'unpersistModel').andCallFake (draft) -> spyOn(DatabaseStore, 'unpersistModel').andCallFake (draft) ->
Promise.resolve() Promise.resolve()
@ -85,10 +85,6 @@ describe "SendDraftTask", ->
spyOn(Actions, "postNotification") spyOn(Actions, "postNotification")
spyOn(Actions, "sendDraftSuccess") 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", -> it "should notify the draft was sent", ->
waitsForPromise => @task.performRemote().then => waitsForPromise => @task.performRemote().then =>
args = Actions.sendDraftSuccess.calls[0].args[0] args = Actions.sendDraftSuccess.calls[0].args[0]
@ -176,7 +172,7 @@ describe "SendDraftTask", ->
describe "when the server responds with `Invalid message public ID`", -> describe "when the server responds with `Invalid message public ID`", ->
it "should resend the draft without the reply_to_message_id key set", -> it "should resend the draft without the reply_to_message_id key set", ->
spyOn(DatabaseStore, 'findBy').andCallFake => spyOn(DatabaseStore, 'run').andCallFake =>
Promise.resolve(@draft) Promise.resolve(@draft)
spyOn(NylasAPI, 'makeRequest').andCallFake ({body, success, error}) => spyOn(NylasAPI, 'makeRequest').andCallFake ({body, success, error}) =>
if body.reply_to_message_id if body.reply_to_message_id
@ -195,7 +191,7 @@ describe "SendDraftTask", ->
describe "when the server responds with `Invalid thread ID`", -> 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", -> 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}) => spyOn(NylasAPI, 'makeRequest').andCallFake ({body, success, error}) =>
new Promise (resolve, reject) => new Promise (resolve, reject) =>
if body.thread_id if body.thread_id
@ -215,21 +211,21 @@ describe "SendDraftTask", ->
console.log(err.trace) console.log(err.trace)
it "throws an error if the draft can't be found", -> 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() Promise.resolve()
waitsForPromise => waitsForPromise =>
@task.performRemote().catch (error) -> @task.performRemote().catch (error) ->
expect(error.message).toBeDefined() expect(error.message).toBeDefined()
it "throws an error if the draft isn't saved", -> 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) Promise.resolve(serverId: null)
waitsForPromise => waitsForPromise =>
@task.performRemote().catch (error) -> @task.performRemote().catch (error) ->
expect(error.message).toBeDefined() expect(error.message).toBeDefined()
it "throws an error if the DB store has issues", -> 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") Promise.reject("DB error")
waitsForPromise => waitsForPromise =>
@task.performRemote().catch (error) -> @task.performRemote().catch (error) ->

View file

@ -37,10 +37,11 @@ remoteDraft = -> new Message _.extend {}, testData, {clientId: "local-id", serve
describe "SyncbackDraftTask", -> describe "SyncbackDraftTask", ->
beforeEach -> beforeEach ->
spyOn(DatabaseStore, "findBy").andCallFake (klass, {clientId}) -> spyOn(DatabaseStore, "run").andCallFake (query) ->
if klass is Account if query._klass is Account
return Promise.resolve(new Account(clientId: 'local-abc123', serverId: 'abc123')) return Promise.resolve(new Account(clientId: 'local-abc123', serverId: 'abc123'))
clientId = query.matcherValueForModelKey('clientId')
if clientId is "localDraftId" then Promise.resolve(localDraft()) if clientId is "localDraftId" then Promise.resolve(localDraft())
else if clientId is "remoteDraftId" then Promise.resolve(remoteDraft()) else if clientId is "remoteDraftId" then Promise.resolve(remoteDraft())
else if clientId is "missingDraftId" then Promise.resolve() else if clientId is "missingDraftId" then Promise.resolve()

View file

@ -251,5 +251,12 @@ class ModelQuery
sql += sort.orderBySQL(@_klass) sql += sort.orderBySQL(@_klass)
sql sql
# Introspection
# (These are here to make specs easy)
matcherValueForModelKey: (key) ->
matcher = _.find @_matchers, (m) -> m.attr.modelKey = key
matcher?.val
module.exports = ModelQuery module.exports = ModelQuery