2015-12-18 03:46:05 +08:00
|
|
|
{DestroyCategoryTask,
|
|
|
|
NylasAPI,
|
|
|
|
Task,
|
2016-01-23 06:52:19 +08:00
|
|
|
Category,
|
|
|
|
AccountStore,
|
2015-12-18 03:46:05 +08:00
|
|
|
APIError,
|
2016-01-18 16:47:04 +08:00
|
|
|
Category,
|
2015-12-18 03:46:05 +08:00
|
|
|
DatabaseStore,
|
|
|
|
DatabaseTransaction} = require "nylas-exports"
|
2015-11-24 11:41:53 +08:00
|
|
|
|
|
|
|
describe "DestroyCategoryTask", ->
|
|
|
|
pathOf = (fn) ->
|
|
|
|
fn.calls[0].args[0].path
|
|
|
|
|
|
|
|
methodOf = (fn) ->
|
|
|
|
fn.calls[0].args[0].method
|
|
|
|
|
|
|
|
accountIdOf = (fn) ->
|
|
|
|
fn.calls[0].args[0].accountId
|
|
|
|
|
|
|
|
nameOf = (fn) ->
|
|
|
|
fn.calls[0].args[0].body.display_name
|
|
|
|
|
2016-01-23 06:52:19 +08:00
|
|
|
makeAccount = ({usesFolders, usesLabels} = {}) ->
|
|
|
|
spyOn(AccountStore, "accountForId").andReturn {
|
|
|
|
usesFolders: -> usesFolders
|
|
|
|
usesLabels: -> usesLabels
|
|
|
|
}
|
2016-01-18 16:47:04 +08:00
|
|
|
makeTask = ->
|
|
|
|
category = new Category
|
2015-11-24 11:41:53 +08:00
|
|
|
displayName: "important emails"
|
|
|
|
accountId: "account 123"
|
|
|
|
serverId: "server-444"
|
|
|
|
new DestroyCategoryTask
|
|
|
|
category: category
|
|
|
|
|
2015-12-18 03:46:05 +08:00
|
|
|
beforeEach ->
|
|
|
|
spyOn(DatabaseTransaction.prototype, '_query').andCallFake -> Promise.resolve([])
|
|
|
|
spyOn(DatabaseTransaction.prototype, 'persistModel').andCallFake -> Promise.resolve()
|
2015-11-24 11:41:53 +08:00
|
|
|
|
2015-12-18 03:46:05 +08:00
|
|
|
describe "performLocal", ->
|
|
|
|
it "sets an `isDeleted` flag and persists the category", ->
|
2016-01-18 16:47:04 +08:00
|
|
|
task = makeTask()
|
2015-12-18 03:46:05 +08:00
|
|
|
runs =>
|
|
|
|
task.performLocal()
|
|
|
|
waitsFor =>
|
|
|
|
DatabaseTransaction.prototype.persistModel.callCount > 0
|
|
|
|
runs =>
|
|
|
|
model = DatabaseTransaction.prototype.persistModel.calls[0].args[0]
|
|
|
|
expect(model.serverId).toEqual "server-444"
|
|
|
|
expect(model.isDeleted).toBe true
|
2015-11-24 11:41:53 +08:00
|
|
|
|
|
|
|
describe "performRemote", ->
|
|
|
|
it "throws error when no category present", ->
|
|
|
|
waitsForPromise ->
|
2016-01-18 16:47:04 +08:00
|
|
|
task = makeTask()
|
2015-11-24 11:41:53 +08:00
|
|
|
task.category = null
|
|
|
|
task.performRemote()
|
|
|
|
.then ->
|
|
|
|
throw new Error('The promise should reject')
|
|
|
|
.catch Error, (err) ->
|
|
|
|
expect(err).toBeDefined()
|
|
|
|
|
|
|
|
it "throws error when category does not have a serverId", ->
|
|
|
|
waitsForPromise ->
|
2016-01-18 16:47:04 +08:00
|
|
|
task = makeTask()
|
2015-11-24 11:41:53 +08:00
|
|
|
task.category.serverId = undefined
|
|
|
|
task.performRemote()
|
|
|
|
.then ->
|
|
|
|
throw new Error('The promise should reject')
|
|
|
|
.catch Error, (err) ->
|
|
|
|
expect(err).toBeDefined()
|
|
|
|
|
|
|
|
describe "when request succeeds", ->
|
|
|
|
beforeEach ->
|
|
|
|
spyOn(NylasAPI, "makeRequest").andCallFake -> Promise.resolve("null")
|
|
|
|
|
|
|
|
it "sends API req to /labels if user uses labels", ->
|
2016-01-23 06:52:19 +08:00
|
|
|
makeAccount(usesLabels: true)
|
2016-01-18 16:47:04 +08:00
|
|
|
task = makeTask()
|
2015-11-24 11:41:53 +08:00
|
|
|
task.performRemote()
|
|
|
|
expect(pathOf(NylasAPI.makeRequest)).toBe "/labels/server-444"
|
|
|
|
|
|
|
|
it "sends API req to /folders if user uses folders", ->
|
2016-01-23 06:52:19 +08:00
|
|
|
makeAccount(usesFolders: true)
|
2016-01-18 16:47:04 +08:00
|
|
|
task = makeTask()
|
2015-11-24 11:41:53 +08:00
|
|
|
task.performRemote()
|
|
|
|
expect(pathOf(NylasAPI.makeRequest)).toBe "/folders/server-444"
|
|
|
|
|
|
|
|
it "sends DELETE request", ->
|
2016-01-23 06:52:19 +08:00
|
|
|
makeAccount()
|
2016-01-18 16:47:04 +08:00
|
|
|
task = makeTask()
|
2015-11-24 11:41:53 +08:00
|
|
|
task.performRemote()
|
|
|
|
expect(methodOf(NylasAPI.makeRequest)).toBe "DELETE"
|
|
|
|
|
|
|
|
it "sends the account id", ->
|
2016-01-23 06:52:19 +08:00
|
|
|
makeAccount()
|
2016-01-18 16:47:04 +08:00
|
|
|
task = makeTask()
|
2015-11-24 11:41:53 +08:00
|
|
|
task.performRemote()
|
|
|
|
expect(accountIdOf(NylasAPI.makeRequest)).toBe "account 123"
|
|
|
|
|
|
|
|
describe "when request fails", ->
|
|
|
|
beforeEach ->
|
2016-01-23 06:52:19 +08:00
|
|
|
makeAccount()
|
2015-11-24 11:41:53 +08:00
|
|
|
spyOn(NylasEnv, 'emitError')
|
|
|
|
spyOn(NylasAPI, 'makeRequest').andCallFake ->
|
|
|
|
Promise.reject(new APIError({statusCode: 403}))
|
|
|
|
|
|
|
|
it "updates the isDeleted flag for the category and notifies error", ->
|
|
|
|
waitsForPromise ->
|
2016-01-18 16:47:04 +08:00
|
|
|
task = makeTask()
|
2015-11-24 11:41:53 +08:00
|
|
|
spyOn(task, "_notifyUserOfError")
|
|
|
|
|
|
|
|
task.performRemote().then (status) ->
|
|
|
|
expect(status).toEqual Task.Status.Failed
|
|
|
|
expect(task._notifyUserOfError).toHaveBeenCalled()
|
|
|
|
expect(NylasEnv.emitError).toHaveBeenCalled()
|
2015-12-18 03:46:05 +08:00
|
|
|
expect(DatabaseTransaction.prototype.persistModel).toHaveBeenCalled()
|
|
|
|
model = DatabaseTransaction.prototype.persistModel.calls[0].args[0]
|
2015-11-24 11:41:53 +08:00
|
|
|
expect(model.serverId).toEqual "server-444"
|
|
|
|
expect(model.isDeleted).toBe false
|