Fix last unit tests

This commit is contained in:
Tankred Hase 2014-11-26 13:43:10 +01:00
parent 03b2e10bc3
commit 2b6da522c6
6 changed files with 68 additions and 83 deletions

View File

@ -196,8 +196,8 @@ module.exports = function(grunt) {
'test/unit/controller/app/read-ctrl-test.js', 'test/unit/controller/app/read-ctrl-test.js',
'test/unit/controller/app/navigation-ctrl-test.js', 'test/unit/controller/app/navigation-ctrl-test.js',
'test/unit/controller/app/mail-list-ctrl-test.js', 'test/unit/controller/app/mail-list-ctrl-test.js',
/*'test/unit/write-ctrl-test.js', 'test/unit/controller/app/write-ctrl-test.js',
'test/unit/action-bar-ctrl-test.js',*/ 'test/unit/controller/app/action-bar-ctrl-test.js',
] ]
}, },
options: browserifyOpt options: browserifyOpt

View File

@ -1,13 +1,12 @@
'use strict'; 'use strict';
var axe = require('axe-logger'), var util = require('crypto-lib').util;
util = require('crypto-lib').util;
// //
// Controller // Controller
// //
var WriteCtrl = function($scope, $filter, $q, appConfig, auth, keychain, pgp, email, outbox, dialog) { var WriteCtrl = function($scope, $filter, $q, appConfig, auth, keychain, pgp, email, outbox, dialog, axe) {
var str = appConfig.string; var str = appConfig.string;

View File

@ -50,6 +50,8 @@ if (!Function.prototype.bind) {
// Test setup // Test setup
// //
chai.config.includeStack = true;
// set worker path for tests // set worker path for tests
require('../src/js/app-config').config.workerPath = '../lib'; require('../src/js/app-config').config.workerPath = '../lib';

View File

@ -1,21 +1,21 @@
'use strict'; 'use strict';
var mocks = angular.mock, var Email = require('../../../../src/js/email/email'),
EmailDAO = require('../../src/js/dao/email-dao'), Dialog = require('../../../../src/js/util/dialog'),
appController = require('../../src/js/app-controller'), StatusDisplay = require('../../../../src/js/util/status-display'),
ActionBarCtrl = require('../../src/js/controller/action-bar'); ActionBarCtrl = require('../../../../src/js/controller/app/action-bar');
describe('Action Bar Controller unit test', function() { describe('Action Bar Controller unit test', function() {
var scope, actionBarCtrl, emailDaoMock, origEmailDao; var scope, actionBarCtrl, emailMock, dialogMock, statusDisplayMock;
beforeEach(function() { beforeEach(function() {
origEmailDao = appController._emailDao; emailMock = sinon.createStubInstance(Email);
emailDaoMock = sinon.createStubInstance(EmailDAO); dialogMock = sinon.createStubInstance(Dialog);
appController._emailDao = emailDaoMock; statusDisplayMock = sinon.createStubInstance(StatusDisplay);
angular.module('actionbartest', []); angular.module('actionbartest', []);
mocks.module('actionbartest'); angular.mock.module('actionbartest');
mocks.inject(function($rootScope, $controller) { angular.mock.inject(function($rootScope, $controller) {
scope = $rootScope.$new(); scope = $rootScope.$new();
scope.state = { scope.state = {
mailList: { mailList: {
@ -40,15 +40,15 @@ describe('Action Bar Controller unit test', function() {
}; };
actionBarCtrl = $controller(ActionBarCtrl, { actionBarCtrl = $controller(ActionBarCtrl, {
$scope: scope $scope: scope,
email: emailMock,
dialog: dialogMock,
statusDisplay: statusDisplayMock
}); });
}); });
}); });
afterEach(function() { afterEach(function() {});
// restore the module
appController._emailDao = origEmailDao;
});
describe('deleteMessage', function() { describe('deleteMessage', function() {
it('should not delete without a selected mail', function() { it('should not delete without a selected mail', function() {
@ -56,11 +56,11 @@ describe('Action Bar Controller unit test', function() {
}); });
it('should delete the selected mail', function() { it('should delete the selected mail', function() {
emailDaoMock.deleteMessage.yields(); emailMock.deleteMessage.yields();
scope.deleteMessage({}); scope.deleteMessage({});
expect(emailDaoMock.deleteMessage.calledOnce).to.be.true; expect(emailMock.deleteMessage.calledOnce).to.be.true;
expect(scope.state.read.open).to.be.false; expect(scope.state.read.open).to.be.false;
}); });
}); });
@ -88,11 +88,11 @@ describe('Action Bar Controller unit test', function() {
}); });
it('should move the selected mail', function() { it('should move the selected mail', function() {
emailDaoMock.moveMessage.yields(); emailMock.moveMessage.yields();
scope.moveMessage({}, {}); scope.moveMessage({}, {});
expect(emailDaoMock.moveMessage.calledOnce).to.be.true; expect(emailMock.moveMessage.calledOnce).to.be.true;
expect(scope.state.read.open).to.be.false; expect(scope.state.read.open).to.be.false;
}); });
}); });
@ -120,11 +120,11 @@ describe('Action Bar Controller unit test', function() {
}); });
it('should move the selected mail', function() { it('should move the selected mail', function() {
emailDaoMock.setFlags.yields(); emailMock.setFlags.yields();
scope.markMessage({}, true); scope.markMessage({}, true);
expect(emailDaoMock.setFlags.calledOnce).to.be.true; expect(emailMock.setFlags.calledOnce).to.be.true;
expect(scope.state.read.open).to.be.false; expect(scope.state.read.open).to.be.false;
}); });
}); });

View File

@ -1,15 +1,12 @@
'use strict'; 'use strict';
var mocks = angular.mock, var MailListCtrl = require('../../../../src/js/controller/app/mail-list'),
MailListCtrl = require('../../../../src/js/controller/app/mail-list'),
EmailDAO = require('../../../../src/js/email/email'), EmailDAO = require('../../../../src/js/email/email'),
KeychainDAO = require('../../../../src/js/service/keychain'), KeychainDAO = require('../../../../src/js/service/keychain'),
StatusDisplay = require('../../../../src/js/util/status-display'), StatusDisplay = require('../../../../src/js/util/status-display'),
Dialog = require('../../../../src/js/util/dialog'), Dialog = require('../../../../src/js/util/dialog'),
Search = require('../../../../src/js/email/search'); Search = require('../../../../src/js/email/search');
chai.config.includeStack = true;
describe('Mail List controller unit test', function() { describe('Mail List controller unit test', function() {
var scope, ctrl, statusDisplayMock, notificationMock, emailMock, keychainMock, dialogMock, searchMock, var scope, ctrl, statusDisplayMock, notificationMock, emailMock, keychainMock, dialogMock, searchMock,
emailAddress, emails, emailAddress, emails,
@ -56,8 +53,8 @@ describe('Mail List controller unit test', function() {
searchMock = sinon.createStubInstance(Search); searchMock = sinon.createStubInstance(Search);
angular.module('maillisttest', ['woEmail', 'woServices', 'woUtil']); angular.module('maillisttest', ['woEmail', 'woServices', 'woUtil']);
mocks.module('maillisttest'); angular.mock.module('maillisttest');
mocks.inject(function($rootScope, $controller) { angular.mock.inject(function($rootScope, $controller) {
scope = $rootScope.$new(); scope = $rootScope.$new();
scope.state = { scope.state = {
read: { read: {

View File

@ -1,57 +1,50 @@
'use strict'; 'use strict';
var mocks = angular.mock, var WriteCtrl = require('../../../../src/js/controller/app/write'),
WriteCtrl = require('../../src/js/controller/write'), Email = require('../../../../src/js/email/email'),
EmailDAO = require('../../src/js/dao/email-dao'), Outbox = require('../../../../src/js/email/outbox'),
OutboxBO = require('../../src/js/bo/outbox'), Keychain = require('../../../../src/js/service/keychain'),
KeychainDAO = require('../../src/js/dao/keychain-dao'), Auth = require('../../../../src/js/service/auth'),
appController = require('../../src/js/app-controller'); PGP = require('../../../../src/js/crypto/pgp'),
Dialog = require('../../../../src/js/util/dialog');
describe('Write controller unit test', function() { describe('Write controller unit test', function() {
var ctrl, scope, var ctrl, scope,
origEmailDao, origOutbox, origKeychain, authMock, pgpMock, dialogMock, emailMock, keychainMock, outboxMock,
emailDaoMock, keychainMock, outboxMock, emailAddress, realname; emailAddress, realname;
beforeEach(function() { beforeEach(function() {
// the app controller is a singleton, we need to remember the
// outbox and email dao to restore it after the tests
origEmailDao = appController._emailDao;
origOutbox = appController._outboxBo;
origKeychain = appController._keychain;
outboxMock = sinon.createStubInstance(OutboxBO); authMock = sinon.createStubInstance(Auth);
appController._outboxBo = outboxMock; pgpMock = sinon.createStubInstance(PGP);
dialogMock = sinon.createStubInstance(Dialog);
emailDaoMock = sinon.createStubInstance(EmailDAO); outboxMock = sinon.createStubInstance(Outbox);
appController._emailDao = emailDaoMock; emailMock = sinon.createStubInstance(Email);
keychainMock = sinon.createStubInstance(Keychain);
emailAddress = 'fred@foo.com'; emailAddress = 'fred@foo.com';
realname = 'Fred Foo'; realname = 'Fred Foo';
emailDaoMock._account = { authMock.emailAddress = emailAddress;
emailAddress: emailAddress, authMock.realname = realname;
realname: realname
};
keychainMock = sinon.createStubInstance(KeychainDAO); angular.module('writetest', ['woEmail', 'woServices', 'woUtil']);
appController._keychain = keychainMock; angular.mock.module('writetest');
angular.mock.inject(function($rootScope, $controller) {
angular.module('writetest', []);
mocks.module('writetest');
mocks.inject(function($rootScope, $controller) {
scope = $rootScope.$new(); scope = $rootScope.$new();
scope.state = {}; scope.state = {};
ctrl = $controller(WriteCtrl, { ctrl = $controller(WriteCtrl, {
$scope: scope $scope: scope,
auth: authMock,
keychain: keychainMock,
pgp: pgpMock,
email: emailMock,
outbox: outboxMock,
dialog: dialogMock
}); });
}); });
}); });
afterEach(function() { afterEach(function() {});
// restore the app controller
appController._emailDao = origEmailDao;
appController._outboxBo = origOutbox;
appController._keychain = origKeychain;
});
describe('scope variables', function() { describe('scope variables', function() {
it('should be set correctly', function() { it('should be set correctly', function() {
@ -187,7 +180,7 @@ describe('Write controller unit test', function() {
expect(keychainMock.getReceiverPublicKey.called).to.be.false; expect(keychainMock.getReceiverPublicKey.called).to.be.false;
}); });
it('should not work for error in keychain', function(done) { it('should not work for error in keychain', function() {
var recipient = { var recipient = {
address: 'asds@example.com' address: 'asds@example.com'
}; };
@ -198,15 +191,13 @@ describe('Write controller unit test', function() {
errMsg: '404 not found yadda yadda' errMsg: '404 not found yadda yadda'
}); });
scope.onError = function() {
expect(recipient.key).to.be.undefined;
expect(recipient.secure).to.be.false;
expect(scope.checkSendStatus.callCount).to.equal(1);
expect(keychainMock.refreshKeyForUserId.calledOnce).to.be.true;
done();
};
scope.verify(recipient); scope.verify(recipient);
expect(dialogMock.error.calledOnce).to.be.true;
expect(recipient.key).to.be.undefined;
expect(recipient.secure).to.be.false;
expect(scope.checkSendStatus.callCount).to.equal(1);
expect(keychainMock.refreshKeyForUserId.calledOnce).to.be.true;
}); });
it('should work for main userId', function(done) { it('should work for main userId', function(done) {
@ -337,6 +328,7 @@ describe('Write controller unit test', function() {
address: emailAddress, address: emailAddress,
name: realname name: realname
}]); }]);
expect(mail.to).to.deep.equal(scope.to); expect(mail.to).to.deep.equal(scope.to);
expect(mail.cc).to.deep.equal(scope.cc); expect(mail.cc).to.deep.equal(scope.cc);
expect(mail.bcc).to.deep.equal(scope.bcc); expect(mail.bcc).to.deep.equal(scope.bcc);
@ -345,19 +337,14 @@ describe('Write controller unit test', function() {
expect(mail.attachments).to.be.empty; expect(mail.attachments).to.be.empty;
expect(mail.sentDate).to.exist; expect(mail.sentDate).to.exist;
return true; return true;
})).yields(); })).yields();
emailDaoMock.setFlags.yields(); emailMock.setFlags.yields();
scope.onError = function(err) {
expect(err).to.not.exist;
};
scope.sendToOutbox(); scope.sendToOutbox();
expect(outboxMock.put.calledOnce).to.be.true; expect(outboxMock.put.calledOnce).to.be.true;
expect(emailDaoMock.setFlags.calledOnce).to.be.true; expect(emailMock.setFlags.calledOnce).to.be.true;
expect(scope.state.lightbox).to.be.undefined; expect(scope.state.lightbox).to.be.undefined;
expect(scope.replyTo.answered).to.be.true; expect(scope.replyTo.answered).to.be.true;
}); });