Fix CategoryPicker specs

- Add NylasUtils.mockObservable helper
This commit is contained in:
Juan Tejada 2016-01-11 13:23:34 -08:00
parent 2c74b38323
commit 9847e58b7f
4 changed files with 32 additions and 9 deletions

View file

@ -55,10 +55,9 @@ class CategoryPicker extends React.Component
_registerObservables: => _registerObservables: =>
@_unregisterObservables() @_unregisterObservables()
@disposables = [] @disposables = [
@disposables.push(
Categories.forAccount(@_account).subscribe(@_onCategoriesChanged) Categories.forAccount(@_account).subscribe(@_onCategoriesChanged)
) ]
_unregisterObservables: => _unregisterObservables: =>
return unless @disposables return unless @disposables

View file

@ -15,8 +15,11 @@ CategoryPicker = require '../lib/category-picker'
TaskFactory, TaskFactory,
SyncbackCategoryTask, SyncbackCategoryTask,
FocusedPerspectiveStore, FocusedPerspectiveStore,
NylasTestUtils,
TaskQueueStatusStore} = require 'nylas-exports' TaskQueueStatusStore} = require 'nylas-exports'
{Categories} = require 'nylas-observables'
describe 'CategoryPicker', -> describe 'CategoryPicker', ->
beforeEach -> beforeEach ->
CategoryStore._categoryCache = {} CategoryStore._categoryCache = {}
@ -27,19 +30,27 @@ describe 'CategoryPicker', ->
setupFor = (organizationUnit) -> setupFor = (organizationUnit) ->
NylasEnv.testOrganizationUnit = organizationUnit NylasEnv.testOrganizationUnit = organizationUnit
@categoryClass = if organizationUnit is "label" then Label else Folder @categoryClass = if organizationUnit is "label" then Label else Folder
@account = {
id: TEST_ACCOUNT_ID
usesLabels: -> organizationUnit is "label"
usesFolders: -> organizationUnit isnt "label"
categoryClass: => @categoryClass
}
@inboxCategory = new @categoryClass(id: 'id-123', name: 'inbox', displayName: "INBOX") @inboxCategory = new @categoryClass(id: 'id-123', name: 'inbox', displayName: "INBOX")
@archiveCategory = new @categoryClass(id: 'id-456', name: 'archive', displayName: "ArCHIVe") @archiveCategory = new @categoryClass(id: 'id-456', name: 'archive', displayName: "ArCHIVe")
@userCategory = new @categoryClass(id: 'id-789', name: null, displayName: "MyCategory") @userCategory = new @categoryClass(id: 'id-789', name: null, displayName: "MyCategory")
spyOn(CategoryStore, "getStandardCategories").andReturn [ @inboxCategory, @archiveCategory ] spyOn(Categories, "forAccount").andReturn NylasTestUtils.mockObservable(
spyOn(CategoryStore, "getUserCategories").andReturn [ @userCategory ] [@inboxCategory, @archiveCategory, @userCategory]
)
spyOn(CategoryStore, "getStandardCategory").andReturn @inboxCategory spyOn(CategoryStore, "getStandardCategory").andReturn @inboxCategory
spyOn(AccountStore, "accountForItems").andReturn @account
# By default we're going to set to "inbox". This has implications for # By default we're going to set to "inbox". This has implications for
# what categories get filtered out of the list. # what categories get filtered out of the list.
f = FocusedPerspectiveStore f = FocusedPerspectiveStore
f._setMailView f._defaultMailView() f._setPerspective f._defaultPerspective(@account)
setupForCreateNew = (orgUnit = "folder") -> setupForCreateNew = (orgUnit = "folder") ->
setupFor.call(@, orgUnit) setupFor.call(@, orgUnit)

View file

@ -40,4 +40,18 @@ NylasTestUtils =
document.body.removeChild(child) document.body.removeChild(child)
return return
# Returns mock observable that triggers immediately and provides helper
# function to trigger later
mockObservable: (data, {dispose} = {}) ->
dispose ?= ->
func = ->
return {
subscribe: (fn) ->
func = fn
func(data)
return {dispose}
triggerNext: (nextData = data) ->
func(nextData)
}
module.exports = NylasTestUtils module.exports = NylasTestUtils

View file

@ -41,9 +41,8 @@ class FocusedPerspectiveStore extends NylasStore
@_setPerspective(@_currentBeforeSearch) @_setPerspective(@_currentBeforeSearch)
@_currentBeforeSearch = null @_currentBeforeSearch = null
_defaultPerspective: -> # TODO Update unified MailboxPerspective
# TODO Update unified MailboxPerspective _defaultPerspective: (account = AccountStore.accounts()[0])->
account = AccountStore.accounts()[0]
category = CategoryStore.getStandardCategory(account, "inbox") category = CategoryStore.getStandardCategory(account, "inbox")
return null unless category return null unless category
MailboxPerspective.forCategory(account, category) MailboxPerspective.forCategory(account, category)