From 68b6d7df60ffd794ea8e6396d906253575e4a78a Mon Sep 17 00:00:00 2001 From: Juan Tejada Date: Tue, 9 Feb 2016 20:06:48 -0800 Subject: [PATCH] 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 --- .../focused-perspective-store-spec.coffee | 29 +++++++++++++++++++ .../stores/focused-perspective-store.coffee | 15 ++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/spec/stores/focused-perspective-store-spec.coffee b/spec/stores/focused-perspective-store-spec.coffee index fee30ecad..b17f653ee 100644 --- a/spec/stores/focused-perspective-store-spec.coffee +++ b/spec/stores/focused-perspective-store-spec.coffee @@ -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 diff --git a/src/flux/stores/focused-perspective-store.coffee b/src/flux/stores/focused-perspective-store.coffee index 4cf640bf7..0c115ff23 100644 --- a/src/flux/stores/focused-perspective-store.coffee +++ b/src/flux/stores/focused-perspective-store.coffee @@ -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: ->