diff --git a/src/js/controller/mail-list.js b/src/js/controller/mail-list.js index e2a18fe..6b52512 100644 --- a/src/js/controller/mail-list.js +++ b/src/js/controller/mail-list.js @@ -9,7 +9,7 @@ define(function(require) { notification = require('js/util/notification'), emailDao, outboxBo; - var MailListCtrl = function($scope) { + var MailListCtrl = function($scope, $timeout) { // // Init // @@ -128,7 +128,7 @@ define(function(require) { } // sort emails - selectFirstMessage(getFolder().messages); + selectFirstMessage(); // display last update updateStatus('Last update: ', new Date()); $scope.$apply(); @@ -198,7 +198,7 @@ define(function(require) { if (!window.chrome || !chrome.identity) { updateStatus('Last update: ', new Date()); getFolder().messages = createDummyMails(); - selectFirstMessage(getFolder().messages); + selectFirstMessage(); return; } @@ -206,8 +206,11 @@ define(function(require) { // unselect selection from old folder $scope.select(); - // display and select first - selectFirstMessage(getFolder().messages); + $timeout(function() { + // display and select first + selectFirstMessage(); + $scope.$apply(); + }); $scope.synchronize(); }); @@ -256,7 +259,9 @@ define(function(require) { $scope.lastUpdate = (time) ? time : ''; } - function selectFirstMessage(emails) { + function selectFirstMessage() { + var emails = $scope.filteredMessages; + if (!emails || emails.length < 1) { $scope.select(); return; @@ -264,7 +269,7 @@ define(function(require) { if (!$scope.state.mailList.selected) { // select first message - $scope.select(emails[emails.length - 1]); + $scope.select(emails[0]); } } diff --git a/test/new-unit/mail-list-ctrl-test.js b/test/new-unit/mail-list-ctrl-test.js index 04ba791..cd0d213 100644 --- a/test/new-unit/mail-list-ctrl-test.js +++ b/test/new-unit/mail-list-ctrl-test.js @@ -200,9 +200,12 @@ define(function(require) { currentFolder: currentFolder }; + var loadVisibleBodiesStub = sinon.stub(scope, 'loadVisibleBodies'); + scope.synchronize(function() { expect(scope.state.nav.currentFolder.messages).to.deep.equal(emails); - expect(scope.state.mailList.selected).to.exist; + expect(loadVisibleBodiesStub.calledOnce).to.be.true; + loadVisibleBodiesStub.restore(); done(); });