add(specs): Add specs for MailboxPerspective

- Renames canApplyToThreads -> canReceiveThreads and applyToThreads ->
  receiveThreads
- Add initial specs and better documentation for
  MailboxPerspective.canReceiveThreads.
This commit is contained in:
Juan Tejada 2016-01-28 14:11:50 -08:00
parent 8237e3742c
commit 89f431edec
3 changed files with 84 additions and 15 deletions

View file

@ -74,7 +74,7 @@ class SidebarItem
jsonString = event.dataTransfer.getData(item.dataTransferType) jsonString = event.dataTransfer.getData(item.dataTransferType)
data = Utils.jsonParse(jsonString) data = Utils.jsonParse(jsonString)
return unless data return unless data
item.perspective.applyToThreads(data.threadIds) item.perspective.receiveThreads(data.threadIds)
shouldAcceptDrop: (item, event) -> shouldAcceptDrop: (item, event) ->
target = item.perspective target = item.perspective
current = FocusedPerspectiveStore.current() current = FocusedPerspectiveStore.current()
@ -83,7 +83,7 @@ class SidebarItem
return false unless data return false unless data
return false unless target return false unless target
return false if target.isEqual(current) return false if target.isEqual(current)
return false unless target.canApplyToThreads(data.accountIds) return false unless target.canReceiveThreads(data.accountIds)
return item.dataTransferType in event.dataTransfer.types return item.dataTransferType in event.dataTransfer.types

View file

@ -0,0 +1,50 @@
{AccountStore, MailboxPerspective, Category} = require 'nylas-exports'
describe 'MailboxPerspective', ->
beforeEach ->
spyOn(AccountStore, 'accountForId').andReturn {categoryIcon: -> 'icon'}
@accountIds = ['a1', 'a2', 'a3']
@perspective = new MailboxPerspective(@accountIds)
describe 'isEqual', ->
# TODO
describe 'canReceiveThreads', ->
it 'returns true if the thread account ids are included in the current account ids', ->
expect(@perspective.canReceiveThreads(['a1'])).toBe true
it 'returns false otherwise', ->
expect(@perspective.canReceiveThreads(['a4'])).toBe false
expect(@perspective.canReceiveThreads([])).toBe false
expect(@perspective.canReceiveThreads()).toBe false
describe 'CategoriesMailboxPerspective', ->
beforeEach ->
@accountIds = ['a1', 'a2']
@categories = [
new Category(displayName: 'c1', accountId: 'a1')
new Category(displayName: 'c2', accountId: 'a2')
new Category(displayName: 'c3', accountId: 'a2')
]
@perspective = MailboxPerspective.forCategories(@categories)
describe 'canReceiveThreads', ->
it 'returns true if the thread account ids are included in the current account ids', ->
expect(@perspective.canReceiveThreads(['a2'])).toBe true
it 'returns false otherwise', ->
expect(@perspective.canReceiveThreads(['a4'])).toBe false
expect(@perspective.canReceiveThreads([])).toBe false
expect(@perspective.canReceiveThreads()).toBe false
it 'returns false if it is not a locked category', ->
@perspective._categories.push(
new Category(name: 'sent', displayName: 'c4', accountId: 'a1')
)
expect(@perspective.canReceiveThreads(['a2'])).toBe false
describe 'receiveThreads', ->
# TODO

View file

