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'
|
|
|
|
FocusedPerspectiveStore = require '../../src/flux/stores/focused-perspective-store'
|
|
|
|
|
|
|
|
describe "FocusedPerspectiveStore", ->
|
|
|
|
beforeEach ->
|
|
|
|
spyOn(FocusedPerspectiveStore, 'trigger')
|
|
|
|
FocusedPerspectiveStore._perspective = null
|
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)
|
|
|
|
@inboxFilter = MailboxPerspective.forCategory(@inboxCategory)
|
|
|
|
@userCategory = new Category(id: 'id-456', name: null, displayName: "MyCategory", accountId: @account.id)
|
|
|
|
@userFilter = MailboxPerspective.forCategory(@userCategory)
|
|
|
|
|
|
|
|
spyOn(CategoryStore, "getStandardCategory").andReturn @inboxCategory
|
|
|
|
spyOn(CategoryStore, "byId").andCallFake (id) =>
|
|
|
|
return @inboxCategory if id is @inboxCategory.id
|
|
|
|
return @userCategory if id is @userCategory.id
|
|
|
|
return null
|
|
|
|
|
2016-02-10 12:06:48 +08:00
|
|
|
describe "_initCurrentPerspective", ->
|
|
|
|
beforeEach ->
|
|
|
|
@default = 'default'
|
|
|
|
@accounts = [{id: 1}, {id: 2}]
|
|
|
|
spyOn(MailboxPerspective, 'fromJSON').andCallFake (json) -> json
|
|
|
|
spyOn(FocusedPerspectiveStore, '_defaultPerspective').andReturn @default
|
|
|
|
|
|
|
|
it "uses default perspective when no perspective has been saved", ->
|
|
|
|
current = FocusedPerspectiveStore._initCurrentPerspective(undefined, @accounts)
|
|
|
|
expect(current).toEqual @default
|
|
|
|
|
|
|
|
it "uses default if saved perspective has more account ids not present in current accounts", ->
|
|
|
|
saved = {accountIds: [1,2,3]}
|
|
|
|
current = FocusedPerspectiveStore._initCurrentPerspective(saved, @accounts)
|
|
|
|
expect(current).toEqual @default
|
|
|
|
|
|
|
|
saved = {accountIds: [3]}
|
|
|
|
current = FocusedPerspectiveStore._initCurrentPerspective(saved, @accounts)
|
|
|
|
expect(current).toEqual @default
|
|
|
|
|
|
|
|
it "uses saved perspective if all accounts in saved perspective are present in the current accounts", ->
|
|
|
|
saved = {accountIds: [1,2]}
|
|
|
|
current = FocusedPerspectiveStore._initCurrentPerspective(saved, @accounts)
|
|
|
|
expect(current).toEqual saved
|
|
|
|
|
|
|
|
saved = {accountIds: [1]}
|
|
|
|
current = FocusedPerspectiveStore._initCurrentPerspective(saved, @accounts)
|
|
|
|
expect(current).toEqual saved
|
|
|
|
|
2016-01-26 03:07:40 +08:00
|
|
|
describe "_onCategoryStoreChanged", ->
|
|
|
|
it "should set the current category to Inbox when it is unset", ->
|
|
|
|
FocusedPerspectiveStore._perspective = null
|
|
|
|
FocusedPerspectiveStore._onCategoryStoreChanged()
|
|
|
|
expect(FocusedPerspectiveStore.current()).not.toBe(null)
|
|
|
|
expect(FocusedPerspectiveStore.current().categories()).toEqual([@inboxCategory])
|
|
|
|
|
|
|
|
it "should set the current category to Inbox when the current category no longer exists in the CategoryStore", ->
|
|
|
|
otherAccountInbox = @inboxCategory.clone()
|
|
|
|
otherAccountInbox.serverId = 'other-id'
|
|
|
|
FocusedPerspectiveStore._perspective = MailboxPerspective.forCategory(otherAccountInbox)
|
|
|
|
FocusedPerspectiveStore._onCategoryStoreChanged()
|
|
|
|
expect(FocusedPerspectiveStore.current().categories()).toEqual([@inboxCategory])
|
|
|
|
|
|
|
|
describe "_onFocusPerspective", ->
|
2016-01-26 08:36:56 +08:00
|
|
|
it "should focus the category and trigger", ->
|
2016-01-26 03:07:40 +08:00
|
|
|
FocusedPerspectiveStore._onFocusPerspective(@userFilter)
|
|
|
|
expect(FocusedPerspectiveStore.trigger).toHaveBeenCalled()
|
|
|
|
expect(FocusedPerspectiveStore.current().categories()).toEqual([@userCategory])
|
|
|
|
|
|
|
|
it "should do nothing if the category is already focused", ->
|
|
|
|
FocusedPerspectiveStore._onFocusPerspective(@inboxFilter)
|
|
|
|
spyOn(FocusedPerspectiveStore, '_setPerspective')
|
|
|
|
FocusedPerspectiveStore._onFocusPerspective(@inboxFilter)
|
|
|
|
expect(FocusedPerspectiveStore._setPerspective).not.toHaveBeenCalled()
|