2015-12-18 03:46:05 +08:00
|
|
|
{Label,
|
|
|
|
NylasAPI,
|
|
|
|
Folder,
|
|
|
|
DatabaseStore,
|
|
|
|
SyncbackCategoryTask,
|
|
|
|
DatabaseTransaction} = require "nylas-exports"
|
2015-09-11 01:34:09 +08:00
|
|
|
|
|
|
|
describe "SyncbackCategoryTask", ->
|
|
|
|
describe "performRemote", ->
|
|
|
|
pathOf = (fn) ->
|
|
|
|
fn.calls[0].args[0].path
|
|
|
|
|
|
|
|
accountIdOf = (fn) ->
|
|
|
|
fn.calls[0].args[0].accountId
|
|
|
|
|
|
|
|
nameOf = (fn) ->
|
|
|
|
fn.calls[0].args[0].body.display_name
|
|
|
|
|
2015-10-22 02:07:08 +08:00
|
|
|
makeTask = (CategoryClass) ->
|
|
|
|
category = new CategoryClass
|
2015-09-11 01:34:09 +08:00
|
|
|
displayName: "important emails"
|
|
|
|
accountId: "account 123"
|
|
|
|
clientId: "local-444"
|
|
|
|
new SyncbackCategoryTask
|
|
|
|
category: category
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
spyOn(NylasAPI, "makeRequest").andCallFake ->
|
|
|
|
Promise.resolve(id: "server-444")
|
2015-12-18 03:46:05 +08:00
|
|
|
spyOn(DatabaseTransaction.prototype, "_query").andCallFake => Promise.resolve([])
|
|
|
|
spyOn(DatabaseTransaction.prototype, "persistModel")
|
2015-09-11 01:34:09 +08:00
|
|
|
|
|
|
|
it "sends API req to /labels if user uses labels", ->
|
2015-10-22 02:07:08 +08:00
|
|
|
task = makeTask(Label)
|
2015-09-11 01:34:09 +08:00
|
|
|
task.performRemote({})
|
|
|
|
expect(pathOf(NylasAPI.makeRequest)).toBe "/labels"
|
|
|
|
|
|
|
|
it "sends API req to /folders if user uses folders", ->
|
2015-10-22 02:07:08 +08:00
|
|
|
task = makeTask(Folder)
|
2015-09-11 01:34:09 +08:00
|
|
|
task.performRemote({})
|
|
|
|
expect(pathOf(NylasAPI.makeRequest)).toBe "/folders"
|
|
|
|
|
|
|
|
it "sends the account id", ->
|
2015-10-22 02:07:08 +08:00
|
|
|
task = makeTask(Label)
|
2015-09-11 01:34:09 +08:00
|
|
|
task.performRemote({})
|
|
|
|
expect(accountIdOf(NylasAPI.makeRequest)).toBe "account 123"
|
|
|
|
|
|
|
|
it "sends the display name in the body", ->
|
2015-10-22 02:07:08 +08:00
|
|
|
task = makeTask(Label)
|
2015-09-11 01:34:09 +08:00
|
|
|
task.performRemote({})
|
|
|
|
expect(nameOf(NylasAPI.makeRequest)).toBe "important emails"
|
|
|
|
|
|
|
|
it "adds server id to the category, then saves the category", ->
|
|
|
|
waitsForPromise ->
|
2015-10-22 02:07:08 +08:00
|
|
|
task = makeTask(Label)
|
2015-09-11 01:34:09 +08:00
|
|
|
task.performRemote({})
|
|
|
|
.then ->
|
2015-12-18 03:46:05 +08:00
|
|
|
expect(DatabaseTransaction.prototype.persistModel).toHaveBeenCalled()
|
|
|
|
model = DatabaseTransaction.prototype.persistModel.calls[0].args[0]
|
2015-09-11 01:34:09 +08:00
|
|
|
expect(model.clientId).toBe "local-444"
|
|
|
|
expect(model.serverId).toBe "server-444"
|