Merge "Add booleanAsPromise to q.extensions service"
This commit is contained in:
commit
23fcba3eb5
@ -32,7 +32,8 @@
|
|||||||
*/
|
*/
|
||||||
function qExtensions($q) {
|
function qExtensions($q) {
|
||||||
var service = {
|
var service = {
|
||||||
allSettled: allSettled
|
allSettled: allSettled,
|
||||||
|
booleanAsPromise: booleanAsPromise
|
||||||
};
|
};
|
||||||
|
|
||||||
return service;
|
return service;
|
||||||
@ -64,10 +65,10 @@
|
|||||||
* The order of the resolve or rejection reasons is non-deterministic
|
* The order of the resolve or rejection reasons is non-deterministic
|
||||||
* and should not be relied upon for correlation to input promises.
|
* and should not be relied upon for correlation to input promises.
|
||||||
*
|
*
|
||||||
* @param promiseList
|
* @param {array} promiseList
|
||||||
* The list of promises to resolve
|
* The list of promises to resolve
|
||||||
*
|
*
|
||||||
* @return
|
* @return {object}
|
||||||
* An object with 2 lists, one for promises that got resolved
|
* An object with 2 lists, one for promises that got resolved
|
||||||
* and one for promises that got rejected.
|
* and one for promises that got rejected.
|
||||||
*
|
*
|
||||||
@ -126,5 +127,27 @@
|
|||||||
deferred.resolve({pass: passList, fail: failList});
|
deferred.resolve({pass: passList, fail: failList});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a promise that resolves if true, otherwise is rejected.
|
||||||
|
*
|
||||||
|
* @param {expression} value
|
||||||
|
* the boolean value to test
|
||||||
|
*
|
||||||
|
* @return {promise}
|
||||||
|
* the promise object
|
||||||
|
*/
|
||||||
|
function booleanAsPromise(value) {
|
||||||
|
var deferred = $q.defer();
|
||||||
|
|
||||||
|
if (value === true) {
|
||||||
|
deferred.resolve();
|
||||||
|
} else {
|
||||||
|
deferred.reject();
|
||||||
|
}
|
||||||
|
|
||||||
|
return deferred.promise;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
@ -15,54 +15,89 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
describe('horizon.framework.util.q.extensions', function () {
|
describe('horizon.framework.util.q.extensions', function () {
|
||||||
var service, $q, $scope;
|
|
||||||
|
|
||||||
var failedPromise = function() {
|
describe('allSettled', function() {
|
||||||
var deferred2 = $q.defer();
|
var service, $q, $scope;
|
||||||
deferred2.reject('failed');
|
|
||||||
return deferred2.promise;
|
|
||||||
};
|
|
||||||
|
|
||||||
var passedPromise = function() {
|
var failedPromise = function() {
|
||||||
var deferred1 = $q.defer();
|
var deferred2 = $q.defer();
|
||||||
deferred1.resolve('passed');
|
deferred2.reject('failed');
|
||||||
return deferred1.promise;
|
return deferred2.promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
beforeEach(module('horizon.framework.util.q'));
|
var passedPromise = function() {
|
||||||
beforeEach(inject(function($injector, _$rootScope_) {
|
var deferred1 = $q.defer();
|
||||||
service = $injector.get('horizon.framework.util.q.extensions');
|
deferred1.resolve('passed');
|
||||||
$q = $injector.get('$q');
|
return deferred1.promise;
|
||||||
$scope = _$rootScope_.$new();
|
};
|
||||||
}));
|
|
||||||
|
|
||||||
it('should define allSettled', function () {
|
beforeEach(module('horizon.framework.util.q'));
|
||||||
expect(service.allSettled).toBeDefined();
|
beforeEach(inject(function($injector, _$rootScope_) {
|
||||||
|
service = $injector.get('horizon.framework.util.q.extensions');
|
||||||
|
$q = $injector.get('$q');
|
||||||
|
$scope = _$rootScope_.$new();
|
||||||
|
}));
|
||||||
|
|
||||||
|
it('should define allSettled', function () {
|
||||||
|
expect(service.allSettled).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should resolve all given promises', function() {
|
||||||
|
service.allSettled([{
|
||||||
|
promise: failedPromise(),
|
||||||
|
context: '1'
|
||||||
|
}, {
|
||||||
|
promise: passedPromise(),
|
||||||
|
context: '2'
|
||||||
|
}]).then(onAllSettled, failTest);
|
||||||
|
|
||||||
|
$scope.$apply();
|
||||||
|
|
||||||
|
function onAllSettled(resolvedPromises) {
|
||||||
|
expect(resolvedPromises.fail.length).toEqual(1);
|
||||||
|
expect(resolvedPromises.fail[0]).toEqual({data: 'failed', context: '1'});
|
||||||
|
expect(resolvedPromises.pass.length).toEqual(1);
|
||||||
|
expect(resolvedPromises.pass[0]).toEqual({data: 'passed', context: '2'});
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should resolve all given promises', function() {
|
describe('booleanAsPromise', function() {
|
||||||
service.allSettled([{
|
var service, $scope;
|
||||||
promise: failedPromise(),
|
|
||||||
context: '1'
|
|
||||||
}, {
|
|
||||||
promise: passedPromise(),
|
|
||||||
context: '2'
|
|
||||||
}]).then(onAllSettled, failTest);
|
|
||||||
|
|
||||||
$scope.$apply();
|
beforeEach(module('horizon.framework.util.q'));
|
||||||
|
beforeEach(inject(function($injector, _$rootScope_) {
|
||||||
|
service = $injector.get('horizon.framework.util.q.extensions');
|
||||||
|
$scope = _$rootScope_.$new();
|
||||||
|
}));
|
||||||
|
|
||||||
|
it('should define booleanAsPromise', function () {
|
||||||
|
expect(service.booleanAsPromise).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should reject the promise if condition does not evaluates to true', function() {
|
||||||
|
service.booleanAsPromise(false).then(failTest, angular.noop);
|
||||||
|
$scope.$apply();
|
||||||
|
service.booleanAsPromise(null).then(failTest, angular.noop);
|
||||||
|
$scope.$apply();
|
||||||
|
service.booleanAsPromise({}).then(failTest, angular.noop);
|
||||||
|
$scope.$apply();
|
||||||
|
service.booleanAsPromise('A').then(failTest, angular.noop);
|
||||||
|
$scope.$apply();
|
||||||
|
service.booleanAsPromise(7).then(failTest, angular.noop);
|
||||||
|
$scope.$apply();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should resolve the promise only if condition to true', function() {
|
||||||
|
service.booleanAsPromise(true).then(angular.noop, failTest);
|
||||||
|
$scope.$apply();
|
||||||
|
});
|
||||||
|
|
||||||
function onAllSettled(resolvedPromises) {
|
|
||||||
expect(resolvedPromises.fail.length).toEqual(1);
|
|
||||||
expect(resolvedPromises.fail[0]).toEqual({data: 'failed', context: '1'});
|
|
||||||
expect(resolvedPromises.pass.length).toEqual(1);
|
|
||||||
expect(resolvedPromises.pass[0]).toEqual({data: 'passed', context: '2'});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
function failTest() {
|
function failTest() {
|
||||||
expect(false).toBeTruthy();
|
expect(false).toBeTruthy();
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
Loading…
Reference in New Issue
Block a user