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'),
|
2014-04-28 12:09:51 -04:00
|
|
|
appController = require('js/app-controller'),
|
|
|
|
notification = require('js/util/notification');
|
2013-11-11 11:56:51 -05:00
|
|
|
|
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() {
|
2014-05-23 08:23:50 -04:00
|
|
|
var scope, ctrl, origEmailDao, emailDaoMock, keychainMock, deviceStorageMock,
|
2013-11-21 11:37:07 -05:00
|
|
|
emailAddress, notificationClickedHandler, emails,
|
2014-04-28 12:09:51 -04:00
|
|
|
hasChrome, hasSocket, hasRuntime, hasIdentity;
|
2013-11-11 11:56:51 -05:00
|
|
|
|
|
|
|
beforeEach(function() {
|
2014-05-23 04:52:34 -04:00
|
|
|
hasChrome = !!window.chrome;
|
|
|
|
hasSocket = !!window.chrome.socket;
|
|
|
|
hasIdentity = !!window.chrome.identity;
|
2013-11-11 11:56:51 -05:00
|
|
|
if (!hasChrome) {
|
|
|
|
window.chrome = {};
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2014-04-28 12:09:51 -04:00
|
|
|
sinon.stub(notification, 'setOnClickedListener', function(func) {
|
|
|
|
notificationClickedHandler = func;
|
|
|
|
});
|
|
|
|
|
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);
|
2014-05-23 04:52:34 -04:00
|
|
|
appController._keychain = keychainMock;
|
2013-11-11 11:56:51 -05:00
|
|
|
|
|
|
|
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() {}
|
|
|
|
}
|
|
|
|
};
|
2014-03-12 12:02:41 -04:00
|
|
|
|
|
|
|
scope.loadVisibleBodies = function() {};
|
2013-11-11 11:56:51 -05:00
|
|
|
ctrl = $controller(MailListCtrl, {
|
|
|
|
$scope: scope
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
2014-04-28 12:09:51 -04:00
|
|
|
notification.setOnClickedListener.restore();
|
|
|
|
|
2013-11-11 11:56:51 -05:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
2014-07-16 06:47:25 -04:00
|
|
|
describe('displayMore', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
scope.state.nav = {
|
|
|
|
currentFolder: {
|
|
|
|
messages: ['a', 'b']
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
it('should not do anything when display length equals messages length', function() {
|
|
|
|
scope.displayMessages = ['a', 'b'];
|
|
|
|
|
|
|
|
scope.displayMore();
|
|
|
|
expect(scope.displayMessages.length).to.equal(scope.state.nav.currentFolder.messages.length);
|
|
|
|
});
|
|
|
|
it('should append next message interval', function() {
|
|
|
|
scope.displayMessages = ['a'];
|
|
|
|
|
|
|
|
scope.displayMore();
|
|
|
|
expect(scope.displayMessages.length).to.equal(scope.state.nav.currentFolder.messages.length);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('displaySearchResults', function() {
|
|
|
|
var clock;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
scope.state.nav = {
|
|
|
|
currentFolder: {
|
|
|
|
messages: ['a', 'b']
|
|
|
|
}
|
|
|
|
};
|
|
|
|
scope.watchMessages();
|
|
|
|
scope.watchOnline();
|
|
|
|
clock = sinon.useFakeTimers();
|
|
|
|
});
|
|
|
|
afterEach(function() {
|
|
|
|
clock.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show initial message on empty', function() {
|
|
|
|
scope.displaySearchResults();
|
|
|
|
expect(scope.searching).to.be.false;
|
|
|
|
expect(scope.lastUpdateLbl).to.equal('Online');
|
|
|
|
expect(scope.displayMessages.length).to.equal(2);
|
|
|
|
});
|
|
|
|
it('should show initial message on empty', function() {
|
|
|
|
var searchStub = sinon.stub(scope, 'search');
|
|
|
|
searchStub.returns(['a']);
|
|
|
|
|
|
|
|
|
|
|
|
scope.displaySearchResults('query');
|
|
|
|
expect(scope.searching).to.be.true;
|
|
|
|
expect(scope.lastUpdateLbl).to.equal('Searching ...');
|
|
|
|
clock.tick(500);
|
|
|
|
|
|
|
|
expect(scope.displayMessages).to.deep.equal(['a']);
|
|
|
|
expect(scope.searching).to.be.false;
|
|
|
|
expect(scope.lastUpdateLbl).to.equal('Matches in this folder');
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('search', function() {
|
|
|
|
var message1 = {
|
|
|
|
to: [{
|
|
|
|
name: 'name1',
|
|
|
|
address: 'address1'
|
|
|
|
}],
|
|
|
|
subject: 'subject1',
|
|
|
|
body: 'body1',
|
|
|
|
html: 'html1'
|
|
|
|
},
|
|
|
|
message2 = {
|
|
|
|
to: [{
|
|
|
|
name: 'name2',
|
|
|
|
address: 'address2'
|
|
|
|
}],
|
|
|
|
subject: 'subject2',
|
|
|
|
body: 'body2',
|
|
|
|
html: 'html2'
|
|
|
|
},
|
|
|
|
message3 = {
|
|
|
|
to: [{
|
|
|
|
name: 'name3',
|
|
|
|
address: 'address3'
|
|
|
|
}],
|
|
|
|
subject: 'subject3',
|
|
|
|
body: 'body1',
|
|
|
|
html: 'html1',
|
|
|
|
encrypted: true
|
|
|
|
},
|
|
|
|
message4 = {
|
|
|
|
to: [{
|
|
|
|
name: 'name4',
|
|
|
|
address: 'address4'
|
|
|
|
}],
|
|
|
|
subject: 'subject4',
|
|
|
|
body: 'body1',
|
|
|
|
html: 'html1',
|
|
|
|
encrypted: true,
|
|
|
|
decrypted: true
|
|
|
|
},
|
|
|
|
testMessages = [message1, message2, message3, message4];
|
|
|
|
|
|
|
|
it('return same messages array on empty query string', function() {
|
|
|
|
var result = scope.search(testMessages, '');
|
|
|
|
expect(result).to.equal(testMessages);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('return message1 on matching subject', function() {
|
|
|
|
var result = scope.search(testMessages, 'subject1');
|
|
|
|
expect(result.length).to.equal(1);
|
|
|
|
expect(result[0]).to.equal(message1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('return message1 on matching name', function() {
|
|
|
|
var result = scope.search(testMessages, 'name1');
|
|
|
|
expect(result.length).to.equal(1);
|
|
|
|
expect(result[0]).to.equal(message1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('return message1 on matching address', function() {
|
|
|
|
var result = scope.search(testMessages, 'address1');
|
|
|
|
expect(result.length).to.equal(1);
|
|
|
|
expect(result[0]).to.equal(message1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('return plaintext and decrypted messages on matching body', function() {
|
|
|
|
var result = scope.search(testMessages, 'body1');
|
|
|
|
expect(result.length).to.equal(2);
|
|
|
|
expect(result[0]).to.equal(message1);
|
|
|
|
expect(result[1]).to.equal(message4);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('return plaintext and decrypted messages on matching html', function() {
|
|
|
|
var result = scope.search(testMessages, 'html1');
|
|
|
|
expect(result.length).to.equal(2);
|
|
|
|
expect(result[0]).to.equal(message1);
|
|
|
|
expect(result[1]).to.equal(message4);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-11-11 11:56:51 -05:00
|
|
|
describe('scope variables', function() {
|
|
|
|
it('should be set correctly', function() {
|
|
|
|
expect(scope.select).to.exist;
|
|
|
|
expect(scope.remove).to.exist;
|
|
|
|
expect(scope.state.mailList).to.exist;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('push notification', function() {
|
2014-04-28 12:09:51 -04:00
|
|
|
beforeEach(function() {
|
2013-11-21 11:37:07 -05:00
|
|
|
scope._stopWatchTask();
|
2014-04-28 12:09:51 -04:00
|
|
|
});
|
2013-11-21 11:37:07 -05:00
|
|
|
|
2014-04-28 12:09:51 -04:00
|
|
|
it('should succeed for single mail', function(done) {
|
|
|
|
var mail = {
|
|
|
|
uid: 123,
|
2013-11-11 11:56:51 -05:00
|
|
|
from: [{
|
|
|
|
address: 'asd'
|
|
|
|
}],
|
2014-04-28 12:09:51 -04:00
|
|
|
subject: 'this is the subject!',
|
2013-11-11 11:56:51 -05:00
|
|
|
unread: true
|
|
|
|
};
|
2014-04-28 12:09:51 -04:00
|
|
|
|
|
|
|
sinon.stub(notification, 'create', function(opts) {
|
|
|
|
expect(opts.id).to.equal('' + mail.uid);
|
|
|
|
expect(opts.title).to.equal(mail.from[0].address);
|
|
|
|
expect(opts.message).to.equal(mail.subject);
|
|
|
|
|
|
|
|
notification.create.restore();
|
2013-11-11 11:56:51 -05:00
|
|
|
done();
|
2014-04-28 12:09:51 -04:00
|
|
|
});
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-05-23 08:23:50 -04:00
|
|
|
emailDaoMock.onIncomingMessage([mail]);
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
|
2014-04-28 12:09:51 -04:00
|
|
|
it('should succeed for multiple mails', function(done) {
|
|
|
|
var mails = [{
|
|
|
|
uid: 1,
|
|
|
|
from: [{
|
|
|
|
address: 'asd'
|
|
|
|
}],
|
|
|
|
subject: 'this is the subject!',
|
|
|
|
unread: true
|
|
|
|
}, {
|
|
|
|
uid: 2,
|
|
|
|
from: [{
|
|
|
|
address: 'qwe'
|
|
|
|
}],
|
|
|
|
subject: 'this is the other subject!',
|
|
|
|
unread: true
|
|
|
|
}, {
|
|
|
|
uid: 3,
|
|
|
|
from: [{
|
|
|
|
address: 'qwe'
|
|
|
|
}],
|
|
|
|
subject: 'this is the other subject!',
|
|
|
|
unread: false
|
|
|
|
}];
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-04-28 12:09:51 -04:00
|
|
|
sinon.stub(notification, 'create', function(opts) {
|
|
|
|
expect(opts.id).to.equal('' + mails[0].uid);
|
|
|
|
expect(opts.title).to.equal('2 new messages');
|
|
|
|
expect(opts.message).to.equal(mails[0].subject + '\n' + mails[1].subject);
|
2013-11-21 11:37:07 -05:00
|
|
|
|
2014-04-28 12:09:51 -04:00
|
|
|
notification.create.restore();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2014-05-23 08:23:50 -04:00
|
|
|
emailDaoMock.onIncomingMessage(mails);
|
2014-04-28 12:09:51 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should focus mail when clicked', function() {
|
|
|
|
var mail = {
|
2013-12-04 08:15:12 -05:00
|
|
|
uid: 123,
|
2013-11-11 11:56:51 -05:00
|
|
|
from: [{
|
|
|
|
address: 'asd'
|
|
|
|
}],
|
2014-04-28 12:09:51 -04:00
|
|
|
subject: 'asdasd',
|
2013-11-11 11:56:51 -05:00
|
|
|
unread: true
|
|
|
|
};
|
2014-04-28 12:09:51 -04:00
|
|
|
|
2013-11-11 11:56:51 -05:00
|
|
|
scope.state.nav = {
|
2014-04-28 12:09:51 -04:00
|
|
|
currentFolder: {
|
|
|
|
type: 'asd',
|
|
|
|
messages: [mail]
|
|
|
|
}
|
2013-11-11 11:56:51 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
});
|
2014-04-28 12:09:51 -04:00
|
|
|
|
|
|
|
it('should not change focus mail when popup id is NaN', function() {
|
|
|
|
scope.state.nav = {
|
|
|
|
currentFolder: {
|
|
|
|
type: 'asd',
|
|
|
|
messages: []
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var focus = scope.state.mailList.selected = {};
|
|
|
|
|
|
|
|
notificationClickedHandler('');
|
|
|
|
expect(scope.state.mailList.selected).to.equal(focus);
|
|
|
|
});
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
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() {
|
2014-05-23 04:52:34 -04:00
|
|
|
it('should decrypt, focus mark an unread mail as read', function() {
|
2014-05-23 08:23:50 -04:00
|
|
|
var mail = {
|
2014-05-23 04:52:34 -04:00
|
|
|
from: [{
|
|
|
|
address: 'asd'
|
|
|
|
}],
|
|
|
|
unread: true,
|
2014-02-17 08:31:14 -05:00
|
|
|
};
|
|
|
|
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() {}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-05-23 04:52:34 -04:00
|
|
|
keychainMock.refreshKeyForUserId.withArgs(mail.from[0].address).yields();
|
|
|
|
|
2014-02-17 08:31:14 -05:00
|
|
|
scope.select(mail);
|
|
|
|
|
2014-02-24 04:14:07 -05:00
|
|
|
expect(emailDaoMock.decryptBody.calledOnce).to.be.true;
|
2014-05-23 04:52:34 -04:00
|
|
|
expect(keychainMock.refreshKeyForUserId.calledOnce).to.be.true;
|
2014-02-17 08:31:14 -05:00
|
|
|
expect(scope.state.mailList.selected).to.equal(mail);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should decrypt and focus a read mail', function() {
|
2014-05-23 08:23:50 -04:00
|
|
|
var mail = {
|
2014-05-23 04:52:34 -04:00
|
|
|
from: [{
|
|
|
|
address: 'asd'
|
|
|
|
}],
|
2014-02-17 08:31:14 -05:00
|
|
|
unread: false
|
|
|
|
};
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-02-17 08:31:14 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-05-23 04:52:34 -04:00
|
|
|
keychainMock.refreshKeyForUserId.withArgs(mail.from[0].address).yields();
|
|
|
|
|
2014-02-17 08:31:14 -05:00
|
|
|
scope.select(mail);
|
|
|
|
|
2014-02-24 04:14:07 -05:00
|
|
|
expect(emailDaoMock.decryptBody.calledOnce).to.be.true;
|
2014-05-23 04:52:34 -04:00
|
|
|
expect(keychainMock.refreshKeyForUserId.calledOnce).to.be.true;
|
2014-02-17 08:31:14 -05:00
|
|
|
expect(scope.state.mailList.selected).to.equal(mail);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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
|
|
|
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
|
|
|
|
};
|
2014-05-23 08:23:50 -04:00
|
|
|
emailDaoMock.deleteMessage.yields();
|
2013-11-11 11:56:51 -05:00
|
|
|
|
|
|
|
scope.remove(mail);
|
|
|
|
|
2014-05-23 08:23:50 -04:00
|
|
|
expect(emailDaoMock.deleteMessage.calledOnce).to.be.true;
|
2013-11-11 11:56:51 -05:00
|
|
|
expect(scope.state.mailList.selected).to.not.exist;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|