fix(focused-persepective): Init saved perspective correctly

- When accounts changed and the saved perspective could reference
  accounts that no longer exist and cause all sorts of errors. This is
  fixed.
- Add specs
- Fixes some sentry errors
This commit is contained in:
Juan Tejada 2016-02-09 20:06:48 -08:00
parent 8a8170eb0e
commit 68b6d7df60
2 changed files with 41 additions and 3 deletions

View file

@ -25,6 +25,35 @@ describe "FocusedPerspectiveStore", ->
return @userCategory if id is @userCategory.id
return null
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
describe "_onCategoryStoreChanged", ->
it "should set the current category to Inbox when it is unset", ->
FocusedPerspectiveStore._perspective = null

View file

@ -7,14 +7,23 @@ Actions = require '../actions'
class FocusedPerspectiveStore extends NylasStore
constructor: ->
if NylasEnv.savedState.perspective
@_current = MailboxPerspective.fromJSON(NylasEnv.savedState.perspective)
@_current ?= @_defaultPerspective()
@_current = @_initCurrentPerspective(NylasEnv.savedState.perspective)
@listenTo CategoryStore, @_onCategoryStoreChanged
@listenTo Actions.focusMailboxPerspective, @_onFocusPerspective
@listenTo Actions.focusDefaultMailboxPerspectiveForAccounts, @_onFocusAccounts
_initCurrentPerspective: (savedPerspective, accounts = AccountStore.accounts()) =>
if savedPerspective
current = MailboxPerspective.fromJSON(savedPerspective)
if current
accountIds = _.pluck(accounts, 'id')
accountIdsNotPresent = _.difference(current.accountIds, accountIds)
current = null if accountIdsNotPresent.length > 0
current ?= @_defaultPerspective()
return current
# Inbound Events
_onCategoryStoreChanged: ->