1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-22 17:02:17 -05:00

Write dialog unit test

This commit is contained in:
Tankred Hase 2014-11-21 12:13:06 +01:00
parent 198118571f
commit cb5431dc16
6 changed files with 161 additions and 33 deletions

View File

@ -1,41 +1,27 @@
'use strict';
var axe = require('axe-logger');
var DialogCtrl = function($scope, $q, dialog) {
var callback;
var DialogCtrl = function($scope, dialog) {
//
// Set dialog disply functions
//
dialog.displayInfo = function(options) {
return $q(function(resolve) {
setOptions(options);
resolve();
});
};
dialog.displayError = function(options) {
return $q(function(resolve) {
if (options) {
axe.error((options.errMsg || options.message) + (options.stack ? ('\n' + options.stack) : ''));
if (!options) {
return;
}
setOptions(options);
$scope.title = options.title || 'Error';
$scope.showBugReporter = (typeof options.showBugReporter !== 'undefined' ? options.showBugReporter : !options.title); // if title is set, presume it's not an error by default
}
resolve();
});
};
dialog.displayConfirm = function(options) {
return $q(function(resolve) {
setOptions(options);
resolve();
});
};
function setOptions(options) {
@ -46,8 +32,7 @@ var DialogCtrl = function($scope, $q, dialog) {
$scope.positiveBtnStr = options.positiveBtnStr || 'Ok';
$scope.negativeBtnStr = options.negativeBtnStr || 'Cancel';
$scope.showNegativeBtn = options.showNegativeBtn || false;
callback = options.callback;
$scope.callback = options.callback;
}
//
@ -57,10 +42,10 @@ var DialogCtrl = function($scope, $q, dialog) {
$scope.confirm = function(ok) {
$scope.open = false;
if (callback) {
callback(ok);
if ($scope.callback) {
$scope.callback(ok);
}
callback = undefined;
$scope.callback = undefined;
};
};

8
src/js/util/axe.js Normal file
View File

@ -0,0 +1,8 @@
'use strict';
var axe = require('axe-logger');
var ngModule = angular.module('woUtil');
ngModule.factory('axe', function() {
return axe;
});

View File

@ -4,16 +4,57 @@ var ngModule = angular.module('woUtil');
ngModule.service('dialog', Dialog);
module.exports = Dialog;
function Dialog() {}
/**
* A central service to display messages to the user in a dialog
*/
function Dialog($q, axe) {
this._q = $q;
this._axe = axe;
}
/**
* Show an information dialog
* @param {String} options.title The title of the displayed dialog
* @param {String} options.message The message to be displayed
* @return {Promise}
*/
Dialog.prototype.info = function(options) {
this.displayInfo(options);
return this._handle(options, this.displayInfo, 'displayInfo');
};
/**
* Show an error dialog
* @param {String} options.title (optional) The title of the displayed dialog
* @param {String} options.message The message to be displayed
* @return {Promise}
*/
Dialog.prototype.error = function(options) {
this.displayError(options);
// log the error
this._axe.error((options.errMsg || options.message) + (options.stack ? ('\n' + options.stack) : ''));
return this._handle(options, this.displayError, 'displayError');
};
/**
* Show an confirm dialog
* @param {String} options.title The title of the displayed dialog
* @param {String} options.message The message to be displayed
* @param {String} options.callback The callback that is called after the confirmation has been granted or denied
* @return {Promise}
*/
Dialog.prototype.confirm = function(options) {
this.displayConfirm(options);
return this._handle(options, this.displayConfirm, 'displayConfirm');
};
/**
* Helper function which returns a promise
*/
Dialog.prototype._handle = function(options, fn, errMsg) {
return this._q(function(resolve, reject) {
if (fn) {
fn(options);
resolve();
} else {
reject(new Error('Dialog service ' + errMsg + ' not set!'));
}
});
};

View File

@ -2,6 +2,7 @@
angular.module('woUtil', []);
require('./axe');
require('./dialog');
require('./connection-doctor');
require('./update/update-handler');

View File

@ -46,8 +46,19 @@ if (!Function.prototype.bind) {
}
})();
//
// Test setup
//
// set worker path for tests
require('../src/js/app-config').config.workerPath = '../lib';
var axe = require('axe-logger');
axe.removeAppender(axe.defaultAppender);
// include angular modules
require('../src/js/app-config');
require('../src/js/util');
require('../src/js/crypto');
require('../src/js/service');
require('../src/js/email');

View File

@ -0,0 +1,82 @@
'use strict';
describe('Dialog Service unit test', function() {
var dialog,
opt = {
foo: 'bar'
};
beforeEach(function() {
angular.module('dialog-test', ['woUtil']);
angular.mock.module('dialog-test');
angular.mock.inject(function($injector) {
dialog = $injector.get('dialog');
});
});
afterEach(function() {});
describe('info', function() {
it('should work', inject(function($rootScope) {
dialog.displayInfo = function() {};
var displayInfoStub = sinon.stub(dialog, 'displayInfo');
dialog.info(opt).then(function(result) {
expect(result).to.not.exist;
});
$rootScope.$apply();
expect(displayInfoStub.withArgs(opt).calledOnce).to.be.true;
}));
it('should fail for no display function', inject(function($rootScope) {
dialog.info(opt).catch(function(err) {
expect(err.message).to.match(/displayInfo/);
});
$rootScope.$apply();
}));
});
describe('error', function() {
it('should work', inject(function($rootScope) {
dialog.displayError = function() {};
var displayErrorStub = sinon.stub(dialog, 'displayError');
dialog.error(opt).then(function(result) {
expect(result).to.not.exist;
});
$rootScope.$apply();
expect(displayErrorStub.withArgs(opt).calledOnce).to.be.true;
}));
it('should fail for no display function', inject(function($rootScope) {
dialog.error(opt).catch(function(err) {
expect(err.message).to.match(/displayError/);
});
$rootScope.$apply();
}));
});
describe('confirm', function() {
it('should work', inject(function($rootScope) {
dialog.displayConfirm = function() {};
var displayConfirmStub = sinon.stub(dialog, 'displayConfirm');
dialog.confirm(opt).then(function(result) {
expect(result).to.not.exist;
});
$rootScope.$apply();
expect(displayConfirmStub.withArgs(opt).calledOnce).to.be.true;
}));
it('should fail for no display function', inject(function($rootScope) {
dialog.confirm(opt).catch(function(err) {
expect(err.message).to.match(/displayConfirm/);
});
$rootScope.$apply();
}));
});
});