From 81008e70bb426e79f23a59aaab9c1177a43f6de1 Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Wed, 21 Oct 2015 10:57:42 -0700 Subject: [PATCH] fix(specs): Unbreak tests for CategoryPicker --- .../category-picker/lib/category-picker.cjsx | 4 +- .../spec/category-picker-spec.cjsx | 43 +++++++++++-------- spec/spec-helper.coffee | 19 ++++---- 3 files changed, 37 insertions(+), 29 deletions(-) diff --git a/internal_packages/category-picker/lib/category-picker.cjsx b/internal_packages/category-picker/lib/category-picker.cjsx index 1e7bbf031..c9c261005 100644 --- a/internal_packages/category-picker/lib/category-picker.cjsx +++ b/internal_packages/category-picker/lib/category-picker.cjsx @@ -200,9 +200,11 @@ class CategoryPicker extends React.Component @refs.menu.setSelectedItem(null) if item.newCategoryItem - category = new AccountStore.current().categoryClass() + CategoryClass = AccountStore.current().categoryClass() + category = new CategoryClass displayName: @state.searchValue, accountId: AccountStore.current().id + syncbackTask = new SyncbackCategoryTask({category}) TaskQueueStatusStore.waitForPerformRemote(syncbackTask).then => DatabaseStore.findBy(category.constructor, clientId: category.clientId).then (category) => diff --git a/internal_packages/category-picker/spec/category-picker-spec.cjsx b/internal_packages/category-picker/spec/category-picker-spec.cjsx index 878d9c1e8..0ec7c00e1 100644 --- a/internal_packages/category-picker/spec/category-picker-spec.cjsx +++ b/internal_packages/category-picker/spec/category-picker-spec.cjsx @@ -9,6 +9,7 @@ CategoryPicker = require '../lib/category-picker' Folder, Thread, Actions, + AccountStore, CategoryStore, DatabaseStore, TaskFactory, @@ -16,7 +17,7 @@ CategoryPicker = require '../lib/category-picker' FocusedMailViewStore, TaskQueueStatusStore} = require 'nylas-exports' -fdescribe 'CategoryPicker', -> +describe 'CategoryPicker', -> beforeEach -> CategoryStore._categoryCache = {} @@ -25,11 +26,11 @@ fdescribe 'CategoryPicker', -> setupFor = (organizationUnit) -> atom.testOrganizationUnit = organizationUnit - klass = if organizationUnit is "label" then Label else Folder + @categoryClass = if organizationUnit is "label" then Label else Folder - @inboxCategory = new klass(id: 'id-123', name: 'inbox', displayName: "INBOX") - @archiveCategory = new klass(id: 'id-456', name: 'archive', displayName: "ArCHIVe") - @userCategory = new klass(id: 'id-789', name: null, displayName: "MyCategory") + @inboxCategory = new @categoryClass(id: 'id-123', name: 'inbox', displayName: "INBOX") + @archiveCategory = new @categoryClass(id: 'id-456', name: 'archive', displayName: "ArCHIVe") + @userCategory = new @categoryClass(id: 'id-789', name: null, displayName: "MyCategory") spyOn(CategoryStore, "getStandardCategories").andReturn [ @inboxCategory, @archiveCategory ] spyOn(CategoryStore, "getUserCategories").andReturn [ @userCategory ] @@ -147,37 +148,45 @@ fdescribe 'CategoryPicker', -> describe "when selecting a new category", -> beforeEach -> - input = + @input = newCategoryItem: true @picker.setState(searchValue: "teSTing!") - @picker._onSelectCategory(input) it "queues a new syncback task for creating a category", -> + @picker._onSelectCategory(@input) expect(Actions.queueTask).toHaveBeenCalled() syncbackTask = Actions.queueTask.calls[0].args[0] newCategory = syncbackTask.category - expect(syncbackTask.organizationUnit).toBe "label" + expect(newCategory instanceof @categoryClass).toBe(true) expect(newCategory.displayName).toBe "teSTing!" expect(newCategory.accountId).toBe TEST_ACCOUNT_ID it "queues a task for applying the category after it has saved", -> - label = new Label(displayName: "teSTing!") - + category = false + resolveSave = false spyOn(TaskQueueStatusStore, "waitForPerformRemote").andCallFake (task) -> expect(task instanceof SyncbackCategoryTask).toBe true - Promise.resolve() + new Promise (resolve, reject) -> + resolveSave = resolve spyOn(DatabaseStore, "findBy").andCallFake (klass, {clientId}) -> - expect(klass).toBe(Label) + expect(klass).toBe(Folder) expect(typeof clientId).toBe("string") - Promise.resolve(label) + Promise.resolve(category) + + @picker._onSelectCategory(@input) waitsFor -> - Actions.queueTask.calls.length > 1 - label = Actions.queueTask.calls[0].args[0].category + Actions.queueTask.callCount > 0 + + runs -> + category = Actions.queueTask.calls[0].args[0].category + resolveSave() + + waitsFor -> + TaskFactory.taskForApplyingCategory.calls.length is 1 runs -> expect(TaskFactory.taskForApplyingCategory).toHaveBeenCalledWith threads: [@testThread] - category: label - expect(TaskFactory.taskForApplyingCategory.callCount).toBe(1) + category: category diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index 42c37faf5..1d3fa8176 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -153,17 +153,14 @@ beforeEach -> spyOn(atom.menu, 'sendToBrowserProcess') # Log in a fake user - spyOn(AccountStore, 'current').andCallFake -> new Account - name: TEST_ACCOUNT_NAME - provider: "gmail" - emailAddress: TEST_ACCOUNT_EMAIL - organizationUnit: atom.testOrganizationUnit - clientId: TEST_ACCOUNT_CLIENT_ID - serverId: TEST_ACCOUNT_ID - usesLabels: -> atom.testOrganizationUnit is "label" - usesFolders: -> atom.testOrganizationUnit is "folder" - me: -> - new Contact(email: TEST_ACCOUNT_EMAIL, name: TEST_ACCOUNT_NAME) + spyOn(AccountStore, 'current').andCallFake -> + new Account + provider: "gmail" + name: TEST_ACCOUNT_NAME + emailAddress: TEST_ACCOUNT_EMAIL + organizationUnit: atom.testOrganizationUnit + clientId: TEST_ACCOUNT_CLIENT_ID + serverId: TEST_ACCOUNT_ID # reset config before each spec; don't load or save from/to `config.json` spyOn(Config::, 'load')