2014-10-07 14:32:23 -04:00
|
|
|
'use strict';
|
|
|
|
|
2014-11-26 07:43:10 -05:00
|
|
|
var MailListCtrl = require('../../../../src/js/controller/app/mail-list'),
|
2014-11-26 06:59:44 -05:00
|
|
|
EmailDAO = require('../../../../src/js/email/email'),
|
|
|
|
KeychainDAO = require('../../../../src/js/service/keychain'),
|
2014-12-04 10:24:42 -05:00
|
|
|
Status = require('../../../../src/js/util/status'),
|
2014-11-26 06:59:44 -05:00
|
|
|
Dialog = require('../../../../src/js/util/dialog'),
|
|
|
|
Search = require('../../../../src/js/email/search');
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
describe('Mail List controller unit test', function() {
|
2014-12-04 10:24:42 -05:00
|
|
|
var scope, ctrl, statusMock, notificationMock, emailMock, keychainMock, dialogMock, searchMock,
|
|
|
|
emailAddress, emails, location;
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
emails = [{
|
|
|
|
unread: true
|
|
|
|
}, {
|
|
|
|
unread: true
|
|
|
|
}, {
|
|
|
|
unread: true
|
|
|
|
}];
|
|
|
|
|
|
|
|
emailAddress = 'fred@foo.com';
|
|
|
|
|
2014-11-26 06:59:44 -05:00
|
|
|
notificationMock = {
|
|
|
|
create: function() {},
|
|
|
|
close: function() {}
|
|
|
|
};
|
2014-10-07 14:32:23 -04:00
|
|
|
|
2014-12-04 10:24:42 -05:00
|
|
|
statusMock = sinon.createStubInstance(Status);
|
2014-11-26 06:59:44 -05:00
|
|
|
emailMock = sinon.createStubInstance(EmailDAO);
|
2014-10-07 14:32:23 -04:00
|
|
|
keychainMock = sinon.createStubInstance(KeychainDAO);
|
2014-11-26 06:59:44 -05:00
|
|
|
dialogMock = sinon.createStubInstance(Dialog);
|
|
|
|
searchMock = sinon.createStubInstance(Search);
|
2014-10-07 14:32:23 -04:00
|
|
|
|
2014-11-26 06:59:44 -05:00
|
|
|
angular.module('maillisttest', ['woEmail', 'woServices', 'woUtil']);
|
2014-11-26 07:43:10 -05:00
|
|
|
angular.mock.module('maillisttest');
|
2014-12-04 10:24:42 -05:00
|
|
|
angular.mock.inject(function($rootScope, $controller, $location) {
|
2014-10-07 14:32:23 -04:00
|
|
|
scope = $rootScope.$new();
|
2014-12-04 10:24:42 -05:00
|
|
|
location = $location;
|
|
|
|
scope.state = {};
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.loadVisibleBodies = function() {};
|
|
|
|
ctrl = $controller(MailListCtrl, {
|
|
|
|
$scope: scope,
|
2014-12-04 10:24:42 -05:00
|
|
|
$location: location,
|
|
|
|
status: statusMock,
|
2014-11-26 06:59:44 -05:00
|
|
|
notification: notificationMock,
|
|
|
|
email: emailMock,
|
|
|
|
keychain: keychainMock,
|
|
|
|
dialog: dialogMock,
|
|
|
|
search: searchMock
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-12-04 10:24:42 -05:00
|
|
|
afterEach(function() {});
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -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'];
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.displayMore();
|
|
|
|
expect(scope.displayMessages.length).to.equal(scope.state.nav.currentFolder.messages.length);
|
|
|
|
});
|
|
|
|
it('should append next message interval', function() {
|
|
|
|
scope.displayMessages = ['a'];
|
2014-03-12 12:02:41 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.displayMore();
|
|
|
|
expect(scope.displayMessages.length).to.equal(scope.state.nav.currentFolder.messages.length);
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('displaySearchResults', function() {
|
|
|
|
var clock;
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
beforeEach(function() {
|
|
|
|
scope.state.nav = {
|
|
|
|
currentFolder: {
|
|
|
|
messages: ['a', 'b']
|
|
|
|
}
|
|
|
|
};
|
|
|
|
scope.watchMessages();
|
|
|
|
scope.watchOnline();
|
|
|
|
clock = sinon.useFakeTimers();
|
|
|
|
});
|
2013-11-11 11:56:51 -05:00
|
|
|
afterEach(function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
clock.restore();
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should show initial message on empty', function() {
|
|
|
|
scope.displaySearchResults();
|
2014-12-04 10:24:42 -05:00
|
|
|
expect(statusMock.setSearching.withArgs(false).calledOnce).to.be.true;
|
|
|
|
expect(statusMock.update.withArgs('Online').calledOnce).to.be.true;
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(scope.displayMessages.length).to.equal(2);
|
|
|
|
});
|
|
|
|
it('should show initial message on empty', function() {
|
2014-11-26 06:59:44 -05:00
|
|
|
searchMock.filter.returns(['a']);
|
2014-07-16 06:47:25 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.displaySearchResults('query');
|
2014-12-04 10:24:42 -05:00
|
|
|
expect(statusMock.setSearching.withArgs(true).calledOnce).to.be.true;
|
|
|
|
expect(statusMock.update.withArgs('Searching ...').calledOnce).to.be.true;
|
2014-10-07 14:32:23 -04:00
|
|
|
clock.tick(500);
|
|
|
|
|
|
|
|
expect(scope.displayMessages).to.deep.equal(['a']);
|
2014-12-04 10:24:42 -05:00
|
|
|
expect(statusMock.setSearching.withArgs(false).calledOnce).to.be.true;
|
|
|
|
expect(statusMock.update.withArgs('Matches in this folder').calledOnce).to.be.true;
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
|
|
|
});
|
2014-07-16 06:47:25 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('scope variables', function() {
|
|
|
|
it('should be set correctly', function() {
|
|
|
|
expect(scope.select).to.exist;
|
|
|
|
expect(scope.state.mailList).to.exist;
|
2014-07-16 06:47:25 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-07-16 06:47:25 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('push notification', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
scope._stopWatchTask();
|
2014-12-04 10:24:42 -05:00
|
|
|
sinon.stub(scope, 'navigate');
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-07-16 06:47:25 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
afterEach(function() {
|
2014-11-26 06:59:44 -05:00
|
|
|
notificationMock.create.restore();
|
2014-12-04 10:24:42 -05:00
|
|
|
scope.navigate.restore();
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-07-16 06:47:25 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should succeed for single mail', function(done) {
|
|
|
|
var mail = {
|
|
|
|
uid: 123,
|
|
|
|
from: [{
|
|
|
|
address: 'asd'
|
|
|
|
}],
|
|
|
|
subject: 'this is the subject!',
|
|
|
|
unread: true
|
|
|
|
};
|
2014-07-16 06:47:25 -04:00
|
|
|
|
2014-11-26 06:59:44 -05:00
|
|
|
sinon.stub(notificationMock, 'create', function(opts) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(opts.title).to.equal(mail.from[0].address);
|
|
|
|
expect(opts.message).to.equal(mail.subject);
|
2014-07-16 06:47:25 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
opts.onClick();
|
2014-12-04 10:24:42 -05:00
|
|
|
expect(scope.navigate.withArgs(mail).calledOnce).to.be.true;
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-07-16 06:47:25 -04:00
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.state.nav = {
|
|
|
|
currentFolder: {
|
|
|
|
type: 'asd',
|
|
|
|
messages: [mail]
|
|
|
|
}
|
|
|
|
};
|
2014-07-16 06:47:25 -04:00
|
|
|
|
2014-11-26 06:59:44 -05:00
|
|
|
emailMock.onIncomingMessage([mail]);
|
2014-07-16 06:47:25 -04:00
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -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-11-26 06:59:44 -05:00
|
|
|
sinon.stub(notificationMock, 'create', function(opts) {
|
2014-10-07 14:32:23 -04:00
|
|
|
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-10-07 14:32:23 -04:00
|
|
|
opts.onClick();
|
2014-12-04 10:24:42 -05:00
|
|
|
expect(scope.navigate.withArgs(mails[0]).calledOnce).to.be.true;
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-09-16 13:01:49 -04:00
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.state.nav = {
|
|
|
|
currentFolder: {
|
|
|
|
type: 'asd',
|
|
|
|
messages: mails
|
|
|
|
}
|
|
|
|
};
|
2014-09-16 13:01:49 -04:00
|
|
|
|
2014-11-26 06:59:44 -05:00
|
|
|
emailMock.onIncomingMessage(mails);
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
|
|
|
});
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('getBody', function() {
|
|
|
|
it('should get the mail content', function() {
|
|
|
|
scope.state.nav = {
|
|
|
|
currentFolder: {
|
|
|
|
type: 'asd',
|
|
|
|
}
|
|
|
|
};
|
2014-04-28 12:09:51 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.getBody();
|
2014-11-26 06:59:44 -05:00
|
|
|
expect(emailMock.getBody.calledOnce).to.be.true;
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-11-21 11:37:07 -05:00
|
|
|
|
2014-12-02 12:36:15 -05:00
|
|
|
describe('flag', function() {
|
|
|
|
it('should flag or unflag a message', function() {
|
|
|
|
var mail = {
|
|
|
|
from: [{
|
|
|
|
address: 'asd'
|
|
|
|
}],
|
|
|
|
flagged: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
scope.state = {
|
|
|
|
actionBar: {
|
|
|
|
flagMessage: function(mail, flagged) {
|
|
|
|
mail.flagged = flagged;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
scope.flag(mail, false);
|
|
|
|
expect(mail.flagged).to.be.false;
|
|
|
|
|
|
|
|
scope.flag(mail, true);
|
|
|
|
expect(mail.flagged).to.be.true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('select', function() {
|
|
|
|
it('should decrypt, focus mark an unread mail as read', function() {
|
|
|
|
scope.pendingNotifications = ['asd'];
|
2014-11-26 06:59:44 -05:00
|
|
|
sinon.stub(notificationMock, 'close');
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
var mail = {
|
|
|
|
from: [{
|
|
|
|
address: 'asd'
|
|
|
|
}],
|
|
|
|
unread: true,
|
|
|
|
};
|
|
|
|
scope.state = {
|
|
|
|
nav: {
|
2014-02-17 08:31:14 -05:00
|
|
|
currentFolder: {
|
2014-10-07 14:32:23 -04:00
|
|
|
type: 'Inbox'
|
2014-02-17 08:31:14 -05:00
|
|
|
}
|
2014-10-07 14:32:23 -04:00
|
|
|
},
|
|
|
|
mailList: {},
|
|
|
|
read: {
|
|
|
|
toggle: function() {}
|
2014-11-05 08:26:09 -05:00
|
|
|
},
|
|
|
|
actionBar: {
|
|
|
|
markMessage: function() {}
|
2014-10-07 14:32:23 -04:00
|
|
|
}
|
|
|
|
};
|
2014-02-17 08:31:14 -05:00
|
|
|
|
2014-11-06 08:28:14 -05:00
|
|
|
keychainMock.refreshKeyForUserId.withArgs({
|
|
|
|
userId: mail.from[0].address
|
|
|
|
}).yields();
|
2014-02-17 08:31:14 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.select(mail);
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-11-26 06:59:44 -05:00
|
|
|
expect(emailMock.decryptBody.calledOnce).to.be.true;
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(keychainMock.refreshKeyForUserId.calledOnce).to.be.true;
|
|
|
|
expect(scope.state.mailList.selected).to.equal(mail);
|
2014-11-26 06:59:44 -05:00
|
|
|
expect(notificationMock.close.calledWith('asd')).to.be.true;
|
|
|
|
expect(notificationMock.close.calledOnce).to.be.true;
|
2014-02-17 08:31:14 -05:00
|
|
|
|
2014-11-26 06:59:44 -05:00
|
|
|
notificationMock.close.restore();
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-09-16 13:01:49 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should decrypt and focus a read mail', function() {
|
|
|
|
var mail = {
|
|
|
|
from: [{
|
|
|
|
address: 'asd'
|
|
|
|
}],
|
|
|
|
unread: false
|
|
|
|
};
|
2014-02-17 08:31:14 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.state = {
|
|
|
|
mailList: {},
|
|
|
|
read: {
|
|
|
|
toggle: function() {}
|
|
|
|
},
|
|
|
|
nav: {
|
|
|
|
currentFolder: {
|
|
|
|
type: 'asd'
|
2014-02-17 08:31:14 -05:00
|
|
|
}
|
2014-10-07 14:32:23 -04:00
|
|
|
}
|
|
|
|
};
|
2014-02-17 08:31:14 -05:00
|
|
|
|
2014-11-05 08:26:09 -05:00
|
|
|
keychainMock.refreshKeyForUserId.withArgs({
|
|
|
|
userId: mail.from[0].address
|
|
|
|
}).yields();
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.select(mail);
|
2014-02-17 08:31:14 -05:00
|
|
|
|
2014-11-26 06:59:44 -05:00
|
|
|
expect(emailMock.decryptBody.calledOnce).to.be.true;
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(keychainMock.refreshKeyForUserId.calledOnce).to.be.true;
|
|
|
|
expect(scope.state.mailList.selected).to.equal(mail);
|
2014-02-17 08:31:14 -05:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-12-01 04:04:20 -05:00
|
|
|
|
|
|
|
describe('formatDate', function() {
|
|
|
|
it('should output short time if date is today', angular.mock.inject(function($filter) {
|
|
|
|
var now = new Date();
|
|
|
|
var expected = $filter('date')(now, 'shortTime');
|
|
|
|
expect(scope.formatDate(now)).to.equal(expected);
|
|
|
|
}));
|
|
|
|
it('should output date only if date is not today', angular.mock.inject(function($filter) {
|
|
|
|
var yesterday = new Date();
|
2014-12-04 10:24:42 -05:00
|
|
|
yesterday.setTime(yesterday.getTime() - 24 * 60 * 60 * 1000);
|
2014-12-01 04:04:20 -05:00
|
|
|
|
|
|
|
var expected = $filter('date')(yesterday, 'mediumDate');
|
|
|
|
expect(scope.formatDate(yesterday)).to.equal(expected);
|
|
|
|
}));
|
|
|
|
});
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|