From 3a266496c6a428272daf28af43f7b4c1818e4a10 Mon Sep 17 00:00:00 2001 From: benbracha Date: Thu, 5 Nov 2015 12:39:37 +0200 Subject: [PATCH 1/2] Fix angular-busy promise-compare issues and added UT --- angular-busy.js | 10 ++++++++++ dist/angular-busy.js | 10 ++++++++++ dist/angular-busy.min.js | 2 +- test/spec.js | 32 +++++++++----------------------- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/angular-busy.js b/angular-busy.js index 557435c..16bd043 100755 --- a/angular-busy.js +++ b/angular-busy.js @@ -185,6 +185,16 @@ angular.module('cgBusy').directive('cgBusy',['$compile','$templateCache','cgBusy templateScope.$message = options.message; + if (options.promise) { + // Assign each promise a "id" field, so the equals below will work correctly + // Specifically - if we counter a new promise object, reset the tracker. + angular.forEach(options.promise, function(promise) { + if (promise) { + promise.id = promise.id || Math.random(); + } + }); + } + if (!angular.equals(tracker.promises,options.promise)) { tracker.reset({ promises:options.promise, diff --git a/dist/angular-busy.js b/dist/angular-busy.js index 31c8952..2fa98b4 100644 --- a/dist/angular-busy.js +++ b/dist/angular-busy.js @@ -185,6 +185,16 @@ angular.module('cgBusy').directive('cgBusy',['$compile','$templateCache','cgBusy templateScope.$message = options.message; + if (options.promise) { + // Assign each promise a "id" field, so the equals below will work correctly + // Specifically - if we counter a new promise object, reset the tracker. + angular.forEach(options.promise, function(promise) { + if (promise) { + promise.id = promise.id || Math.random(); + } + }); + } + if (!angular.equals(tracker.promises,options.promise)) { tracker.reset({ promises:options.promise, diff --git a/dist/angular-busy.min.js b/dist/angular-busy.min.js index 6d8738d..c1a5987 100644 --- a/dist/angular-busy.min.js +++ b/dist/angular-busy.min.js @@ -1 +1 @@ -angular.module("cgBusy",[]),angular.module("cgBusy").factory("_cgBusyTrackerFactory",["$timeout","$q",function(a,b){return function(){var c={};c.promises=[],c.delayPromise=null,c.durationPromise=null,c.delayJustFinished=!1,c.reset=function(b){c.minDuration=b.minDuration,c.promises=[],angular.forEach(b.promises,function(a){a&&!a.$cgBusyFulfilled&&d(a)}),0!==c.promises.length&&(c.delayJustFinished=!1,b.delay&&(c.delayPromise=a(function(){c.delayPromise=null,c.delayJustFinished=!0},parseInt(b.delay,10))),b.minDuration&&(c.durationPromise=a(function(){c.durationPromise=null},parseInt(b.minDuration,10)+(b.delay?parseInt(b.delay,10):0))))},c.isPromise=function(a){var b=a&&(a.then||a.$then||a.$promise&&a.$promise.then);return"undefined"!=typeof b},c.callThen=function(a,c,d){var e;a.then||a.$then?e=a:a.$promise?e=a.$promise:a.denodeify&&(e=b.when(a));var f=e.then||e.$then;f.call(e,c,d)};var d=function(a){if(!c.isPromise(a))throw new Error("cgBusy expects a promise (or something that has a .promise or .$promise");-1===c.promises.indexOf(a)&&(c.promises.push(a),c.callThen(a,function(){a.$cgBusyFulfilled=!0,-1!==c.promises.indexOf(a)&&c.promises.splice(c.promises.indexOf(a),1)},function(){a.$cgBusyFulfilled=!0,-1!==c.promises.indexOf(a)&&c.promises.splice(c.promises.indexOf(a),1)}))};return c.active=function(){return c.delayPromise?!1:c.delayJustFinished?(c.delayJustFinished=!1,0===c.promises.length&&(c.durationPromise=null),c.promises.length>0):c.durationPromise?!0:c.promises.length>0},c}}]),angular.module("cgBusy").value("cgBusyDefaults",{}),angular.module("cgBusy").directive("cgBusy",["$compile","$templateCache","cgBusyDefaults","$http","_cgBusyTrackerFactory",function(a,b,c,d,e){return{restrict:"A",link:function(f,g,h){var i=g.css("position");("static"===i||""===i||"undefined"==typeof i)&&g.css("position","relative");var j,k,l,m,n,o=e(),p={templateUrl:"angular-busy.html",delay:0,minDuration:0,backdrop:!0,message:"Please Wait...",wrapperClass:"cg-busy cg-busy-animation"};angular.extend(p,c),f.$watchCollection(h.cgBusy,function(c){if(c||(c={promise:null}),angular.isString(c))throw new Error("Invalid value for cg-busy. cgBusy no longer accepts string ids to represent promises/trackers.");(angular.isArray(c)||o.isPromise(c))&&(c={promise:c}),c=angular.extend(angular.copy(p),c),c.templateUrl||(c.templateUrl=p.templateUrl),angular.isArray(c.promise)||(c.promise=[c.promise]),m||(m=f.$new()),m.$message=c.message,angular.equals(o.promises,c.promise)||o.reset({promises:c.promise,delay:c.delay,minDuration:c.minDuration}),m.$cgBusyIsActive=function(){return o.active()},j&&l===c.templateUrl&&n===c.backdrop||(j&&j.remove(),k&&k.remove(),l=c.templateUrl,n=c.backdrop,d.get(l,{cache:b}).success(function(b){if(c.backdrop="undefined"==typeof c.backdrop?!0:c.backdrop,c.backdrop){var d='
';k=a(d)(m),g.append(k)}var e='
'+b+"
";j=a(e)(m),angular.element(j.children()[0]).css("position","absolute").css("top",0).css("left",0).css("right",0).css("bottom",0),g.append(j)}).error(function(a){throw new Error("Template specified for cgBusy ("+c.templateUrl+") could not be loaded. "+a)}))},!0)}}}]),angular.module("cgBusy").run(["$templateCache",function(a){"use strict";a.put("angular-busy.html",'
\n\n
\n\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n\n
{{$message}}
\n\n
\n\n
')}]); \ No newline at end of file +angular.module("cgBusy",[]),angular.module("cgBusy").factory("_cgBusyTrackerFactory",["$timeout","$q",function(a,b){return function(){var c={};c.promises=[],c.delayPromise=null,c.durationPromise=null,c.delayJustFinished=!1,c.reset=function(b){c.minDuration=b.minDuration,c.promises=[],angular.forEach(b.promises,function(a){a&&!a.$cgBusyFulfilled&&d(a)}),0!==c.promises.length&&(c.delayJustFinished=!1,b.delay&&(c.delayPromise=a(function(){c.delayPromise=null,c.delayJustFinished=!0},parseInt(b.delay,10))),b.minDuration&&(c.durationPromise=a(function(){c.durationPromise=null},parseInt(b.minDuration,10)+(b.delay?parseInt(b.delay,10):0))))},c.isPromise=function(a){var b=a&&(a.then||a.$then||a.$promise&&a.$promise.then);return"undefined"!=typeof b},c.callThen=function(a,c,d){var e;a.then||a.$then?e=a:a.$promise?e=a.$promise:a.denodeify&&(e=b.when(a));var f=e.then||e.$then;f.call(e,c,d)};var d=function(a){if(!c.isPromise(a))throw new Error("cgBusy expects a promise (or something that has a .promise or .$promise");-1===c.promises.indexOf(a)&&(c.promises.push(a),c.callThen(a,function(){a.$cgBusyFulfilled=!0,-1!==c.promises.indexOf(a)&&c.promises.splice(c.promises.indexOf(a),1)},function(){a.$cgBusyFulfilled=!0,-1!==c.promises.indexOf(a)&&c.promises.splice(c.promises.indexOf(a),1)}))};return c.active=function(){return c.delayPromise?!1:c.delayJustFinished?(c.delayJustFinished=!1,0===c.promises.length&&(c.durationPromise=null),c.promises.length>0):c.durationPromise?!0:c.promises.length>0},c}}]),angular.module("cgBusy").value("cgBusyDefaults",{}),angular.module("cgBusy").directive("cgBusy",["$compile","$templateCache","cgBusyDefaults","$http","_cgBusyTrackerFactory",function(a,b,c,d,e){return{restrict:"A",link:function(f,g,h,i){var j=g.css("position");("static"===j||""===j||"undefined"==typeof j)&&g.css("position","relative");var k,l,m,n,o,p=e(),q={templateUrl:"angular-busy.html",delay:0,minDuration:0,backdrop:!0,message:"Please Wait...",wrapperClass:"cg-busy cg-busy-animation"};angular.extend(q,c),f.$watchCollection(h.cgBusy,function(c){if(c||(c={promise:null}),angular.isString(c))throw new Error("Invalid value for cg-busy. cgBusy no longer accepts string ids to represent promises/trackers.");(angular.isArray(c)||p.isPromise(c))&&(c={promise:c}),c=angular.extend(angular.copy(q),c),c.templateUrl||(c.templateUrl=q.templateUrl),angular.isArray(c.promise)||(c.promise=[c.promise]),n||(n=f.$new()),n.$message=c.message,c.promise&&angular.forEach(c.promise,function(a){a&&(a.id=a.id||Math.random())}),angular.equals(p.promises,c.promise)||p.reset({promises:c.promise,delay:c.delay,minDuration:c.minDuration}),n.$cgBusyIsActive=function(){return p.active()},k&&m===c.templateUrl&&o===c.backdrop||(k&&k.remove(),l&&l.remove(),m=c.templateUrl,o=c.backdrop,d.get(m,{cache:b}).success(function(b){if(c.backdrop="undefined"==typeof c.backdrop?!0:c.backdrop,c.backdrop){var d='
';l=a(d)(n),g.append(l)}var e='
'+b+"
";k=a(e)(n),angular.element(k.children()[0]).css("position","absolute").css("top",0).css("left",0).css("right",0).css("bottom",0),g.append(k)}).error(function(a){throw new Error("Template specified for cgBusy ("+c.templateUrl+") could not be loaded. "+a)}))},!0)}}}]),angular.module("cgBusy").run(["$templateCache",function(a){"use strict";a.put("angular-busy.html",'
\n\n
\n\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n\n
{{$message}}
\n\n
\n\n
')}]); \ No newline at end of file diff --git a/test/spec.js b/test/spec.js index ca879e4..7a0778d 100755 --- a/test/spec.js +++ b/test/spec.js @@ -32,39 +32,25 @@ describe('cgBusy', function() { expect(this.element.children().css('display')).toBe('block');//ensure its visible (promise is ongoing) - this.testPromise.resolve(); - scope.$apply(); - - expect(this.element.children().css('display')).toBe('none'); //ensure its now invisible as the promise is resolved - }); - - it('should show the overlay during multiple promises', function() { - - this.element = compile('
')(scope); - angular.element('body').append(this.element); - - this.testPromise = q.defer(); - scope.my_promise = this.testPromise.promise; - + // Change promise this.testPromise2 = q.defer(); - scope.my_promise2 = this.testPromise2.promise; - - //httpBackend.flush(); + scope.my_promise = this.testPromise2.promise; + scope.$apply(); + // Now resolve the first one promise, the overlay should still be visible + this.testPromise.resolve(); scope.$apply(); expect(this.element.children().length).toBe(2); //ensure the elements are added expect(this.element.children().css('display')).toBe('block');//ensure its visible (promise is ongoing) - this.testPromise.resolve(); - scope.$apply(); - - expect(this.element.children().css('display')).toBe('block'); //ensure its still visible (promise is ongoing) - + // Now resolve the second promise, and overlay should be removed this.testPromise2.resolve(); scope.$apply(); + expect(this.element.children().css('display')).toBe('none'); //ensure its now invisible as the promise is resolved + }); it('should load custom templates', function(){ @@ -132,6 +118,6 @@ describe('cgBusy', function() { timeout.flush(101); //1001ms total expect(this.element.children().css('display')).toBe('none'); - }); + }); }); \ No newline at end of file From e8a36ee9f3c26fbd213f8dd053b4818a54d0f033 Mon Sep 17 00:00:00 2001 From: benbracha Date: Thu, 5 Nov 2015 17:42:36 +0200 Subject: [PATCH 2/2] Return accidently removed UT from angular-busy --- test/spec.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/test/spec.js b/test/spec.js index 7a0778d..a10728d 100755 --- a/test/spec.js +++ b/test/spec.js @@ -32,6 +32,58 @@ describe('cgBusy', function() { expect(this.element.children().css('display')).toBe('block');//ensure its visible (promise is ongoing) + this.testPromise.resolve(); + scope.$apply(); + + expect(this.element.children().css('display')).toBe('none'); //ensure its now invisible as the promise is resolved + }); + + it('should show the overlay during multiple promises', function() { + + this.element = compile('
')(scope); + angular.element('body').append(this.element); + + this.testPromise = q.defer(); + scope.my_promise = this.testPromise.promise; + + this.testPromise2 = q.defer(); + scope.my_promise2 = this.testPromise2.promise; + + //httpBackend.flush(); + + scope.$apply(); + + expect(this.element.children().length).toBe(2); //ensure the elements are added + + expect(this.element.children().css('display')).toBe('block');//ensure its visible (promise is ongoing) + + this.testPromise.resolve(); + scope.$apply(); + + expect(this.element.children().css('display')).toBe('block'); //ensure its still visible (promise is ongoing) + + this.testPromise2.resolve(); + scope.$apply(); + expect(this.element.children().css('display')).toBe('none'); //ensure its now invisible as the promise is resolved + }); + + + it('should show the overlay when changing an unresolved promise with other unresolved promise', function() { + + this.element = compile('
')(scope); + angular.element('body').append(this.element); + + this.testPromise = q.defer(); + scope.my_promise = this.testPromise.promise; + + //httpBackend.flush(); + + scope.$apply(); + + expect(this.element.children().length).toBe(2); //ensure the elements are added + + expect(this.element.children().css('display')).toBe('block');//ensure its visible (promise is ongoing) + // Change promise this.testPromise2 = q.defer(); scope.my_promise = this.testPromise2.promise;