From 81a2d648353480fb400f6da23c7660aa553f357c Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Sat, 29 Nov 2014 14:46:16 +0100 Subject: [PATCH] Use in dialog service instead of controller --- src/js/controller/app/dialog.js | 24 ++++++-------- src/js/util/dialog.js | 11 ++++--- test/unit/util/dialog-test.js | 56 ++++++++++++++++----------------- 3 files changed, 42 insertions(+), 49 deletions(-) diff --git a/src/js/controller/app/dialog.js b/src/js/controller/app/dialog.js index 58d8cde..880c332 100644 --- a/src/js/controller/app/dialog.js +++ b/src/js/controller/app/dialog.js @@ -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) { diff --git a/src/js/util/dialog.js b/src/js/util/dialog.js index e521221..c39a95d 100644 --- a/src/js/util/dialog.js +++ b/src/js/util/dialog.js @@ -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!'); } }); }; \ No newline at end of file diff --git a/test/unit/util/dialog-test.js b/test/unit/util/dialog-test.js index e0b95c8..920d2b8 100644 --- a/test/unit/util/dialog-test.js +++ b/test/unit/util/dialog-test.js @@ -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; + }); }); }); \ No newline at end of file