Merge "Add booleanAsPromise to q.extensions service"

This commit is contained in:
Jenkins 2016-01-06 21:05:55 +00:00 committed by Gerrit Code Review
commit 23fcba3eb5
2 changed files with 96 additions and 38 deletions

View File

@ -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;
}
} }
})(); })();

View File

@ -15,6 +15,8 @@
'use strict'; 'use strict';
describe('horizon.framework.util.q.extensions', function () { describe('horizon.framework.util.q.extensions', function () {
describe('allSettled', function() {
var service, $q, $scope; var service, $q, $scope;
var failedPromise = function() { var failedPromise = function() {
@ -58,11 +60,44 @@
expect(resolvedPromises.pass[0]).toEqual({data: 'passed', context: '2'}); expect(resolvedPromises.pass[0]).toEqual({data: 'passed', context: '2'});
} }
}); });
});
describe('booleanAsPromise', function() {
var service, $scope;
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 failTest() { function failTest() {
expect(false).toBeTruthy(); expect(false).toBeTruthy();
} }
}); });
})(); })();