2015-09-11 01:34:09 +08:00
|
|
|
_ = require 'underscore'
|
|
|
|
React = require "react/addons"
|
|
|
|
ReactTestUtils = React.addons.TestUtils
|
|
|
|
CategoryPicker = require '../lib/category-picker'
|
|
|
|
{Popover} = require 'nylas-component-kit'
|
|
|
|
|
|
|
|
{Utils,
|
2016-01-18 16:47:04 +08:00
|
|
|
Category,
|
2015-09-11 01:34:09 +08:00
|
|
|
Thread,
|
|
|
|
Actions,
|
2015-10-22 01:57:42 +08:00
|
|
|
AccountStore,
|
2015-09-11 01:34:09 +08:00
|
|
|
CategoryStore,
|
|
|
|
DatabaseStore,
|
2015-10-22 01:38:00 +08:00
|
|
|
TaskFactory,
|
2015-09-11 01:34:09 +08:00
|
|
|
SyncbackCategoryTask,
|
2016-01-09 06:58:31 +08:00
|
|
|
FocusedPerspectiveStore,
|
2016-01-26 03:07:40 +08:00
|
|
|
MailboxPerspective,
|
2016-01-12 05:23:34 +08:00
|
|
|
NylasTestUtils,
|
2015-09-11 01:34:09 +08:00
|
|
|
TaskQueueStatusStore} = require 'nylas-exports'
|
|
|
|
|
2016-01-12 05:23:34 +08:00
|
|
|
{Categories} = require 'nylas-observables'
|
|
|
|
|
2016-01-26 03:11:57 +08:00
|
|
|
describe 'CategoryPicker', ->
|
2015-09-11 01:34:09 +08:00
|
|
|
beforeEach ->
|
|
|
|
CategoryStore._categoryCache = {}
|
|
|
|
|
|
|
|
afterEach ->
|
2015-11-12 02:25:11 +08:00
|
|
|
NylasEnv.testOrganizationUnit = null
|
2015-09-11 01:34:09 +08:00
|
|
|
|
|
|
|
setupFor = (organizationUnit) ->
|
2015-11-12 02:25:11 +08:00
|
|
|
NylasEnv.testOrganizationUnit = organizationUnit
|
2016-01-12 05:23:34 +08:00
|
|
|
@account = {
|
|
|
|
id: TEST_ACCOUNT_ID
|
|
|
|
usesLabels: -> organizationUnit is "label"
|
|
|
|
usesFolders: -> organizationUnit isnt "label"
|
|
|
|
}
|
2015-09-11 01:34:09 +08:00
|
|
|
|
2016-01-26 03:07:40 +08:00
|
|
|
@inboxCategory = new Category(id: 'id-123', name: 'inbox', displayName: "INBOX", accountId: TEST_ACCOUNT_ID)
|
|
|
|
@archiveCategory = new Category(id: 'id-456', name: 'archive', displayName: "ArCHIVe", accountId: TEST_ACCOUNT_ID)
|
|
|
|
@userCategory = new Category(id: 'id-789', name: null, displayName: "MyCategory", accountId: TEST_ACCOUNT_ID)
|
2015-09-11 01:34:09 +08:00
|
|
|
|
2016-01-12 05:23:34 +08:00
|
|
|
spyOn(Categories, "forAccount").andReturn NylasTestUtils.mockObservable(
|
|
|
|
[@inboxCategory, @archiveCategory, @userCategory]
|
|
|
|
)
|
2015-09-11 01:34:09 +08:00
|
|
|
spyOn(CategoryStore, "getStandardCategory").andReturn @inboxCategory
|
2016-01-12 05:23:34 +08:00
|
|
|
spyOn(AccountStore, "accountForItems").andReturn @account
|
2015-09-11 01:34:09 +08:00
|
|
|
|
|
|
|
# By default we're going to set to "inbox". This has implications for
|
|
|
|
# what categories get filtered out of the list.
|
2016-01-26 03:07:40 +08:00
|
|
|
spyOn(FocusedPerspectiveStore, 'current').andCallFake =>
|
|
|
|
MailboxPerspective.forCategory(@inboxCategory)
|
2015-09-11 01:34:09 +08:00
|
|
|
|
|
|
|
setupForCreateNew = (orgUnit = "folder") ->
|
|
|
|
setupFor.call(@, orgUnit)
|
|
|
|
|
2016-01-29 03:13:59 +08:00
|
|
|
@testThread = new Thread(id: 't1', subject: "fake", accountId: TEST_ACCOUNT_ID, categories: [])
|
2015-09-11 01:34:09 +08:00
|
|
|
@picker = ReactTestUtils.renderIntoDocument(
|
|
|
|
<CategoryPicker thread={@testThread} />
|
|
|
|
)
|
|
|
|
|
|
|
|
@popover = ReactTestUtils.findRenderedComponentWithType @picker, Popover
|
|
|
|
@popover.open()
|
|
|
|
|
|
|
|
describe 'when using labels', ->
|
|
|
|
beforeEach ->
|
|
|
|
setupFor.call(@, "label")
|
|
|
|
|
|
|
|
describe 'when using folders', ->
|
|
|
|
beforeEach ->
|
|
|
|
setupFor.call(@, "folder")
|
|
|
|
|
2016-01-29 03:13:59 +08:00
|
|
|
@testThread = new Thread(id: 't1', subject: "fake", accountId: TEST_ACCOUNT_ID, categories: [])
|
2015-09-11 01:34:09 +08:00
|
|
|
@picker = ReactTestUtils.renderIntoDocument(
|
|
|
|
<CategoryPicker thread={@testThread} />
|
|
|
|
)
|
|
|
|
|
|
|
|
it 'lists the desired categories', ->
|
|
|
|
data = @picker.state.categoryData
|
|
|
|
# NOTE: The inbox category is not included here because it's the
|
|
|
|
# currently focused category, which gets filtered out of the list.
|
2016-01-26 03:07:40 +08:00
|
|
|
expect(data.length).toBe 3
|
|
|
|
|
2015-09-11 01:34:09 +08:00
|
|
|
expect(data[0].id).toBe "id-456"
|
|
|
|
expect(data[0].name).toBe "archive"
|
|
|
|
expect(data[0].category).toBe @archiveCategory
|
2016-01-26 03:07:40 +08:00
|
|
|
|
2015-09-11 01:34:09 +08:00
|
|
|
expect(data[1].divider).toBe true
|
|
|
|
expect(data[1].id).toBe "category-divider"
|
2016-01-26 03:07:40 +08:00
|
|
|
|
2015-09-11 01:34:09 +08:00
|
|
|
expect(data[2].id).toBe "id-789"
|
|
|
|
expect(data[2].name).toBeUndefined()
|
|
|
|
expect(data[2].category).toBe @userCategory
|
|
|
|
|
|
|
|
describe "'create new' item", ->
|
|
|
|
beforeEach ->
|
|
|
|
setupForCreateNew.call @
|
|
|
|
|
2015-11-12 02:25:11 +08:00
|
|
|
afterEach -> NylasEnv.testOrganizationUnit = null
|
2015-09-11 01:34:09 +08:00
|
|
|
|
|
|
|
it "is not visible when the search box is empty", ->
|
|
|
|
count = ReactTestUtils.scryRenderedDOMComponentsWithClass(@picker, 'category-create-new').length
|
|
|
|
expect(count).toBe 0
|
|
|
|
|
|
|
|
it "is visible when the search box has text", ->
|
|
|
|
inputNode = React.findDOMNode(ReactTestUtils.scryRenderedDOMComponentsWithTag(@picker, "input")[0])
|
|
|
|
ReactTestUtils.Simulate.change inputNode, target: { value: "calendar" }
|
|
|
|
count = ReactTestUtils.scryRenderedDOMComponentsWithClass(@picker, 'category-create-new').length
|
|
|
|
expect(count).toBe 1
|
|
|
|
|
|
|
|
it "shows folder icon if we're using exchange", ->
|
|
|
|
inputNode = React.findDOMNode(ReactTestUtils.scryRenderedDOMComponentsWithTag(@picker, "input")[0])
|
|
|
|
ReactTestUtils.Simulate.change inputNode, target: { value: "calendar" }
|
|
|
|
count = ReactTestUtils.scryRenderedDOMComponentsWithClass(@picker, 'category-create-new-folder').length
|
|
|
|
expect(count).toBe 1
|
|
|
|
|
|
|
|
describe "'create new' item with labels", ->
|
|
|
|
beforeEach ->
|
|
|
|
setupForCreateNew.call @, "label"
|
|
|
|
|
|
|
|
it "shows label icon if we're using gmail", ->
|
|
|
|
inputNode = React.findDOMNode(ReactTestUtils.scryRenderedDOMComponentsWithTag(@picker, "input")[0])
|
|
|
|
ReactTestUtils.Simulate.change inputNode, target: { value: "calendar" }
|
|
|
|
count = ReactTestUtils.scryRenderedDOMComponentsWithClass(@picker, 'category-create-new-tag').length
|
|
|
|
expect(count).toBe 1
|
|
|
|
|
|
|
|
describe "_onSelectCategory()", ->
|
2015-10-22 01:38:00 +08:00
|
|
|
beforeEach ->
|
|
|
|
setupForCreateNew.call @, "folder"
|
|
|
|
spyOn(TaskFactory, 'taskForRemovingCategory').andCallThrough()
|
|
|
|
spyOn(TaskFactory, 'taskForApplyingCategory').andCallThrough()
|
|
|
|
spyOn(Actions, "queueTask")
|
2015-09-11 01:34:09 +08:00
|
|
|
|
2015-10-22 01:38:00 +08:00
|
|
|
it "closes the popover", ->
|
|
|
|
spyOn(@popover, "close")
|
|
|
|
@picker._onSelectCategory { usage: 0, category: "asdf" }
|
|
|
|
expect(@popover.close).toHaveBeenCalled()
|
2015-09-11 01:34:09 +08:00
|
|
|
|
2015-10-22 01:38:00 +08:00
|
|
|
describe "when selecting a category currently on all the selected items", ->
|
|
|
|
it "fires a task to remove the category", ->
|
|
|
|
input =
|
|
|
|
category: "asdf"
|
|
|
|
usage: 1
|
2015-09-11 01:34:09 +08:00
|
|
|
|
2015-10-22 01:38:00 +08:00
|
|
|
@picker._onSelectCategory(input)
|
|
|
|
expect(TaskFactory.taskForRemovingCategory).toHaveBeenCalledWith
|
|
|
|
threads: [@testThread]
|
|
|
|
category: "asdf"
|
2015-09-11 01:34:09 +08:00
|
|
|
expect(Actions.queueTask).toHaveBeenCalled()
|
|
|
|
|
2015-10-22 01:38:00 +08:00
|
|
|
describe "when selecting a category not on all the selected items", ->
|
|
|
|
it "fires a task to add the category", ->
|
|
|
|
input =
|
|
|
|
category: "asdf"
|
|
|
|
usage: 0
|
2015-09-11 01:34:09 +08:00
|
|
|
|
2015-10-22 01:38:00 +08:00
|
|
|
@picker._onSelectCategory(input)
|
|
|
|
expect(TaskFactory.taskForApplyingCategory).toHaveBeenCalledWith
|
|
|
|
threads: [@testThread]
|
|
|
|
category: "asdf"
|
2015-09-11 01:34:09 +08:00
|
|
|
expect(Actions.queueTask).toHaveBeenCalled()
|
|
|
|
|
2015-10-22 01:38:00 +08:00
|
|
|
describe "when selecting a new category", ->
|
|
|
|
beforeEach ->
|
2015-10-22 01:57:42 +08:00
|
|
|
@input =
|
2015-10-22 01:38:00 +08:00
|
|
|
newCategoryItem: true
|
|
|
|
@picker.setState(searchValue: "teSTing!")
|
2015-09-11 01:34:09 +08:00
|
|
|
|
2015-10-22 01:38:00 +08:00
|
|
|
it "queues a new syncback task for creating a category", ->
|
2015-10-22 01:57:42 +08:00
|
|
|
@picker._onSelectCategory(@input)
|
2015-09-11 01:34:09 +08:00
|
|
|
expect(Actions.queueTask).toHaveBeenCalled()
|
|
|
|
syncbackTask = Actions.queueTask.calls[0].args[0]
|
|
|
|
newCategory = syncbackTask.category
|
2016-01-18 16:47:04 +08:00
|
|
|
expect(newCategory instanceof Category).toBe(true)
|
2015-09-11 01:34:09 +08:00
|
|
|
expect(newCategory.displayName).toBe "teSTing!"
|
|
|
|
expect(newCategory.accountId).toBe TEST_ACCOUNT_ID
|
|
|
|
|
2015-10-22 01:38:00 +08:00
|
|
|
it "queues a task for applying the category after it has saved", ->
|
2015-10-22 01:57:42 +08:00
|
|
|
category = false
|
|
|
|
resolveSave = false
|
2015-09-11 01:34:09 +08:00
|
|
|
spyOn(TaskQueueStatusStore, "waitForPerformRemote").andCallFake (task) ->
|
|
|
|
expect(task instanceof SyncbackCategoryTask).toBe true
|
2015-10-22 01:57:42 +08:00
|
|
|
new Promise (resolve, reject) ->
|
|
|
|
resolveSave = resolve
|
2015-09-11 01:34:09 +08:00
|
|
|
|
|
|
|
spyOn(DatabaseStore, "findBy").andCallFake (klass, {clientId}) ->
|
2016-01-26 03:07:40 +08:00
|
|
|
expect(klass).toBe(Category)
|
2015-10-22 01:38:00 +08:00
|
|
|
expect(typeof clientId).toBe("string")
|
2015-10-22 01:57:42 +08:00
|
|
|
Promise.resolve(category)
|
|
|
|
|
|
|
|
@picker._onSelectCategory(@input)
|
|
|
|
|
|
|
|
waitsFor ->
|
|
|
|
Actions.queueTask.callCount > 0
|
|
|
|
|
|
|
|
runs ->
|
|
|
|
category = Actions.queueTask.calls[0].args[0].category
|
|
|
|
resolveSave()
|
2015-09-11 01:34:09 +08:00
|
|
|
|
2015-10-22 01:38:00 +08:00
|
|
|
waitsFor ->
|
2015-10-22 01:57:42 +08:00
|
|
|
TaskFactory.taskForApplyingCategory.calls.length is 1
|
2015-09-11 01:34:09 +08:00
|
|
|
|
|
|
|
runs ->
|
2015-10-22 01:38:00 +08:00
|
|
|
expect(TaskFactory.taskForApplyingCategory).toHaveBeenCalledWith
|
|
|
|
threads: [@testThread]
|
2015-10-22 01:57:42 +08:00
|
|
|
category: category
|