2016-01-09 06:58:31 +08:00
|
|
|
_ = require 'underscore'
|
|
|
|
|
|
|
|
Actions = require '../../src/flux/actions'
|
2016-01-26 03:07:40 +08:00
|
|
|
Category = require '../../src/flux/models/category'
|
2016-01-09 06:58:31 +08:00
|
|
|
MailboxPerspective = require '../../src/mailbox-perspective'
|
|
|
|
|
|
|
|
CategoryStore = require '../../src/flux/stores/category-store'
|
|
|
|
AccountStore = require '../../src/flux/stores/account-store'
|
2016-10-01 15:08:19 +08:00
|
|
|
FocusedPerspectiveStore = require('../../src/flux/stores/focused-perspective-store').default
|
2016-01-09 06:58:31 +08:00
|
|
|
|
2016-09-22 02:56:54 +08:00
|
|
|
describe "FocusedPerspectiveStore", ->
|
2016-01-09 06:58:31 +08:00
|
|
|
beforeEach ->
|
|
|
|
spyOn(FocusedPerspectiveStore, 'trigger')
|
2016-09-29 05:46:03 +08:00
|
|
|
FocusedPerspectiveStore._current = MailboxPerspective.forNothing()
|
2016-01-12 07:58:10 +08:00
|
|
|
@account = AccountStore.accounts()[0]
|
2016-01-09 06:58:31 +08:00
|
|
|
|
2016-01-26 03:07:40 +08:00
|
|
|
@inboxCategory = new Category(id: 'id-123', name: 'inbox', displayName: "INBOX", accountId: @account.id)
|
2016-04-11 22:59:40 +08:00
|
|
|
@inboxPerspective = MailboxPerspective.forCategory(@inboxCategory)
|
2016-01-26 03:07:40 +08:00
|
|
|
@userCategory = new Category(id: 'id-456', name: null, displayName: "MyCategory", accountId: @account.id)
|
2016-04-11 22:59:40 +08:00
|
|
|
@userPerspective = MailboxPerspective.forCategory(@userCategory)
|
2016-01-26 03:07:40 +08:00
|
|
|
|
|
|
|
spyOn(CategoryStore, "getStandardCategory").andReturn @inboxCategory
|
2016-09-29 05:46:03 +08:00
|
|
|
spyOn(CategoryStore, "byId").andCallFake (aid, cid) =>
|
|
|
|
return {id: 'A'} if aid is 1 and cid is 'A'
|
|
|
|
return @inboxCategory if cid is @inboxCategory.id
|
|
|
|
return @userCategory if cid is @userCategory.id
|
2016-01-26 03:07:40 +08:00
|
|
|
return null
|
|
|
|
|
2016-09-29 05:46:03 +08:00
|
|
|
describe "_initializeFromSavedState", ->
|
2016-02-10 12:06:48 +08:00
|
|
|
beforeEach ->
|
2016-09-29 05:46:03 +08:00
|
|
|
@default = MailboxPerspective.forCategory(@inboxCategory)
|
|
|
|
spyOn(AccountStore, 'accountIds').andReturn([1, 2])
|
2016-02-10 12:06:48 +08:00
|
|
|
spyOn(MailboxPerspective, 'fromJSON').andCallFake (json) -> json
|
|
|
|
spyOn(FocusedPerspectiveStore, '_defaultPerspective').andReturn @default
|
2016-09-29 05:46:03 +08:00
|
|
|
spyOn(FocusedPerspectiveStore, '_setPerspective')
|
2016-02-10 12:06:48 +08:00
|
|
|
|
|
|
|
it "uses default perspective when no perspective has been saved", ->
|
2016-09-29 05:46:03 +08:00
|
|
|
NylasEnv.savedState.sidebarAccountIds = undefined
|
2016-09-22 02:23:38 +08:00
|
|
|
NylasEnv.savedState.perspective = undefined
|
2016-09-29 05:46:03 +08:00
|
|
|
FocusedPerspectiveStore._initializeFromSavedState()
|
|
|
|
expect(FocusedPerspectiveStore._setPerspective).toHaveBeenCalledWith(@default, @default.accountIds)
|
2016-02-10 12:06:48 +08:00
|
|
|
|
2016-09-29 05:46:03 +08:00
|
|
|
it "uses default if the saved perspective has account ids no longer present", ->
|
|
|
|
NylasEnv.savedState.sidebarAccountIds = [1, 2, 3]
|
|
|
|
NylasEnv.savedState.perspective =
|
|
|
|
accountIds: [1, 2, 3],
|
2016-09-29 06:58:16 +08:00
|
|
|
categories: => [{accountId: 1, id: 'A'}],
|
2016-09-29 05:46:03 +08:00
|
|
|
FocusedPerspectiveStore._initializeFromSavedState()
|
|
|
|
expect(FocusedPerspectiveStore._setPerspective).toHaveBeenCalledWith(@default, @default.accountIds)
|
2016-02-10 12:06:48 +08:00
|
|
|
|
2016-09-29 05:46:03 +08:00
|
|
|
NylasEnv.savedState.sidebarAccountIds = [1, 2, 3]
|
|
|
|
NylasEnv.savedState.perspective =
|
|
|
|
accountIds: [3]
|
2016-09-29 06:58:16 +08:00
|
|
|
categories: => [{accountId: 3, id: 'A'}]
|
2016-09-29 05:46:03 +08:00
|
|
|
FocusedPerspectiveStore._initializeFromSavedState()
|
|
|
|
expect(FocusedPerspectiveStore._setPerspective).toHaveBeenCalledWith(@default, @default.accountIds)
|
2016-02-10 12:06:48 +08:00
|
|
|
|
2016-09-29 05:46:03 +08:00
|
|
|
it "uses default if the saved perspective has category ids no longer present", ->
|
|
|
|
NylasEnv.savedState.sidebarAccountIds = [2]
|
|
|
|
NylasEnv.savedState.perspective =
|
|
|
|
accountIds: [2]
|
2016-09-29 06:58:16 +08:00
|
|
|
categories: => [{accountId: 2, id: 'C'}]
|
2016-09-29 05:46:03 +08:00
|
|
|
FocusedPerspectiveStore._initializeFromSavedState()
|
|
|
|
expect(FocusedPerspectiveStore._setPerspective).toHaveBeenCalledWith(@default, @default.accountIds)
|
2016-02-10 12:06:48 +08:00
|
|
|
|
2016-09-29 05:46:03 +08:00
|
|
|
it "does not honor sidebarAccountIds if it includes account ids no longer present", ->
|
|
|
|
NylasEnv.savedState.sidebarAccountIds = [1, 2, 3]
|
|
|
|
NylasEnv.savedState.perspective =
|
|
|
|
accountIds: [1]
|
2016-09-29 06:58:16 +08:00
|
|
|
categories: => [{accountId: 1, id: 'A'}]
|
2016-09-29 05:46:03 +08:00
|
|
|
FocusedPerspectiveStore._initializeFromSavedState()
|
|
|
|
expect(FocusedPerspectiveStore._setPerspective).toHaveBeenCalledWith(NylasEnv.savedState.perspective, [1])
|
|
|
|
|
|
|
|
it "uses the saved perspective if it is still valid", ->
|
|
|
|
NylasEnv.savedState.sidebarAccountIds = [1, 2]
|
|
|
|
NylasEnv.savedState.perspective =
|
|
|
|
accountIds: [1, 2]
|
2016-09-29 06:58:16 +08:00
|
|
|
categories: => [{accountId: 1, id: 'A'}]
|
2016-09-29 05:46:03 +08:00
|
|
|
FocusedPerspectiveStore._initializeFromSavedState()
|
|
|
|
expect(FocusedPerspectiveStore._setPerspective).toHaveBeenCalledWith(NylasEnv.savedState.perspective, [1, 2])
|
|
|
|
|
|
|
|
NylasEnv.savedState.sidebarAccountIds = [1, 2]
|
|
|
|
NylasEnv.savedState.perspective =
|
|
|
|
accountIds: [1]
|
2016-09-29 06:58:16 +08:00
|
|
|
categories: => []
|
2016-09-29 05:46:03 +08:00
|
|
|
type: 'DraftsMailboxPerspective'
|
|
|
|
|
|
|
|
FocusedPerspectiveStore._initializeFromSavedState()
|
|
|
|
expect(FocusedPerspectiveStore._setPerspective).toHaveBeenCalledWith(NylasEnv.savedState.perspective, [1, 2])
|
|
|
|
|
|
|
|
NylasEnv.savedState.sidebarAccountIds = [1]
|
|
|
|
NylasEnv.savedState.perspective =
|
|
|
|
accountIds: [1]
|
2016-09-29 06:58:16 +08:00
|
|
|
categories: => []
|
2016-09-29 05:46:03 +08:00
|
|
|
type: 'DraftsMailboxPerspective'
|
|
|
|
|
|
|
|
FocusedPerspectiveStore._initializeFromSavedState()
|
|
|
|
expect(FocusedPerspectiveStore._setPerspective).toHaveBeenCalledWith(NylasEnv.savedState.perspective, [1])
|
2016-02-10 12:06:48 +08:00
|
|
|
|
2016-01-26 03:07:40 +08:00
|
|
|
describe "_onCategoryStoreChanged", ->
|
2016-09-29 05:46:03 +08:00
|
|
|
it "should try to initialize if the current perspective is `nothing`", ->
|
|
|
|
spyOn(FocusedPerspectiveStore, '_initializeFromSavedState')
|
|
|
|
|
|
|
|
FocusedPerspectiveStore._current = @inboxPerspective
|
|
|
|
FocusedPerspectiveStore._onCategoryStoreChanged()
|
|
|
|
expect(FocusedPerspectiveStore._initializeFromSavedState).not.toHaveBeenCalled()
|
|
|
|
|
|
|
|
FocusedPerspectiveStore._current = MailboxPerspective.forNothing()
|
2016-01-26 03:07:40 +08:00
|
|
|
FocusedPerspectiveStore._onCategoryStoreChanged()
|
2016-09-29 05:46:03 +08:00
|
|
|
expect(FocusedPerspectiveStore._initializeFromSavedState).toHaveBeenCalled()
|
2016-03-25 10:35:24 +08:00
|
|
|
|
2016-04-11 22:59:40 +08:00
|
|
|
it "should set the current category to default when the current category no longer exists in the CategoryStore", ->
|
|
|
|
defaultPerspective = @inboxPerspective
|
|
|
|
spyOn(FocusedPerspectiveStore, '_defaultPerspective').andReturn(defaultPerspective)
|
2016-09-29 05:46:03 +08:00
|
|
|
|
2016-01-26 03:07:40 +08:00
|
|
|
otherAccountInbox = @inboxCategory.clone()
|
|
|
|
otherAccountInbox.serverId = 'other-id'
|
2016-04-11 22:59:40 +08:00
|
|
|
FocusedPerspectiveStore._current = MailboxPerspective.forCategory(otherAccountInbox)
|
2016-09-29 05:46:03 +08:00
|
|
|
|
2016-01-26 03:07:40 +08:00
|
|
|
FocusedPerspectiveStore._onCategoryStoreChanged()
|
2016-04-11 22:59:40 +08:00
|
|
|
expect(FocusedPerspectiveStore.current()).toEqual(defaultPerspective)
|
2016-01-26 03:07:40 +08:00
|
|
|
|
|
|
|
describe "_onFocusPerspective", ->
|
2016-01-26 08:36:56 +08:00
|
|
|
it "should focus the category and trigger", ->
|
2016-04-11 22:59:40 +08:00
|
|
|
FocusedPerspectiveStore._onFocusPerspective(@userPerspective)
|
2016-01-26 03:07:40 +08:00
|
|
|
expect(FocusedPerspectiveStore.trigger).toHaveBeenCalled()
|
|
|
|
expect(FocusedPerspectiveStore.current().categories()).toEqual([@userCategory])
|
|
|
|
|
2016-05-05 02:50:54 +08:00
|
|
|
describe "_setPerspective", ->
|
|
|
|
it "should not trigger if the perspective is already focused", ->
|
|
|
|
FocusedPerspectiveStore._setPerspective(@inboxPerspective)
|
|
|
|
FocusedPerspectiveStore.trigger.reset()
|
|
|
|
FocusedPerspectiveStore._setPerspective(@inboxPerspective)
|
|
|
|
expect(FocusedPerspectiveStore.trigger).not.toHaveBeenCalled()
|