diff --git a/spec/tasks/syncback-category-task-spec.coffee b/spec/tasks/syncback-category-task-spec.coffee index 006509bbe..f767f0c3d 100644 --- a/spec/tasks/syncback-category-task-spec.coffee +++ b/spec/tasks/syncback-category-task-spec.coffee @@ -13,15 +13,13 @@ describe "SyncbackCategoryTask", -> nameOf = (fn) -> fn.calls[0].args[0].body.display_name - makeTask = (orgUnit) -> - Category = if orgUnit is "label" then Label else Folder - category = new Category + makeTask = (CategoryClass) -> + category = new CategoryClass displayName: "important emails" accountId: "account 123" clientId: "local-444" new SyncbackCategoryTask category: category - organizationUnit: orgUnit beforeEach -> spyOn(NylasAPI, "makeRequest").andCallFake -> @@ -29,28 +27,28 @@ describe "SyncbackCategoryTask", -> spyOn(DatabaseStore, "persistModel") it "sends API req to /labels if user uses labels", -> - task = makeTask "label" + task = makeTask(Label) task.performRemote({}) expect(pathOf(NylasAPI.makeRequest)).toBe "/labels" it "sends API req to /folders if user uses folders", -> - task = makeTask "folder" + task = makeTask(Folder) task.performRemote({}) expect(pathOf(NylasAPI.makeRequest)).toBe "/folders" it "sends the account id", -> - task = makeTask "label" + task = makeTask(Label) task.performRemote({}) expect(accountIdOf(NylasAPI.makeRequest)).toBe "account 123" it "sends the display name in the body", -> - task = makeTask "label" + task = makeTask(Label) task.performRemote({}) expect(nameOf(NylasAPI.makeRequest)).toBe "important emails" it "adds server id to the category, then saves the category", -> waitsForPromise -> - task = makeTask "label" + task = makeTask(Label) task.performRemote({}) .then -> expect(DatabaseStore.persistModel).toHaveBeenCalled() diff --git a/src/flux/tasks/syncback-category-task.coffee b/src/flux/tasks/syncback-category-task.coffee index a3d3a20e8..a83a56c2d 100644 --- a/src/flux/tasks/syncback-category-task.coffee +++ b/src/flux/tasks/syncback-category-task.coffee @@ -1,5 +1,7 @@ CategoryStore = require '../stores/category-store' DatabaseStore = require '../stores/database-store' +Label = require '../models/label' +Folder = require '../models/folder' {generateTempId} = require '../models/utils' Task = require './task' NylasAPI = require '../nylas-api' @@ -7,11 +9,14 @@ NylasAPI = require '../nylas-api' module.exports = class SyncbackCategoryTask extends Task - constructor: ({@category, @organizationUnit}={}) -> + constructor: ({@category}={}) -> super label: -> - "Creating new #{@organizationUnit}..." + if @category instanceof Label + "Creating new label..." + else + "Creating new folder..." performLocal: -> # When we send drafts, we don't update anything in the app until @@ -19,8 +24,6 @@ module.exports = class SyncbackCategoryTask extends Task # already sent when they haven't! if not @category return Promise.reject(new Error("Attempt to call SyncbackCategoryTask.performLocal without @category.")) - else if @organizationUnit isnt "label" and @organizationUnit isnt "folder" - return Promise.reject(new Error("Attempt to call SyncbackCategoryTask.performLocal with @organizationUnit which wasn't 'folder' or 'label'.")) if @_shouldChangeBackwards() DatabaseStore.unpersistModel @category @@ -28,9 +31,9 @@ module.exports = class SyncbackCategoryTask extends Task DatabaseStore.persistModel @category performRemote: -> - if @organizationUnit is "label" + if @category instanceof Label path = "/labels" - else if @organizationUnit is "folder" + else path = "/folders" NylasAPI.makeRequest