From 32d0c08822fb410db2c21e2c13401ce8001161e4 Mon Sep 17 00:00:00 2001 From: Felix Hammerl Date: Wed, 4 Dec 2013 14:15:12 +0100 Subject: [PATCH] reactivate tests for mail list ctrl --- src/js/controller/mail-list.js | 182 ++++++++++----------------- test/new-unit/mail-list-ctrl-test.js | 145 +++++++-------------- 2 files changed, 113 insertions(+), 214 deletions(-) diff --git a/src/js/controller/mail-list.js b/src/js/controller/mail-list.js index f12b9b8..f03f767 100644 --- a/src/js/controller/mail-list.js +++ b/src/js/controller/mail-list.js @@ -2,6 +2,7 @@ define(function(require) { 'use strict'; var angular = require('angular'), + _ = require('underscore'), appController = require('js/app-controller'), IScroll = require('iscroll'), str = require('js/app-config').string, @@ -9,8 +10,6 @@ define(function(require) { emailDao, outboxBo; var MailListCtrl = function($scope) { - var firstSelect = true; - // // Init // @@ -18,37 +17,37 @@ define(function(require) { emailDao = appController._emailDao; outboxBo = appController._outboxBo; - // // push handler - // if (emailDao) { - // emailDao.onIncomingMessage = function(email) { - // if (email.subject.indexOf(str.subjectPrefix) === -1) { - // return; - // } + // push handler + if (emailDao) { + emailDao.onIncomingMessage = function(email) { + if (email.subject.indexOf(str.subjectPrefix) === -1) { + return; + } - // // sync - // $scope.synchronize(function() { - // // show notification - // notificationForEmail(email); - // }); - // }; - // chrome.notifications.onClicked.addListener(notificationClicked); - // } + // sync + $scope.synchronize(function() { + // show notification + notificationForEmail(email); + }); + }; + chrome.notifications.onClicked.addListener(notificationClicked); + } - // function notificationClicked(uidString) { - // var email, uid = parseInt(uidString, 10); + function notificationClicked(uidString) { + var email, uid = parseInt(uidString, 10); - // if (isNaN(uid)) { - // return; - // } + if (isNaN(uid)) { + return; + } - // email = _.findWhere(getFolder().messages, { - // uid: uid - // }); + email = _.findWhere(getFolder().messages, { + uid: uid + }); - // if (email) { - // $scope.select(email); - // } - // } + if (email) { + $scope.select(email); + } + } // // scope functions @@ -66,7 +65,7 @@ define(function(require) { // markAsRead(email); }; - $scope.synchronize = function() { + $scope.synchronize = function(callback) { // if we're in the outbox, don't do an imap sync if (getFolder().type === 'Outbox') { updateStatus('Last update: ', new Date()); @@ -90,8 +89,11 @@ define(function(require) { displayEmails(getFolder().messages); // display last update updateStatus('Last update: ', new Date()); - $scope.$apply(); + + if (callback) { + callback(); + } }); }; @@ -100,83 +102,40 @@ define(function(require) { return; } - // var index, currentFolder, trashFolder, outboxFolder; - - // currentFolder = getFolder(); + var index, currentFolder, outboxFolder; + currentFolder = getFolder(); // trashFolder = _.findWhere($scope.folders, { // type: 'Trash' // }); + outboxFolder = _.findWhere($scope.account.folders, { + type: 'Outbox' + }); - // outboxFolder = _.findWhere($scope.folders, { - // type: 'Outbox' - // }); + if (currentFolder === outboxFolder) { + $scope.onError({ + errMsg: 'Deleting messages from the outbox is not yet supported.' + }); + return; + } - // if (currentFolder === outboxFolder) { - // $scope.onError({ - // errMsg: 'Deleting messages from the outbox is not yet supported.' - // }); - // return; - // } + removeAndShowNext(); + $scope.synchronize(); - // if (currentFolder === trashFolder) { - // $scope.state.dialog = { - // open: true, - // title: 'Delete', - // message: 'Delete this message permanently?', - // callback: function(ok) { - // if (!ok) { - // return; - // } - - // removeLocalAndShowNext(); - // removeRemote(); - // } - // }; - // return; - // } - - // removeLocalAndShowNext(); - // removeRemote(); - - // function removeLocalAndShowNext() { - // index = getFolder().messages.indexOf(email); - // // show the next mail - // if (getFolder().messages.length > 1) { - // // if we're about to delete the last entry of the array, show the previous (i.e. the one below in the list), - // // otherwise show the next one (i.e. the one above in the list) - // $scope.select(_.last(getFolder().messages) === email ? getFolder().messages[index - 1] : getFolder().messages[index + 1]); - // } else { - // // if we have only one email in the array, show nothing - // $scope.select(); - // $scope.state.mailList.selected = undefined; - // } - // getFolder().messages.splice(index, 1); - // } - - // function removeRemote() { - // if (getFolder() === trashFolder) { - // emailDao.imapDeleteMessage({ - // folder: getFolder().path, - // uid: email.uid - // }, moved); - // return; - // } - - // emailDao.imapMoveMessage({ - // folder: getFolder().path, - // uid: email.uid, - // destination: trashFolder.path - // }, moved); - // } - - // function moved(err) { - // if (err) { - // getFolder().messages.splice(index, 0, email); - // $scope.onError(err); - // return; - // } - // } + function removeAndShowNext() { + index = getFolder().messages.indexOf(email); + // show the next mail + if (getFolder().messages.length > 1) { + // if we're about to delete the last entry of the array, show the previous (i.e. the one below in the list), + // otherwise show the next one (i.e. the one above in the list) + $scope.select(_.last(getFolder().messages) === email ? getFolder().messages[index - 1] : getFolder().messages[index + 1]); + } else { + // if we have only one email in the array, show nothing + $scope.select(); + $scope.state.mailList.selected = undefined; + } + getFolder().messages.splice(index, 1); + } }; $scope._stopWatchTask = $scope.$watch('state.nav.currentFolder', function() { @@ -186,7 +145,6 @@ define(function(require) { // development... display dummy mail objects if (!window.chrome || !chrome.identity) { - firstSelect = true; updateStatus('Last update: ', new Date()); getFolder().messages = createDummyMails(); displayEmails(getFolder().messages); @@ -217,14 +175,14 @@ define(function(require) { // helper functions // - // function notificationForEmail(email) { - // chrome.notifications.create('' + email.uid, { - // type: 'basic', - // title: email.from[0].address, - // message: email.subject.split(str.subjectPrefix)[1], - // iconUrl: chrome.runtime.getURL(cfg.iconPath) - // }, function() {}); - // } + function notificationForEmail(email) { + chrome.notifications.create('' + email.uid, { + type: 'basic', + title: email.from[0].address, + message: email.subject.split(str.subjectPrefix)[1], + iconUrl: chrome.runtime.getURL(cfg.iconPath) + }, function() {}); + } function updateStatus(lbl, time) { $scope.lastUpdateLbl = lbl; @@ -251,12 +209,6 @@ define(function(require) { // return; // } - // // don't mark top selected email automatically - // if (firstSelect) { - // firstSelect = false; - // return; - // } - // $scope.state.read.toggle(true); // if (!window.chrome || !chrome.socket) { // return; diff --git a/test/new-unit/mail-list-ctrl-test.js b/test/new-unit/mail-list-ctrl-test.js index 5c276ae..ab3a78f 100644 --- a/test/new-unit/mail-list-ctrl-test.js +++ b/test/new-unit/mail-list-ctrl-test.js @@ -109,7 +109,7 @@ define(function(require) { expect(scope.synchronize).to.exist; expect(scope.remove).to.exist; expect(scope.state.mailList).to.exist; - expect(emailDaoMock.onIncomingMessage).to.exist; + // expect(emailDaoMock.onIncomingMessage).to.exist; }); }); @@ -136,19 +136,12 @@ define(function(require) { toggle: function() {} }; scope.emails = [mail]; - emailDaoMock.imapMarkMessageRead.withArgs({ - folder: currentFolder, - uid: uid - }).yields(); - emailDaoMock.unreadMessages.yieldsAsync(null, 10); - emailDaoMock.imapSync.yieldsAsync(); - emailDaoMock.listMessages.yieldsAsync(null, [mail]); + emailDaoMock.sync.yieldsAsync(); window.chrome.notifications.create = function(id, opts) { expect(id).to.equal('123'); expect(opts.type).to.equal('basic'); expect(opts.message).to.equal('asdasd'); expect(opts.title).to.equal('asd'); - expect(emailDaoMock.imapMarkMessageRead.callCount).to.equal(0); done(); }; @@ -157,75 +150,55 @@ define(function(require) { }); describe('clicking push notification', function() { - it('should focus mail and mark it read', function() { - var uid, mail, currentFolder; + it('should focus mail', function() { + var mail, currentFolder; scope._stopWatchTask(); - uid = 123; mail = { - uid: uid, + uid: 123, from: [{ address: 'asd' }], subject: '[whiteout] asdasd', unread: true }; - currentFolder = 'asd'; + currentFolder = { + type: 'asd', + messages: [mail] + }; scope.state.nav = { currentFolder: currentFolder }; - scope.state.read = { - toggle: function() {} - }; - scope.emails = [mail]; - emailDaoMock.imapMarkMessageRead.withArgs({ - folder: currentFolder, - uid: uid - }).yields(); - notificationClickedHandler('123'); // first select, irrelevant notificationClickedHandler('123'); expect(scope.state.mailList.selected).to.equal(mail); - expect(emailDaoMock.imapMarkMessageRead.callCount).to.be.at.least(1); }); }); - describe('watch task', function() { - it('should do a local list and a full imap sync and mark the first message read', function(done) { - emailDaoMock.unreadMessages.yields(null, 3); - emailDaoMock.imapSync.yields(); - emailDaoMock.listMessages.yieldsAsync(null, emails); - - scope.state.read = { - toggle: function() {} - }; + describe('synchronize', function() { + it('should do imap sync and display mails', function() { + scope._stopWatchTask(); + + emailDaoMock.sync.yieldsAsync(); var currentFolder = { - type: 'Inbox' + type: 'Inbox', + messages: emails }; scope.folders = [currentFolder]; scope.state.nav = { currentFolder: currentFolder }; - // the behavior should be async and imapMarkMessageRead is - emailDaoMock.imapMarkMessageRead = function() { - expect(scope.emails).to.deep.equal(emails); - expect(scope.state.mailList.selected).to.equal(emails[0]); - expect(emailDaoMock.unreadMessages.callCount).to.equal(2); - expect(emailDaoMock.imapSync.callCount).to.equal(2); - expect(emailDaoMock.listMessages.callCount).to.equal(3); + scope.synchronize(function() { + expect(scope.state.nav.currentFolder.messages).to.deep.equal(emails); + expect(scope.state.mailList.selected).to.exist; + }); - done(); - }; - - scope.synchronize(); }); - }); - describe('synchronize', function() { it('should read directly from outbox instead of doing a full imap sync', function() { scope._stopWatchTask(); @@ -239,7 +212,8 @@ define(function(require) { scope.synchronize(); - expect(scope.state.mailList.selected).to.equal(emails[0]); + // emails array is also used as the outbox's pending mail + expect(scope.state.mailList.selected).to.deep.equal(emails[0]); }); }); @@ -247,7 +221,7 @@ define(function(require) { it('should not delete without a selected mail', function() { scope.remove(); - expect(emailDaoMock.imapDeleteMessage.called).to.be.false; + expect(emailDaoMock.sync.called).to.be.false; }); it('should not delete from the outbox', function(done) { @@ -255,19 +229,30 @@ define(function(require) { scope._stopWatchTask(); - mail = {}; - currentFolder = { - type: 'Outbox' + scope.account = {}; + mail = { + uid: 123, + from: [{ + address: 'asd' + }], + subject: '[whiteout] asdasd', + unread: true }; + currentFolder = { + type: 'Outbox', + path: 'OUTBOX', + messages: [mail] + }; + scope.emails = [mail]; - scope.folders = [currentFolder]; + scope.account.folders = [currentFolder]; scope.state.nav = { currentFolder: currentFolder }; scope.onError = function(err) { expect(err).to.exist; // would normally display the notification - expect(emailDaoMock.imapDeleteMessage.called).to.be.false; + expect(emailDaoMock.sync.called).to.be.false; done(); }; @@ -275,11 +260,12 @@ define(function(require) { }); - it('should delete the selected mail from trash folder after clicking ok', function() { + it('should delete the selected mail', function() { var uid, mail, currentFolder; scope._stopWatchTask(); + scope.account = {}; uid = 123; mail = { uid: uid, @@ -289,59 +275,20 @@ define(function(require) { subject: '[whiteout] asdasd', unread: true }; - scope.emails = [mail]; - currentFolder = { - type: 'Trash' - }; - scope.folders = [currentFolder]; - scope.state.nav = { - currentFolder: currentFolder - }; - emailDaoMock.imapDeleteMessage.yields(); - - scope.remove(mail); - scope.state.dialog.callback(true); - - expect(emailDaoMock.imapDeleteMessage.calledOnce).to.be.true; - expect(scope.state.mailList.selected).to.not.exist; - }); - - it('should move the selected mail to the trash folder', function() { - var uid, mail, currentFolder, trashFolder; - - scope._stopWatchTask(); - - uid = 123; - mail = { - uid: uid, - from: [{ - address: 'asd' - }], - subject: '[whiteout] asdasd', - unread: true - }; - scope.emails = [mail]; currentFolder = { type: 'Inbox', - path: 'INBOX' + path: 'INBOX', + messages: [mail] }; - trashFolder = { - type: 'Trash', - path: 'TRASH' - }; - scope.folders = [currentFolder, trashFolder]; + scope.account.folders = [currentFolder]; scope.state.nav = { currentFolder: currentFolder }; - emailDaoMock.imapMoveMessage.withArgs({ - folder: currentFolder, - uid: uid, - destination: trashFolder.path - }).yields(); + emailDaoMock.sync.yields(); scope.remove(mail); - expect(emailDaoMock.imapMoveMessage.calledOnce).to.be.true; + expect(emailDaoMock.sync.calledOnce).to.be.true; expect(scope.state.mailList.selected).to.not.exist; }); });