1
0
mirror of https://github.com/moparisthebest/mail synced 2024-12-01 13:22:16 -05:00

Use in dialog service instead of controller

This commit is contained in:
Tankred Hase 2014-11-29 14:46:16 +01:00
parent 5b4d7bcd4a
commit 81a2d64835
3 changed files with 42 additions and 49 deletions

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
var DialogCtrl = function($scope, $timeout, dialog) { var DialogCtrl = function($scope, dialog) {
$scope.state.dialog = { $scope.state.dialog = {
open: false open: false
@ -11,13 +11,10 @@ var DialogCtrl = function($scope, $timeout, dialog) {
// //
dialog.displayInfo = function(options) { dialog.displayInfo = function(options) {
return $timeout(function() {
setOptions(options); setOptions(options);
});
}; };
dialog.displayError = function(options) { dialog.displayError = function(options) {
return $timeout(function() {
if (!options) { if (!options) {
return; return;
} }
@ -25,13 +22,10 @@ var DialogCtrl = function($scope, $timeout, dialog) {
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
});
}; };
dialog.displayConfirm = function(options) { dialog.displayConfirm = function(options) {
return $timeout(function() {
setOptions(options); setOptions(options);
});
}; };
function setOptions(options) { function setOptions(options) {

View File

@ -7,8 +7,8 @@ module.exports = Dialog;
/** /**
* A central service to display messages to the user in a dialog * A central service to display messages to the user in a dialog
*/ */
function Dialog($q, axe) { function Dialog($timeout, axe) {
this._q = $q; this._timeout = $timeout;
this._axe = axe; this._axe = axe;
// binds the methods to the instance of the dialog service so that we can e.g. // binds the methods to the instance of the dialog service so that we can e.g.
@ -58,11 +58,12 @@ Dialog.prototype.confirm = function(options) {
* Helper function which returns a promise * Helper function which returns a promise
*/ */
Dialog.prototype._handle = function(options, fn, errMsg) { Dialog.prototype._handle = function(options, fn, errMsg) {
return this._q(function(resolve, reject) { var self = this;
return self._timeout(function() {
if (fn) { if (fn) {
return resolve(fn(options)); return fn(options);
} else { } else {
reject(new Error('Dialog service ' + errMsg + ' not set!')); self._axe.error('dialog service', errMsg + ' function not set!');
} }
}); });
}; };

View File

@ -1,7 +1,7 @@
'use strict'; 'use strict';
describe('Dialog Service unit test', function() { describe('Dialog Service unit test', function() {
var dialog, logErrorStub, var dialog, logErrorStub, timeout,
opt = { opt = {
foo: 'bar' foo: 'bar'
}; };
@ -9,8 +9,9 @@ describe('Dialog Service unit test', function() {
beforeEach(function() { beforeEach(function() {
angular.module('dialog-test', ['woUtil']); angular.module('dialog-test', ['woUtil']);
angular.mock.module('dialog-test'); angular.mock.module('dialog-test');
angular.mock.inject(function($injector, axe) { angular.mock.inject(function($injector, $timeout, axe) {
logErrorStub = sinon.stub(axe, 'error'); logErrorStub = sinon.stub(axe, 'error');
timeout = $timeout;
dialog = $injector.get('dialog'); dialog = $injector.get('dialog');
}); });
}); });
@ -20,7 +21,7 @@ describe('Dialog Service unit test', function() {
}); });
describe('info', function() { describe('info', function() {
it('should work', inject(function($rootScope) { it('should work', function() {
dialog.displayInfo = function() {}; dialog.displayInfo = function() {};
var displayInfoStub = sinon.stub(dialog, 'displayInfo'); var displayInfoStub = sinon.stub(dialog, 'displayInfo');
@ -28,20 +29,19 @@ describe('Dialog Service unit test', function() {
expect(result).to.not.exist; expect(result).to.not.exist;
}); });
$rootScope.$apply(); timeout.flush();
expect(displayInfoStub.withArgs(opt).calledOnce).to.be.true; 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/);
}); });
it('should fail for no display function', function() {
dialog.info(opt);
$rootScope.$apply(); timeout.flush();
})); expect(logErrorStub.calledOnce).to.be.true;
});
}); });
describe('error', function() { describe('error', function() {
it('should work', inject(function($rootScope) { it('should work', function() {
dialog.displayError = function() {}; dialog.displayError = function() {};
var displayErrorStub = sinon.stub(dialog, 'displayError'); var displayErrorStub = sinon.stub(dialog, 'displayError');
@ -50,21 +50,20 @@ describe('Dialog Service unit test', function() {
}); });
expect(logErrorStub.calledOnce).to.be.true; expect(logErrorStub.calledOnce).to.be.true;
$rootScope.$apply(); timeout.flush();
expect(displayErrorStub.withArgs(opt).calledOnce).to.be.true; 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/);
}); });
it('should fail for no display function', function() {
dialog.error(opt);
expect(logErrorStub.calledOnce).to.be.true; expect(logErrorStub.calledOnce).to.be.true;
$rootScope.$apply(); timeout.flush();
})); expect(logErrorStub.calledTwice).to.be.true;
});
}); });
describe('confirm', function() { describe('confirm', function() {
it('should work', inject(function($rootScope) { it('should work', function() {
dialog.displayConfirm = function() {}; dialog.displayConfirm = function() {};
var displayConfirmStub = sinon.stub(dialog, 'displayConfirm'); var displayConfirmStub = sinon.stub(dialog, 'displayConfirm');
@ -72,16 +71,15 @@ describe('Dialog Service unit test', function() {
expect(result).to.not.exist; expect(result).to.not.exist;
}); });
$rootScope.$apply(); timeout.flush();
expect(displayConfirmStub.withArgs(opt).calledOnce).to.be.true; 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/);
}); });
it('should fail for no display function', function() {
dialog.confirm(opt);
$rootScope.$apply(); timeout.flush();
})); expect(logErrorStub.calledOnce).to.be.true;
});
}); });
}); });