mail/test/unit/controller/app/mail-list-ctrl-test.js

330 lines
10 KiB
JavaScript
Raw Normal View History

2014-10-07 14:32:23 -04:00
'use strict';
2014-10-09 09:56:35 -04:00
var mocks = angular.mock,
MailListCtrl = require('../../../../src/js/controller/app/mail-list'),
EmailDAO = require('../../../../src/js/email/email'),
KeychainDAO = require('../../../../src/js/service/keychain'),
StatusDisplay = require('../../../../src/js/util/status-display'),
Dialog = require('../../../../src/js/util/dialog'),
Search = require('../../../../src/js/email/search');
2014-10-07 14:32:23 -04:00
2014-11-05 08:27:34 -05:00
chai.config.includeStack = true;
2014-10-07 14:32:23 -04:00
describe('Mail List controller unit test', function() {
var scope, ctrl, statusDisplayMock, notificationMock, emailMock, keychainMock, dialogMock, searchMock,
2014-10-07 14:32:23 -04:00
emailAddress, emails,
hasChrome, hasSocket, hasRuntime, hasIdentity;
beforeEach(function() {
hasChrome = !!window.chrome;
hasSocket = !!window.chrome.socket;
hasIdentity = !!window.chrome.identity;
if (!hasChrome) {
window.chrome = {};
}
if (!hasSocket) {
window.chrome.socket = {};
}
if (!hasRuntime) {
window.chrome.runtime = {
getURL: function() {}
};
2014-10-07 14:32:23 -04:00
}
if (!hasIdentity) {
window.chrome.identity = {};
}
emails = [{
unread: true
}, {
unread: true
}, {
unread: true
}];
emailAddress = 'fred@foo.com';
notificationMock = {
create: function() {},
close: function() {}
};
2014-10-07 14:32:23 -04:00
statusDisplayMock = sinon.createStubInstance(StatusDisplay);
emailMock = sinon.createStubInstance(EmailDAO);
2014-10-07 14:32:23 -04:00
keychainMock = sinon.createStubInstance(KeychainDAO);
dialogMock = sinon.createStubInstance(Dialog);
searchMock = sinon.createStubInstance(Search);
2014-10-07 14:32:23 -04:00
angular.module('maillisttest', ['woEmail', 'woServices', 'woUtil']);
2014-10-07 14:32:23 -04:00
mocks.module('maillisttest');
mocks.inject(function($rootScope, $controller) {
scope = $rootScope.$new();
scope.state = {
read: {
toggle: function() {}
}
};
2014-10-07 14:32:23 -04:00
scope.loadVisibleBodies = function() {};
ctrl = $controller(MailListCtrl, {
$scope: scope,
$routeParams: {},
statusDisplay: statusDisplayMock,
notification: notificationMock,
email: emailMock,
keychain: keychainMock,
dialog: dialogMock,
search: searchMock
2014-10-07 14:32:23 -04:00
});
});
});
2014-10-07 14:32:23 -04:00
afterEach(function() {
if (!hasSocket) {
delete window.chrome.socket;
}
if (!hasRuntime) {
delete window.chrome.runtime;
}
if (!hasChrome) {
delete window.chrome;
}
if (!hasIdentity) {
delete window.chrome.identity;
}
});
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'];
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-10-07 14:32:23 -04:00
scope.displayMore();
expect(scope.displayMessages.length).to.equal(scope.state.nav.currentFolder.messages.length);
});
2014-10-07 14:32:23 -04:00
});
describe('displaySearchResults', function() {
var clock;
2014-10-07 14:32:23 -04:00
beforeEach(function() {
scope.state.nav = {
currentFolder: {
messages: ['a', 'b']
}
};
scope.watchMessages();
scope.watchOnline();
clock = sinon.useFakeTimers();
});
afterEach(function() {
2014-10-07 14:32:23 -04:00
clock.restore();
});
2014-10-07 14:32:23 -04:00
it('should show initial message on empty', function() {
scope.displaySearchResults();
expect(statusDisplayMock.setSearching.withArgs(false).calledOnce).to.be.true;
expect(statusDisplayMock.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() {
searchMock.filter.returns(['a']);
2014-10-07 14:32:23 -04:00
scope.displaySearchResults('query');
expect(statusDisplayMock.setSearching.withArgs(true).calledOnce).to.be.true;
expect(statusDisplayMock.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']);
expect(statusDisplayMock.setSearching.withArgs(false).calledOnce).to.be.true;
expect(statusDisplayMock.update.withArgs('Matches in this folder').calledOnce).to.be.true;
2014-10-07 14:32:23 -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-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('push notification', function() {
beforeEach(function() {
scope._stopWatchTask();
});
2014-10-07 14:32:23 -04:00
afterEach(function() {
notificationMock.create.restore();
2014-10-07 14:32:23 -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
};
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-10-07 14:32:23 -04:00
opts.onClick();
expect(scope.state.mailList.selected).to.equal(mail);
done();
});
2014-10-07 14:32:23 -04:00
scope.state.nav = {
currentFolder: {
type: 'asd',
messages: [mail]
}
};
emailMock.onIncomingMessage([mail]);
});
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
}];
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);
2014-10-07 14:32:23 -04:00
opts.onClick();
expect(scope.state.mailList.selected).to.equal(mails[0]);
done();
});
2014-10-07 14:32:23 -04:00
scope.state.nav = {
currentFolder: {
type: 'asd',
messages: mails
}
};
emailMock.onIncomingMessage(mails);
2014-10-07 14:32:23 -04: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-10-07 14:32:23 -04:00
scope.getBody();
expect(emailMock.getBody.calledOnce).to.be.true;
});
2014-10-07 14:32:23 -04:00
});
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'];
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() {}
},
actionBar: {
markMessage: function() {}
2014-10-07 14:32:23 -04:00
}
};
2014-02-17 08:31: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
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);
expect(notificationMock.close.calledWith('asd')).to.be.true;
expect(notificationMock.close.calledOnce).to.be.true;
2014-02-17 08:31:14 -05:00
notificationMock.close.restore();
2014-10-07 14:32:23 -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
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
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
});
});