mirror of
https://github.com/moparisthebest/mail
synced 2025-02-16 23:20:09 -05:00
Write dialog unit test
This commit is contained in:
parent
198118571f
commit
cb5431dc16
@ -1,41 +1,27 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var axe = require('axe-logger');
|
var DialogCtrl = function($scope, dialog) {
|
||||||
|
|
||||||
var DialogCtrl = function($scope, $q, dialog) {
|
|
||||||
|
|
||||||
var callback;
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Set dialog disply functions
|
// Set dialog disply functions
|
||||||
//
|
//
|
||||||
|
|
||||||
dialog.displayInfo = function(options) {
|
dialog.displayInfo = function(options) {
|
||||||
return $q(function(resolve) {
|
|
||||||
setOptions(options);
|
setOptions(options);
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
dialog.displayError = function(options) {
|
dialog.displayError = function(options) {
|
||||||
return $q(function(resolve) {
|
if (!options) {
|
||||||
if (options) {
|
return;
|
||||||
axe.error((options.errMsg || options.message) + (options.stack ? ('\n' + options.stack) : ''));
|
}
|
||||||
|
|
||||||
setOptions(options);
|
setOptions(options);
|
||||||
$scope.title = options.title || 'Error';
|
$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
|
$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) {
|
dialog.displayConfirm = function(options) {
|
||||||
return $q(function(resolve) {
|
|
||||||
setOptions(options);
|
setOptions(options);
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function setOptions(options) {
|
function setOptions(options) {
|
||||||
@ -46,8 +32,7 @@ var DialogCtrl = function($scope, $q, dialog) {
|
|||||||
$scope.positiveBtnStr = options.positiveBtnStr || 'Ok';
|
$scope.positiveBtnStr = options.positiveBtnStr || 'Ok';
|
||||||
$scope.negativeBtnStr = options.negativeBtnStr || 'Cancel';
|
$scope.negativeBtnStr = options.negativeBtnStr || 'Cancel';
|
||||||
$scope.showNegativeBtn = options.showNegativeBtn || false;
|
$scope.showNegativeBtn = options.showNegativeBtn || false;
|
||||||
|
$scope.callback = options.callback;
|
||||||
callback = options.callback;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -57,10 +42,10 @@ var DialogCtrl = function($scope, $q, dialog) {
|
|||||||
$scope.confirm = function(ok) {
|
$scope.confirm = function(ok) {
|
||||||
$scope.open = false;
|
$scope.open = false;
|
||||||
|
|
||||||
if (callback) {
|
if ($scope.callback) {
|
||||||
callback(ok);
|
$scope.callback(ok);
|
||||||
}
|
}
|
||||||
callback = undefined;
|
$scope.callback = undefined;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
8
src/js/util/axe.js
Normal file
8
src/js/util/axe.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var axe = require('axe-logger');
|
||||||
|
|
||||||
|
var ngModule = angular.module('woUtil');
|
||||||
|
ngModule.factory('axe', function() {
|
||||||
|
return axe;
|
||||||
|
});
|
@ -4,16 +4,57 @@ var ngModule = angular.module('woUtil');
|
|||||||
ngModule.service('dialog', Dialog);
|
ngModule.service('dialog', Dialog);
|
||||||
module.exports = 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) {
|
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) {
|
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) {
|
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!'));
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
angular.module('woUtil', []);
|
angular.module('woUtil', []);
|
||||||
|
|
||||||
|
require('./axe');
|
||||||
require('./dialog');
|
require('./dialog');
|
||||||
require('./connection-doctor');
|
require('./connection-doctor');
|
||||||
require('./update/update-handler');
|
require('./update/update-handler');
|
||||||
|
11
test/main.js
11
test/main.js
@ -46,8 +46,19 @@ if (!Function.prototype.bind) {
|
|||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Test setup
|
||||||
|
//
|
||||||
|
|
||||||
// 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';
|
||||||
|
|
||||||
var axe = require('axe-logger');
|
var axe = require('axe-logger');
|
||||||
axe.removeAppender(axe.defaultAppender);
|
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');
|
82
test/unit/util/dialog-test.js
Normal file
82
test/unit/util/dialog-test.js
Normal 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();
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user