From cb5431dc16b8ed9e92a292fcabec1b8899b0a88b Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Fri, 21 Nov 2014 12:13:06 +0100 Subject: [PATCH] Write dialog unit test --- src/js/controller/app/dialog.js | 41 ++++++----------- src/js/util/axe.js | 8 ++++ src/js/util/dialog.js | 49 ++++++++++++++++++-- src/js/util/index.js | 1 + test/main.js | 13 +++++- test/unit/util/dialog-test.js | 82 +++++++++++++++++++++++++++++++++ 6 files changed, 161 insertions(+), 33 deletions(-) create mode 100644 src/js/util/axe.js create mode 100644 test/unit/util/dialog-test.js diff --git a/src/js/controller/app/dialog.js b/src/js/controller/app/dialog.js index 34ec12f..79f9da1 100644 --- a/src/js/controller/app/dialog.js +++ b/src/js/controller/app/dialog.js @@ -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(); - }); + setOptions(options); }; 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(); - }); + 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 $q(function(resolve) { - setOptions(options); - resolve(); - }); + setOptions(options); }; 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; }; }; diff --git a/src/js/util/axe.js b/src/js/util/axe.js new file mode 100644 index 0000000..0325f8d --- /dev/null +++ b/src/js/util/axe.js @@ -0,0 +1,8 @@ +'use strict'; + +var axe = require('axe-logger'); + +var ngModule = angular.module('woUtil'); +ngModule.factory('axe', function() { + return axe; +}); \ No newline at end of file diff --git a/src/js/util/dialog.js b/src/js/util/dialog.js index 8d5a330..ec38523 100644 --- a/src/js/util/dialog.js +++ b/src/js/util/dialog.js @@ -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!')); + } + }); }; \ No newline at end of file diff --git a/src/js/util/index.js b/src/js/util/index.js index 191f042..1536843 100644 --- a/src/js/util/index.js +++ b/src/js/util/index.js @@ -2,6 +2,7 @@ angular.module('woUtil', []); +require('./axe'); require('./dialog'); require('./connection-doctor'); require('./update/update-handler'); diff --git a/test/main.js b/test/main.js index ce266e7..a2e39fe 100644 --- a/test/main.js +++ b/test/main.js @@ -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); \ No newline at end of file +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'); \ No newline at end of file diff --git a/test/unit/util/dialog-test.js b/test/unit/util/dialog-test.js new file mode 100644 index 0000000..67acd6f --- /dev/null +++ b/test/unit/util/dialog-test.js @@ -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(); + })); + }); + +}); \ No newline at end of file