Add booleanAsPromise to q.extensions service
This adds a method to create a promise for a boolean. The method is used in downstream patches https://review.openstack.org/#/c/217422/ https://review.openstack.org/#/c/229677/ https://review.openstack.org/#/c/217964/ Change-Id: I1aba32644a1b874970f0ab9b41d3319afbef8972 Partially-Implements: blueprint angularize-images-table
This commit is contained in:
parent
cb9f3637fa
commit
6d9204cd4e
@ -32,7 +32,8 @@
|
||||
*/
|
||||
function qExtensions($q) {
|
||||
var service = {
|
||||
allSettled: allSettled
|
||||
allSettled: allSettled,
|
||||
booleanAsPromise: booleanAsPromise
|
||||
};
|
||||
|
||||
return service;
|
||||
@ -64,10 +65,10 @@
|
||||
* The order of the resolve or rejection reasons is non-deterministic
|
||||
* and should not be relied upon for correlation to input promises.
|
||||
*
|
||||
* @param promiseList
|
||||
* @param {array} promiseList
|
||||
* The list of promises to resolve
|
||||
*
|
||||
* @return
|
||||
* @return {object}
|
||||
* An object with 2 lists, one for promises that got resolved
|
||||
* and one for promises that got rejected.
|
||||
*
|
||||
@ -126,5 +127,27 @@
|
||||
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';
|
||||
|
||||
describe('horizon.framework.util.q.extensions', function () {
|
||||
var service, $q, $scope;
|
||||
|
||||
var failedPromise = function() {
|
||||
var deferred2 = $q.defer();
|
||||
deferred2.reject('failed');
|
||||
return deferred2.promise;
|
||||
};
|
||||
describe('allSettled', function() {
|
||||
var service, $q, $scope;
|
||||
|
||||
var passedPromise = function() {
|
||||
var deferred1 = $q.defer();
|
||||
deferred1.resolve('passed');
|
||||
return deferred1.promise;
|
||||
};
|
||||
var failedPromise = function() {
|
||||
var deferred2 = $q.defer();
|
||||
deferred2.reject('failed');
|
||||
return deferred2.promise;
|
||||
};
|
||||
|
||||
beforeEach(module('horizon.framework.util.q'));
|
||||
beforeEach(inject(function($injector, _$rootScope_) {
|
||||
service = $injector.get('horizon.framework.util.q.extensions');
|
||||
$q = $injector.get('$q');
|
||||
$scope = _$rootScope_.$new();
|
||||
}));
|
||||
var passedPromise = function() {
|
||||
var deferred1 = $q.defer();
|
||||
deferred1.resolve('passed');
|
||||
return deferred1.promise;
|
||||
};
|
||||
|
||||
it('should define allSettled', function () {
|
||||
expect(service.allSettled).toBeDefined();
|
||||
beforeEach(module('horizon.framework.util.q'));
|
||||
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() {
|
||||
service.allSettled([{
|
||||
promise: failedPromise(),
|
||||
context: '1'
|
||||
}, {
|
||||
promise: passedPromise(),
|
||||
context: '2'
|
||||
}]).then(onAllSettled, failTest);
|
||||
describe('booleanAsPromise', function() {
|
||||
var service, $scope;
|
||||
|
||||
$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() {
|
||||
expect(false).toBeTruthy();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
})();
|
||||
|
Loading…
Reference in New Issue
Block a user