2013-11-11 11:56:51 -05:00
|
|
|
define(function(require) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var expect = chai.expect,
|
|
|
|
angular = require('angular'),
|
|
|
|
mocks = require('angularMocks'),
|
|
|
|
MailListCtrl = require('js/controller/mail-list'),
|
|
|
|
EmailDAO = require('js/dao/email-dao'),
|
|
|
|
DeviceStorageDAO = require('js/dao/devicestorage-dao'),
|
|
|
|
KeychainDAO = require('js/dao/keychain-dao'),
|
|
|
|
appController = require('js/app-controller');
|
|
|
|
|
2014-02-17 08:31:14 -05:00
|
|
|
chai.Assertion.includeStack = true;
|
|
|
|
|
2013-11-11 11:56:51 -05:00
|
|
|
describe('Mail List controller unit test', function() {
|
|
|
|
var scope, ctrl, origEmailDao, emailDaoMock, keychainMock, deviceStorageMock,
|
2013-11-21 11:37:07 -05:00
|
|
|
emailAddress, notificationClickedHandler, emails,
|
2013-11-11 11:56:51 -05:00
|
|
|
hasChrome, hasNotifications, hasSocket, hasRuntime, hasIdentity;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
hasChrome = !! window.chrome;
|
|
|
|
hasNotifications = !! window.chrome.notifications;
|
|
|
|
hasSocket = !! window.chrome.socket;
|
|
|
|
hasIdentity = !! window.chrome.identity;
|
|
|
|
if (!hasChrome) {
|
|
|
|
window.chrome = {};
|
|
|
|
}
|
|
|
|
if (!hasNotifications) {
|
|
|
|
window.chrome.notifications = {
|
|
|
|
onClicked: {
|
|
|
|
addListener: function(handler) {
|
|
|
|
notificationClickedHandler = handler;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
create: function() {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (!hasSocket) {
|
|
|
|
window.chrome.socket = {};
|
|
|
|
}
|
|
|
|
if (!hasRuntime) {
|
|
|
|
window.chrome.runtime = {
|
|
|
|
getURL: function() {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (!hasIdentity) {
|
|
|
|
window.chrome.identity = {};
|
|
|
|
}
|
2013-11-21 11:37:07 -05:00
|
|
|
|
|
|
|
emails = [{
|
|
|
|
unread: true
|
|
|
|
}, {
|
|
|
|
unread: true
|
|
|
|
}, {
|
|
|
|
unread: true
|
|
|
|
}];
|
|
|
|
appController._outboxBo = {
|
|
|
|
pendingEmails: emails
|
|
|
|
};
|
|
|
|
|
2013-11-11 11:56:51 -05:00
|
|
|
origEmailDao = appController._emailDao;
|
|
|
|
emailDaoMock = sinon.createStubInstance(EmailDAO);
|
|
|
|
appController._emailDao = emailDaoMock;
|
|
|
|
emailAddress = 'fred@foo.com';
|
|
|
|
emailDaoMock._account = {
|
|
|
|
emailAddress: emailAddress,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
keychainMock = sinon.createStubInstance(KeychainDAO);
|
|
|
|
emailDaoMock._keychain = keychainMock;
|
|
|
|
|
|
|
|
deviceStorageMock = sinon.createStubInstance(DeviceStorageDAO);
|
|
|
|
emailDaoMock._devicestorage = deviceStorageMock;
|
|
|
|
|
|
|
|
angular.module('maillisttest', []);
|
|
|
|
mocks.module('maillisttest');
|
|
|
|
mocks.inject(function($rootScope, $controller) {
|
|
|
|
scope = $rootScope.$new();
|
2013-12-05 12:28:18 -05:00
|
|
|
scope.state = {
|
|
|
|
read: {
|
|
|
|
toggle: function() {}
|
|
|
|
}
|
|
|
|
};
|
2013-11-11 11:56:51 -05:00
|
|
|
ctrl = $controller(MailListCtrl, {
|
|
|
|
$scope: scope
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
if (!hasNotifications) {
|
|
|
|
delete window.chrome.notifications;
|
|
|
|
}
|
|
|
|
if (!hasSocket) {
|
|
|
|
delete window.chrome.socket;
|
|
|
|
}
|
|
|
|
if (!hasRuntime) {
|
|
|
|
delete window.chrome.runtime;
|
|
|
|
}
|
|
|
|
if (!hasChrome) {
|
|
|
|
delete window.chrome;
|
|
|
|
}
|
|
|
|
if (!hasIdentity) {
|
|
|
|
delete window.chrome.identity;
|
|
|
|
}
|
2013-11-21 11:37:07 -05:00
|
|
|
|
2013-11-11 11:56:51 -05:00
|
|
|
// restore the module
|
|
|
|
appController._emailDao = origEmailDao;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('scope variables', function() {
|
|
|
|
it('should be set correctly', function() {
|
|
|
|
expect(scope.select).to.exist;
|
|
|
|
expect(scope.synchronize).to.exist;
|
|
|
|
expect(scope.remove).to.exist;
|
|
|
|
expect(scope.state.mailList).to.exist;
|
2013-12-04 08:15:12 -05:00
|
|
|
// expect(emailDaoMock.onIncomingMessage).to.exist;
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('push notification', function() {
|
|
|
|
it('should focus mail and not mark it read', function(done) {
|
|
|
|
var uid, mail, currentFolder;
|
|
|
|
|
2013-11-21 11:37:07 -05:00
|
|
|
scope._stopWatchTask();
|
|
|
|
|
2013-11-11 11:56:51 -05:00
|
|
|
uid = 123;
|
|
|
|
mail = {
|
|
|
|
uid: uid,
|
|
|
|
from: [{
|
|
|
|
address: 'asd'
|
|
|
|
}],
|
|
|
|
subject: '[whiteout] asdasd',
|
|
|
|
unread: true
|
|
|
|
};
|
|
|
|
currentFolder = 'asd';
|
|
|
|
scope.state.nav = {
|
|
|
|
currentFolder: currentFolder
|
|
|
|
};
|
|
|
|
scope.state.read = {
|
|
|
|
toggle: function() {}
|
|
|
|
};
|
|
|
|
scope.emails = [mail];
|
2013-12-04 08:15:12 -05:00
|
|
|
emailDaoMock.sync.yieldsAsync();
|
2013-11-11 11:56:51 -05:00
|
|
|
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');
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
|
|
|
emailDaoMock.onIncomingMessage(mail);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('clicking push notification', function() {
|
2013-12-04 08:15:12 -05:00
|
|
|
it('should focus mail', function() {
|
|
|
|
var mail, currentFolder;
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2013-11-21 11:37:07 -05:00
|
|
|
scope._stopWatchTask();
|
|
|
|
|
2013-11-11 11:56:51 -05:00
|
|
|
mail = {
|
2013-12-04 08:15:12 -05:00
|
|
|
uid: 123,
|
2013-11-11 11:56:51 -05:00
|
|
|
from: [{
|
|
|
|
address: 'asd'
|
|
|
|
}],
|
|
|
|
subject: '[whiteout] asdasd',
|
|
|
|
unread: true
|
|
|
|
};
|
2013-12-04 08:15:12 -05:00
|
|
|
currentFolder = {
|
|
|
|
type: 'asd',
|
|
|
|
messages: [mail]
|
|
|
|
};
|
2013-11-11 11:56:51 -05:00
|
|
|
scope.state.nav = {
|
|
|
|
currentFolder: currentFolder
|
|
|
|
};
|
|
|
|
|
|
|
|
notificationClickedHandler('123');
|
|
|
|
|
2013-11-21 11:37:07 -05:00
|
|
|
expect(scope.state.mailList.selected).to.equal(mail);
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
});
|
2013-11-21 11:37:07 -05:00
|
|
|
|
2013-12-04 08:15:12 -05:00
|
|
|
describe('synchronize', function() {
|
2013-12-04 09:53:49 -05:00
|
|
|
it('should do imap sync and display mails', function(done) {
|
2013-12-04 08:15:12 -05:00
|
|
|
scope._stopWatchTask();
|
|
|
|
|
|
|
|
emailDaoMock.sync.yieldsAsync();
|
2013-11-21 11:37:07 -05:00
|
|
|
|
|
|
|
var currentFolder = {
|
2013-12-04 08:15:12 -05:00
|
|
|
type: 'Inbox',
|
|
|
|
messages: emails
|
2013-11-21 11:37:07 -05:00
|
|
|
};
|
|
|
|
scope.folders = [currentFolder];
|
|
|
|
scope.state.nav = {
|
|
|
|
currentFolder: currentFolder
|
|
|
|
};
|
|
|
|
|
2013-12-04 08:15:12 -05:00
|
|
|
scope.synchronize(function() {
|
|
|
|
expect(scope.state.nav.currentFolder.messages).to.deep.equal(emails);
|
|
|
|
expect(scope.state.mailList.selected).to.exist;
|
2013-12-04 09:53:49 -05:00
|
|
|
done();
|
2013-12-04 08:15:12 -05:00
|
|
|
});
|
2013-11-21 11:37:07 -05:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should read directly from outbox instead of doing a full imap sync', function() {
|
|
|
|
scope._stopWatchTask();
|
|
|
|
|
|
|
|
var currentFolder = {
|
|
|
|
type: 'Outbox'
|
|
|
|
};
|
|
|
|
scope.folders = [currentFolder];
|
|
|
|
scope.state.nav = {
|
|
|
|
currentFolder: currentFolder
|
|
|
|
};
|
2013-12-05 12:28:18 -05:00
|
|
|
scope.state.mailList.selected = undefined;
|
2013-11-21 11:37:07 -05:00
|
|
|
|
|
|
|
scope.synchronize();
|
|
|
|
|
2013-12-04 08:15:12 -05:00
|
|
|
// emails array is also used as the outbox's pending mail
|
2013-12-05 12:28:18 -05:00
|
|
|
expect(scope.state.mailList.selected).to.deep.equal(emails[emails.length - 1]);
|
2013-11-21 11:37:07 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-02-20 09:42:51 -05:00
|
|
|
describe('getBody', function() {
|
2014-02-17 08:31:14 -05:00
|
|
|
it('should get the mail content', function() {
|
|
|
|
scope.state.nav = {
|
|
|
|
currentFolder: {
|
|
|
|
type: 'asd',
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-20 09:42:51 -05:00
|
|
|
scope.getBody();
|
|
|
|
expect(emailDaoMock.getBody.calledOnce).to.be.true;
|
2014-02-17 08:31:14 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('select', function() {
|
|
|
|
it('should decrypt, focus mark an unread mail as read', function() {
|
|
|
|
var mail, synchronizeMock;
|
|
|
|
|
|
|
|
mail = {
|
|
|
|
unread: true
|
|
|
|
};
|
|
|
|
synchronizeMock = sinon.stub(scope, 'synchronize');
|
|
|
|
scope.state = {
|
|
|
|
nav: {
|
|
|
|
currentFolder: {
|
2014-02-18 11:05:51 -05:00
|
|
|
type: 'asd'
|
2014-02-17 08:31:14 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
mailList: {},
|
|
|
|
read: {
|
|
|
|
toggle: function() {}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
scope.select(mail);
|
|
|
|
|
|
|
|
expect(emailDaoMock.decryptMessageContent.calledOnce).to.be.true;
|
|
|
|
expect(synchronizeMock.calledOnce).to.be.true;
|
|
|
|
expect(scope.state.mailList.selected).to.equal(mail);
|
|
|
|
|
|
|
|
scope.synchronize.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should decrypt and focus a read mail', function() {
|
|
|
|
var mail, synchronizeMock;
|
|
|
|
|
|
|
|
mail = {
|
|
|
|
unread: false
|
|
|
|
};
|
|
|
|
synchronizeMock = sinon.stub(scope, 'synchronize');
|
|
|
|
scope.state = {
|
|
|
|
mailList: {},
|
|
|
|
read: {
|
|
|
|
toggle: function() {}
|
2014-02-18 11:05:51 -05:00
|
|
|
},
|
|
|
|
nav: {
|
|
|
|
currentFolder: {
|
|
|
|
type: 'asd'
|
|
|
|
}
|
2014-02-17 08:31:14 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
scope.select(mail);
|
|
|
|
|
|
|
|
expect(emailDaoMock.decryptMessageContent.calledOnce).to.be.true;
|
|
|
|
expect(synchronizeMock.called).to.be.false;
|
|
|
|
expect(scope.state.mailList.selected).to.equal(mail);
|
|
|
|
|
|
|
|
scope.synchronize.restore();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-11-11 11:56:51 -05:00
|
|
|
describe('remove', function() {
|
|
|
|
it('should not delete without a selected mail', function() {
|
|
|
|
scope.remove();
|
|
|
|
|
2013-12-04 08:15:12 -05:00
|
|
|
expect(emailDaoMock.sync.called).to.be.false;
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
|
2013-11-21 11:37:07 -05:00
|
|
|
it('should not delete from the outbox', function(done) {
|
|
|
|
var currentFolder, mail;
|
|
|
|
|
|
|
|
scope._stopWatchTask();
|
|
|
|
|
2013-12-04 08:15:12 -05:00
|
|
|
scope.account = {};
|
2013-11-11 11:56:51 -05:00
|
|
|
mail = {
|
2013-12-04 08:15:12 -05:00
|
|
|
uid: 123,
|
2013-11-11 11:56:51 -05:00
|
|
|
from: [{
|
|
|
|
address: 'asd'
|
|
|
|
}],
|
|
|
|
subject: '[whiteout] asdasd',
|
|
|
|
unread: true
|
|
|
|
};
|
|
|
|
currentFolder = {
|
2013-12-04 08:15:12 -05:00
|
|
|
type: 'Outbox',
|
|
|
|
path: 'OUTBOX',
|
|
|
|
messages: [mail]
|
2013-11-11 11:56:51 -05:00
|
|
|
};
|
2013-12-04 08:15:12 -05:00
|
|
|
|
|
|
|
scope.emails = [mail];
|
|
|
|
scope.account.folders = [currentFolder];
|
2013-11-11 11:56:51 -05:00
|
|
|
scope.state.nav = {
|
|
|
|
currentFolder: currentFolder
|
|
|
|
};
|
2013-12-04 08:15:12 -05:00
|
|
|
|
|
|
|
scope.onError = function(err) {
|
|
|
|
expect(err).to.exist; // would normally display the notification
|
|
|
|
expect(emailDaoMock.sync.called).to.be.false;
|
|
|
|
done();
|
|
|
|
};
|
2013-11-11 11:56:51 -05:00
|
|
|
|
|
|
|
scope.remove(mail);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-12-04 08:15:12 -05:00
|
|
|
it('should delete the selected mail', function() {
|
|
|
|
var uid, mail, currentFolder;
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2013-11-21 11:37:07 -05:00
|
|
|
scope._stopWatchTask();
|
|
|
|
|
2013-12-04 08:15:12 -05:00
|
|
|
scope.account = {};
|
2013-11-11 11:56:51 -05:00
|
|
|
uid = 123;
|
|
|
|
mail = {
|
|
|
|
uid: uid,
|
|
|
|
from: [{
|
|
|
|
address: 'asd'
|
|
|
|
}],
|
|
|
|
subject: '[whiteout] asdasd',
|
|
|
|
unread: true
|
|
|
|
};
|
|
|
|
currentFolder = {
|
|
|
|
type: 'Inbox',
|
2013-12-04 08:15:12 -05:00
|
|
|
path: 'INBOX',
|
|
|
|
messages: [mail]
|
2013-11-11 11:56:51 -05:00
|
|
|
};
|
2013-12-04 08:15:12 -05:00
|
|
|
scope.account.folders = [currentFolder];
|
2013-11-11 11:56:51 -05:00
|
|
|
scope.state.nav = {
|
|
|
|
currentFolder: currentFolder
|
|
|
|
};
|
2013-12-04 08:15:12 -05:00
|
|
|
emailDaoMock.sync.yields();
|
2013-11-11 11:56:51 -05:00
|
|
|
|
|
|
|
scope.remove(mail);
|
|
|
|
|
2013-12-04 08:15:12 -05:00
|
|
|
expect(emailDaoMock.sync.calledOnce).to.be.true;
|
2013-11-11 11:56:51 -05:00
|
|
|
expect(scope.state.mailList.selected).to.not.exist;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|