@ -73,12 +73,28 @@ class MailboxPerspective
threadUnreadCount: => threadUnreadCount: =>
0 0
canApplyToThreads: (targetAccountIds) => # Public:
targetIdsInCurrent = _.difference(targetAccountIds, @accountIds).length is 0 # - accountIds {Array} Array of unique account ids associated with the threads
return targetIdsInCurrent # that want to be included in this perspective
#
# Returns true if the accountIds are part of the current ids, or false
# otherwise. This means that it checks if I am moving trying to move threads
# betwee the same set of accounts:
#
# E.g.:
# perpective = Starred for accountIds: a1, a2
# thread1 has accountId a3
# thread2 has accountId a2
#
# perspective.canReceiveThreads([a2, a3]) -> false -> I cant move those threads to Starred
# perspective.canReceiveThreads([a2]) -> true -> I can move that thread to # Starred
canReceiveThreads: (accountIds) =>
return false unless accountIds and accountIds.length > 0
incomingIdsInCurrent = _.difference(accountIds, @accountIds).length is 0
return incomingIdsInCurrent
applyToThreads: (threadsOrIds) => receiveThreads: (threadsOrIds) =>
throw new Error("applyToThreads: Not implemented in base class.") throw new Error("receiveThreads: Not implemented in base class.")
# Whether or not the current MailboxPerspective can "archive" or "trash" # Whether or not the current MailboxPerspective can "archive" or "trash"
# Subclasses should call `super` if they override these methods # Subclasses should call `super` if they override these methods
@ -110,7 +126,7 @@ class SearchMailboxPerspective extends MailboxPerspective
threads: => threads: =>
new SearchSubscription(@searchQuery, @accountIds) new SearchSubscription(@searchQuery, @accountIds)
canApplyToThreads: => canReceiveThreads: =>
false false
canArchiveThreads: => canArchiveThreads: =>
@ -131,7 +147,7 @@ class DraftsMailboxPerspective extends MailboxPerspective
threads: => threads: =>
null null
canApplyToThreads: => canReceiveThreads: =>
false false
class StarredMailboxPerspective extends MailboxPerspective class StarredMailboxPerspective extends MailboxPerspective
@ -149,10 +165,10 @@ class StarredMailboxPerspective extends MailboxPerspective
return new MutableQuerySubscription(query, {asResultSet: true}) return new MutableQuerySubscription(query, {asResultSet: true})
canApplyToThreads: => canReceiveThreads: =>
super super
applyToThreads: (threadsOrIds) => receiveThreads: (threadsOrIds) =>
ChangeStarredTask = require './flux/tasks/change-starred-task' ChangeStarredTask = require './flux/tasks/change-starred-task'
task = new ChangeStarredTask({threads:threadsOrIds, starred: true}) task = new ChangeStarredTask({threads:threadsOrIds, starred: true})
Actions.queueTask(task) Actions.queueTask(task)
@ -166,7 +182,7 @@ class EmptyMailboxPerspective extends MailboxPerspective
query = DatabaseStore.findAll(Thread).where(accountId: -1).limit(0) query = DatabaseStore.findAll(Thread).where(accountId: -1).limit(0)
return new MutableQuerySubscription(query, {asResultSet: true}) return new MutableQuerySubscription(query, {asResultSet: true})
canApplyToThreads: => canReceiveThreads: =>
false false
canArchiveThreads: => canArchiveThreads: =>
@ -175,7 +191,7 @@ class EmptyMailboxPerspective extends MailboxPerspective
canTrashThreads: => canTrashThreads: =>
false false
applyToThreads: (threadsOrIds) => receiveThreads: (threadsOrIds) =>
class CategoryMailboxPerspective extends MailboxPerspective class CategoryMailboxPerspective extends MailboxPerspective
@ -224,7 +240,7 @@ class CategoryMailboxPerspective extends MailboxPerspective
isInbox: => isInbox: =>
@_categories[0].name is 'inbox' @_categories[0].name is 'inbox'
canApplyToThreads: => canReceiveThreads: =>
super and not _.any @_categories, (c) -> c.isLockedCategory() super and not _.any @_categories, (c) -> c.isLockedCategory()
canArchiveThreads: => canArchiveThreads: =>
@ -237,10 +253,13 @@ class CategoryMailboxPerspective extends MailboxPerspective
return false if cat.name in ["trash"] return false if cat.name in ["trash"]
super super
applyToThreads: (threadsOrIds) => receiveThreads: (threadsOrIds) =>
FocusedPerspectiveStore = require './flux/stores/focused-perspective-store' FocusedPerspectiveStore = require './flux/stores/focused-perspective-store'
currentCategories = FocusedPerspectiveStore.current().categories() currentCategories = FocusedPerspectiveStore.current().categories()
# TODO
# This assumes that the we don't have more than one category per accountId
# attached to this perspective
DatabaseStore.modelify(Thread, threadsOrIds).then (threads) => DatabaseStore.modelify(Thread, threadsOrIds).then (threads) =>
tasks = TaskFactory.tasksForApplyingCategories tasks = TaskFactory.tasksForApplyingCategories
threads: threads threads: threads