Skip to content

Commit

Permalink
fixes #23. minDuration now accounted for even if no delay
Browse files Browse the repository at this point in the history
  • Loading branch information
cgross committed Apr 29, 2014
1 parent e8e9605 commit c8193eb
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 65 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ npm-debug.log
node_modules
.DS_Store

temp
temp

_SpecRunner.html

.grunt
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Only the values you'd like overriden need to be specified.


## Release History
* v4.0.2 - Fix for min duration only being used when delay also being set.
* v4.0.0 - Big update
* Dependency on angular-promise-tracker has been removed. We now track promises directly.
* Message is now configurable.
Expand Down
12 changes: 6 additions & 6 deletions angular-busy.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ angular.module('cgBusy').factory('_cgBusyTrackerFactory',['$timeout',function($t
return;
}

if (options.delay > 0) {
if (options.delay) {
tracker.delayPromise = $timeout(function(){
tracker.delayPromise = null;
if (options.minDuration) {
tracker.durationPromise = $timeout(function(){
tracker.durationPromise = null;
},options.minDuration);
}
},options.delay);
}
if (options.minDuration) {
tracker.durationPromise = $timeout(function(){
tracker.durationPromise = null;
},options.minDuration);
}
};

tracker.getThen = function(promise){
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-busy",
"version": "4.0.1",
"version": "4.0.2",
"main": [
"dist/angular-busy.js",
"dist/angular-busy.css"
Expand Down
12 changes: 6 additions & 6 deletions dist/angular-busy.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ angular.module('cgBusy').factory('_cgBusyTrackerFactory',['$timeout',function($t
return;
}

if (options.delay > 0) {
if (options.delay) {
tracker.delayPromise = $timeout(function(){
tracker.delayPromise = null;
if (options.minDuration) {
tracker.durationPromise = $timeout(function(){
tracker.durationPromise = null;
},options.minDuration);
}
},options.delay);
}
if (options.minDuration) {
tracker.durationPromise = $timeout(function(){
tracker.durationPromise = null;
},options.minDuration);
}
};

tracker.getThen = function(promise){
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-busy.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-busy",
"version": "4.0.1",
"version": "4.0.2",
"description": "",
"repository": {
"type": "git",
Expand Down
154 changes: 105 additions & 49 deletions test/spec.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,137 @@
describe('cgBusy', function() {

beforeEach(module('app'));
beforeEach(module('app'));

var scope,compile,q,httpBackend;
var scope,compile,q,httpBackend,timeout;

beforeEach(inject(function($rootScope,$compile,$q,$httpBackend,$templateCache) {
scope = $rootScope.$new();
compile = $compile;
q = $q;
httpBackend = $httpBackend;
httpBackend.whenGET('test-custom-template.html').respond(function(method, url, data, headers){
beforeEach(inject(function($rootScope,$compile,$q,$httpBackend,$templateCache,$timeout) {
scope = $rootScope.$new();
compile = $compile;
q = $q;
httpBackend = $httpBackend;
timeout = $timeout;
httpBackend.whenGET('test-custom-template.html').respond(function(method, url, data, headers){

return [[200],'<div id="custom">test-custom-template-contents</div>'];
});
}));
return [[200],'<div id="custom">test-custom-template-contents</div>'];
});
}));

it('should show the overlay during promise', function() {
it('should show the overlay during promise', function() {

this.element = compile('<div cg-busy="my_promise"></div>')(scope);
angular.element('body').append(this.element);
this.element = compile('<div cg-busy="my_promise"></div>')(scope);
angular.element('body').append(this.element);

this.testPromise = q.defer();
scope.my_promise = this.testPromise.promise;
this.testPromise = q.defer();
scope.my_promise = this.testPromise.promise;

//httpBackend.flush();
//httpBackend.flush();

scope.$apply();
scope.$apply();

expect(this.element.children().length).toBe(1); //ensure element is added
expect(this.element.children().length).toBe(1); //ensure element is added

expect(this.element.children().css('display')).toBe('block');//ensure its visible (promise is ongoing)
expect(this.element.children().css('display')).toBe('block');//ensure its visible (promise is ongoing)

this.testPromise.resolve();
scope.$apply();
this.testPromise.resolve();
scope.$apply();

expect(this.element.children().css('display')).toBe('none'); //ensure its now invisible as the promise is resolved
});
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() {
it('should show the overlay during multiple promises', function() {

this.element = compile('<div cg-busy="[my_promise,my_promise2]"></div>')(scope);
angular.element('body').append(this.element);
this.element = compile('<div cg-busy="[my_promise,my_promise2]"></div>')(scope);
angular.element('body').append(this.element);

this.testPromise = q.defer();
scope.my_promise = this.testPromise.promise;
this.testPromise = q.defer();
scope.my_promise = this.testPromise.promise;

this.testPromise2 = q.defer();
scope.my_promise2 = this.testPromise2.promise;
this.testPromise2 = q.defer();
scope.my_promise2 = this.testPromise2.promise;

//httpBackend.flush();
//httpBackend.flush();

scope.$apply();
scope.$apply();

expect(this.element.children().length).toBe(1); //ensure element is added
expect(this.element.children().length).toBe(1); //ensure element is added

expect(this.element.children().css('display')).toBe('block');//ensure its visible (promise is ongoing)
expect(this.element.children().css('display')).toBe('block');//ensure its visible (promise is ongoing)

this.testPromise.resolve();
scope.$apply();
this.testPromise.resolve();
scope.$apply();

expect(this.element.children().css('display')).toBe('block'); //ensure its still visible (promise is ongoing)
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
});
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(){
it('should load custom templates', function(){

this.element = compile('<div cg-busy="{promise:my_promise,templateUrl:\'test-custom-template.html\'}"></div>')(scope);
angular.element('body').append(this.element);
this.element = compile('<div cg-busy="{promise:my_promise,templateUrl:\'test-custom-template.html\'}"></div>')(scope);
angular.element('body').append(this.element);

httpBackend.flush();
httpBackend.flush();

scope.$apply();
scope.$apply();

expect(angular.element('#custom').html()).toBe('test-custom-template-contents');
expect(angular.element('#custom').html()).toBe('test-custom-template-contents');

});

it('should delay when delay provided.', function() {

this.element = compile('<div cg-busy="{promise:my_promise,delay:300}"></div>')(scope);
angular.element('body').append(this.element);

this.testPromise = q.defer();
scope.my_promise = this.testPromise.promise;

scope.$apply();

expect(this.element.children().length).toBe(1); //ensure element is added

expect(this.element.children().css('display')).toBe('none');

timeout.flush(200);
expect(this.element.children().css('display')).toBe('none');

timeout.flush(301);
expect(this.element.children().css('display')).toBe('block');
this.testPromise.resolve();
scope.$apply();
expect(this.element.children().css('display')).toBe('none');

});

it('should use minDuration correctly.', function() {

this.element = compile('<div cg-busy="{promise:my_promise,minDuration:1000}"></div>')(scope);
angular.element('body').append(this.element);

this.testPromise = q.defer();
scope.my_promise = this.testPromise.promise;

scope.$apply();

expect(this.element.children().length).toBe(1); //ensure element is added

expect(this.element.children().css('display')).toBe('block');

timeout.flush(200);
expect(this.element.children().css('display')).toBe('block');

this.testPromise.resolve();
timeout.flush(400);
expect(this.element.children().css('display')).toBe('block');

timeout.flush(300); //900ms total
expect(this.element.children().css('display')).toBe('block');

timeout.flush(101); //1001ms total
expect(this.element.children().css('display')).toBe('none');

});

})
});

0 comments on commit c8193eb

Please sign in to comment.