mirror of
https://github.com/moparisthebest/mail
synced 2024-11-25 18:32:20 -05:00
Use in dialog service instead of controller
This commit is contained in:
parent
5b4d7bcd4a
commit
81a2d64835
@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
var DialogCtrl = function($scope, $timeout, dialog) {
|
||||
var DialogCtrl = function($scope, dialog) {
|
||||
|
||||
$scope.state.dialog = {
|
||||
open: false
|
||||
@ -11,27 +11,21 @@ var DialogCtrl = function($scope, $timeout, dialog) {
|
||||
//
|
||||
|
||||
dialog.displayInfo = function(options) {
|
||||
return $timeout(function() {
|
||||
setOptions(options);
|
||||
});
|
||||
setOptions(options);
|
||||
};
|
||||
|
||||
dialog.displayError = function(options) {
|
||||
return $timeout(function() {
|
||||
if (!options) {
|
||||
return;
|
||||
}
|
||||
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
|
||||
});
|
||||
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
|
||||
};
|
||||
|
||||
dialog.displayConfirm = function(options) {
|
||||
return $timeout(function() {
|
||||
setOptions(options);
|
||||
});
|
||||
setOptions(options);
|
||||
};
|
||||
|
||||
function setOptions(options) {
|
||||
|
@ -7,8 +7,8 @@ module.exports = Dialog;
|
||||
/**
|
||||
* A central service to display messages to the user in a dialog
|
||||
*/
|
||||
function Dialog($q, axe) {
|
||||
this._q = $q;
|
||||
function Dialog($timeout, axe) {
|
||||
this._timeout = $timeout;
|
||||
this._axe = axe;
|
||||
|
||||
// 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
|
||||
*/
|
||||
Dialog.prototype._handle = function(options, fn, errMsg) {
|
||||
return this._q(function(resolve, reject) {
|
||||
var self = this;
|
||||
return self._timeout(function() {
|
||||
if (fn) {
|
||||
return resolve(fn(options));
|
||||
return fn(options);
|
||||
} else {
|
||||
reject(new Error('Dialog service ' + errMsg + ' not set!'));
|
||||
self._axe.error('dialog service', errMsg + ' function not set!');
|
||||
}
|
||||
});
|
||||
};
|
@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
describe('Dialog Service unit test', function() {
|
||||
var dialog, logErrorStub,
|
||||
var dialog, logErrorStub, timeout,
|
||||
opt = {
|
||||
foo: 'bar'
|
||||
};
|
||||
@ -9,8 +9,9 @@ describe('Dialog Service unit test', function() {
|
||||
beforeEach(function() {
|
||||
angular.module('dialog-test', ['woUtil']);
|
||||
angular.mock.module('dialog-test');
|
||||
angular.mock.inject(function($injector, axe) {
|
||||
angular.mock.inject(function($injector, $timeout, axe) {
|
||||
logErrorStub = sinon.stub(axe, 'error');
|
||||
timeout = $timeout;
|
||||
dialog = $injector.get('dialog');
|
||||
});
|
||||
});
|
||||
@ -20,7 +21,7 @@ describe('Dialog Service unit test', function() {
|
||||
});
|
||||
|
||||
describe('info', function() {
|
||||
it('should work', inject(function($rootScope) {
|
||||
it('should work', function() {
|
||||
dialog.displayInfo = function() {};
|
||||
var displayInfoStub = sinon.stub(dialog, 'displayInfo');
|
||||
|
||||
@ -28,20 +29,19 @@ describe('Dialog Service unit test', function() {
|
||||
expect(result).to.not.exist;
|
||||
});
|
||||
|
||||
$rootScope.$apply();
|
||||
timeout.flush();
|
||||
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() {
|
||||
it('should work', inject(function($rootScope) {
|
||||
it('should work', function() {
|
||||
dialog.displayError = function() {};
|
||||
var displayErrorStub = sinon.stub(dialog, 'displayError');
|
||||
|
||||
@ -50,21 +50,20 @@ describe('Dialog Service unit test', function() {
|
||||
});
|
||||
|
||||
expect(logErrorStub.calledOnce).to.be.true;
|
||||
$rootScope.$apply();
|
||||
timeout.flush();
|
||||
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;
|
||||
$rootScope.$apply();
|
||||
}));
|
||||
timeout.flush();
|
||||
expect(logErrorStub.calledTwice).to.be.true;
|
||||
});
|
||||
});
|
||||
|
||||
describe('confirm', function() {
|
||||
it('should work', inject(function($rootScope) {
|
||||
it('should work', function() {
|
||||
dialog.displayConfirm = function() {};
|
||||
var displayConfirmStub = sinon.stub(dialog, 'displayConfirm');
|
||||
|
||||
@ -72,16 +71,15 @@ describe('Dialog Service unit test', function() {
|
||||
expect(result).to.not.exist;
|
||||
});
|
||||
|
||||
$rootScope.$apply();
|
||||
timeout.flush();
|
||||
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;
|
||||
});
|
||||
});
|
||||
|
||||
});
|
Loading…
Reference in New Issue
Block a user