Commit 628b7644 authored by Julien's avatar Julien

Merge pull request #1689 from bonitasoft/tr/remove-unecessary-waitFor

Remove unecessary waitFor due to new data model implementation
parents 94585621 432b5397
......@@ -12,19 +12,19 @@ function PbButtonCtrl($scope, $http, $timeout, $location, $log, $window) {
} else if ($scope.properties.action === "Start process") {
id = getUrlParam('id');
if (id) {
doRequestDelayed('POST', '../API/bpm/process/' + id + '/instantiation', getUserParam());
doRequest('POST', '../API/bpm/process/' + id + '/instantiation', getUserParam());
} else {
$log.log('Impossible to retrieve the process definition id value from the URL');
}
} else if ($scope.properties.action === 'Submit task') {
id = getUrlParam('id');
if (id) {
doRequestDelayed('POST', '../API/bpm/userTask/' + getUrlParam('id') + '/execution', getUserParam());
doRequest('POST', '../API/bpm/userTask/' + getUrlParam('id') + '/execution', getUserParam());
} else {
$log.log('Impossible to retrieve the task id value from the URL');
}
} else if ($scope.properties.url) {
doRequestDelayed($scope.properties.action, $scope.properties.url);
doRequest($scope.properties.action, $scope.properties.url);
}
};
......@@ -65,14 +65,6 @@ function PbButtonCtrl($scope, $http, $timeout, $location, $log, $window) {
}
}
// we delayed the doRequest to ensure dataToSend is updated
// this usefull when copy() update the dataToSend object.
function doRequestDelayed(method, url, params) {
$scope.properties
.waitFor('dataToSend')
.then(doRequest.bind(null, method, url, params));
}
/**
* Execute a get/post request to an URL
* It also bind custom data from success|error to a data
......@@ -130,4 +122,4 @@ function PbButtonCtrl($scope, $http, $timeout, $location, $log, $window) {
}
return '';
}
}
\ No newline at end of file
}
......@@ -2,7 +2,7 @@
'use strict';
angular.module('bonitasoft.ui.directives')
.directive('pbPropertyValues', function($parse, $q, $timeout, $log, bindingsFactory, propertyValuesFactory, bindingContextFactory, pendingStatus) {
.directive('pbPropertyValues', function($parse, $q, $timeout, $log, bindingsFactory, propertyValuesFactory, bindingContextFactory) {
function pbPropertyValuesCompile(tElement, tAttributes) {
return {
......@@ -18,18 +18,6 @@
pbPropertyValues[propertyName].type === 'variable' &&
!!pbPropertyValues[propertyName].value;
};
$scope.properties.waitFor = function(propertyName) {
// This comment is mandatory to force variable resolution
$log.log('resolving property ', propertyName,' Current value: ', $scope.properties[propertyName]);
var defer = $q.defer();
var removeFn = pendingStatus.listen(function() {
defer.resolve();
});
return defer.promise.then(removeFn);
};
}
};
}
......
(function() {
'use strict';
angular.module('bonitasoft.ui')
.config(function($httpProvider) {
$httpProvider.interceptors.push('httpActivityInterceptor');
});
})();
(function() {
'use strict';
function PendingStatus($timeout) {
var pendingRequests = 0;
var listenners = [];
this.addPendingRequest = addPendingRequest;
this.removePendingRequest = removePendingRequest;
this.isPending = isPending;
this.listen = listen;
function addPendingRequest() {
pendingRequests += 1;
}
function removePendingRequest() {
pendingRequests -= 1;
if (pendingRequests === 0) {
listenners.forEach(function(handler) {
handler();
});
}
}
function isPending() {
return pendingRequests > 0;
}
function listen(handler) {
listenners = listenners.concat(handler);
$timeout(function() {
if (pendingRequests === 0) {
handler();
}
}, 0, false);
return function() {
var index = listenners.indexOf(handler);
listenners = listenners.filter(function(fn, i) {
return index !== i;
});
};
}
}
function httpActivityInterceptor($q, pendingStatus) {
return {
request: requestHandler,
response: responseHandler,
responseError: responseErrorHandler
};
function requestHandler(config) {
if (config.method === 'GET') {
pendingStatus.addPendingRequest();
}
return config;
}
function responseHandler(response) {
if (shouldRemovePendingRequest(response)) {
pendingStatus.removePendingRequest();
}
return (response);
}
function responseErrorHandler(response) {
if (shouldRemovePendingRequest(response)) {
pendingStatus.removePendingRequest();
}
return $q.reject(response);
}
function shouldRemovePendingRequest(response) {
return response.config && response.config.method === 'GET';
}
}
angular
.module('bonitasoft.ui.services')
.service('pendingStatus', PendingStatus)
.service('httpActivityInterceptor', httpActivityInterceptor);
})();
......@@ -67,13 +67,12 @@ describe('Directive: propertyValues', function () {
}
};
var $scope, modelFactory, $compile, $timeout, pendingStatus;
var $scope, modelFactory, $compile, $timeout;
beforeEach(inject(function ($rootScope, _$compile_, _modelFactory_, _$timeout_, _pendingStatus_) {
beforeEach(inject(function ($rootScope, _$compile_, _modelFactory_, _$timeout_) {
$compile = _$compile_;
modelFactory = _modelFactory_;
$timeout = _$timeout_;
pendingStatus = _pendingStatus_;
$scope = $rootScope.$new();
$scope.properties = properties;
}));
......@@ -132,50 +131,6 @@ describe('Directive: propertyValues', function () {
expect(element.find('div').scope().properties.isBound('notExisting')).toBe(false);
});
describe('waitfor', function(){
it('should return a promise', function() {
var element = compileTemplate();
$scope.$apply();
var scope = element.find('div').scope();
var handler = jasmine.createSpy('handler');
var promise = scope.properties.waitFor('baz');
expect(promise).toBeDefined();
expect(promise.then).toBeDefined();
expect(handler).not.toHaveBeenCalled();
});
it('should resolve waitFor if it doesn\'t depends on an External API', function() {
var element = compileTemplate();
$scope.$apply();
var scope = element.find('div').scope();
var handler = jasmine.createSpy('handler');
scope.properties.waitFor('baz').then(handler);
$timeout.flush();
expect(handler).toHaveBeenCalled();
});
it('should resolve waitFor after External API had been resolved', function() {
var element = compileTemplate();
$scope.$apply();
var scope = element.find('div').scope();
var handler = jasmine.createSpy('handler');
pendingStatus.addPendingRequest();
scope.properties.waitFor('baz').then(handler);
expect(handler).not.toHaveBeenCalled();
pendingStatus.removePendingRequest();
$timeout.flush();
expect(handler).toHaveBeenCalled();
});
});
describe('in a repeated context', function () {
function compileRepeatedTemplate() {
......
(function () {
'use strict';
describe('bonitasoft.ui module and its dependencies to former module', function() {
beforeEach(function() {
describe('bonitasoft.ui module', function () {
beforeEach(function () {
//these modules are not loaded in karma conf
angular.module('ngMessages', []);
angular.module('ngUpload', []);
angular.module('bonitasoft.ui.templates', []);
});
beforeEach(module('bonitasoft.ui'));
beforeEach(function() {
beforeEach(function () {
angular.module('pb.generator').filter('someTimeAgo', function () {
return function someTimeAgo() {
return 'some time ago...';
};
});
angular.module('pb.widgets').directive('pbTestWidget', function(){
angular.module('pb.widgets').directive('pbTestWidget', function () {
return {
restrict: 'AE',
template: '<div id="testWidget">{{ "Bonitasoft" | someTimeAgo }}</div>'
};
});
});
it('should instanciate the directive', inject(function($compile, $rootScope) {
var element = $compile('<pb-test-widget></pb-test-widget')($rootScope);
it('should still be compatible with pb.widgets', inject(function ($compile, $rootScope) {
var element = $compile('<pb-test-widget></pb-test-widget>')($rootScope);
$rootScope.$apply();
expect(element.find('#testWidget').text()).toEqual('some time ago...');
}));
});
})();
describe('pendingStatus service', function() {
var service, $timeout;
beforeEach(module('bonitasoft.ui.services'));
beforeEach(inject(function($injector) {
service = $injector.get('pendingStatus');
$timeout = $injector.get('$timeout');
}));
it('should track a pending request', function(){
expect(service.isPending()).toBe(false);
service.addPendingRequest()
expect(service.isPending()).toBe(true);
service.removePendingRequest()
expect(service.isPending()).toBe(false);
});
it('should trigger listener just after listen', function(){
var handler1 = jasmine.createSpy('handler1');
service.listen(handler1)
$timeout.flush();
expect(handler1).toHaveBeenCalled();
});
it('should trigger listener when pending request equal 0', function(){
var handler1 = jasmine.createSpy('handler1');
var handler2 = jasmine.createSpy('handler2');
service.addPendingRequest();
service.listen(handler1)
$timeout.flush();
service.removePendingRequest();
expect(handler1).toHaveBeenCalled();
expect(handler1.calls.count()).toBe(1);
});
describe('httpActivityInterceptor', function(){
var interceptor;
beforeEach(inject(function($injector) {
interceptor = $injector.get('httpActivityInterceptor');
spyOn(service, 'addPendingRequest');
spyOn(service, 'removePendingRequest');
}));
it('should track get request', function(){
interceptor.request({method: 'GET'});
expect(service.addPendingRequest).toHaveBeenCalled();
});
it('should not track non GET request', function(){
interceptor.request({method: 'PUT'});
interceptor.request({method: 'POST'});
interceptor.request({method: 'DELETE'});
expect(service.addPendingRequest).not.toHaveBeenCalled();
});
it('should track response', function(){
interceptor.request({method: 'GET'});
interceptor.response({ config: {method: 'GET'}});
expect(service.removePendingRequest).toHaveBeenCalled();
});
it('should not track non GET response', function(){
interceptor.response({ config: {method: 'PUT'}});
interceptor.response({ config: {method: 'POST'}});
interceptor.response({ config: {method: 'DELETE'}});
expect(service.removePendingRequest).not.toHaveBeenCalled();
});
it('should track response error', function(){
interceptor.request({method: 'GET'});
interceptor.responseError({ config: {method: 'GET'}});
expect(service.removePendingRequest).toHaveBeenCalled();
});
it('should not track non GET response error', function(){
interceptor.responseError({ config: {method: 'PUT'}});
interceptor.responseError({ config: {method: 'POST'}});
interceptor.responseError({ config: {method: 'DELETE'}});
expect(service.removePendingRequest).not.toHaveBeenCalled();
});
})
})
......@@ -20,10 +20,7 @@ describe('pbButton', function () {
scope = $rootScope.$new();
// set the default value for property method
scope.properties = {
method: 'Submit task',
waitFor: function(){
return $q.when(true);
}
method: 'Submit task'
};
element = $compile('<pb-button></pb-button>')(scope);
......
describe('pbImage', function () {
var $compile, scope,
defaultImage = "data:image/svg+xml,%3Csvg%20xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg'%20viewBox%3D'0%200%2050%2020'%3E%3Cpath%20fill%3D'%23ccc'%20d%3D'M10%201v18h30V1H10zm29%2017H11V2h28v16zM20%209.1l3%203%205-7L36%2016H14l6-6.9zM18%206c0%201.1-.9%202-2%202s-2-.9-2-2%20.9-2%202-2%202%20.9%202%202z'%2F%3E%3C%2Fsvg%3E";
defaultImage = "data:image/svg+xml,%3Csvg%20xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg'%20viewBox%3D'0%200%2050%2020'%3E%3Cpath%20fill%3D'%23ccc'%20d%3D'M10%201v18h30V1H10zm29%2017H11V2h28v16zM20%209.1l3%203%205-7L36%2016H14l6-6.9zM18%206c0%201.1-.9%202-2%202s-2-.9-2-2%20.9-2%202-2%202%20.9%202%202z'%2F%3E%3C%2Fsvg%3E";
beforeEach(module('bonitasoft.ui.widgets', 'ngSanitize'));
......@@ -22,7 +22,7 @@ describe('pbImage', function () {
expect(element.find('img').attr('alt')).toBeFalsy();
});
it('should contains src and alt html attributes', function() {
it('should contains src and alt html attributes', function () {
scope.properties.srcType = "Asset";
scope.properties.assetName = "bonita.jpg";
scope.properties.alt = "Bonitasoft Logo";
......@@ -81,7 +81,7 @@ describe('pbImage', function () {
var element = $compile('<pb-image></pb-image>')(scope);
scope.$apply();
expect(element.find('img').attr('ng-src')).toEqual('preview/page/'+pageId+'/assets/img/bonita.jpg');
expect(element.find('img').attr('ng-src')).toEqual('preview/page/' + pageId + '/assets/img/bonita.jpg');
expect(element.find('img').attr('class')).toContain("img-responsive");
expect(element.find('img').attr('alt')).toBeFalsy();
});
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment