Skip to content

Commit f3bf313

Browse files
imranamansmportuga
authored andcommitted
fix for #5620: runValidators function does not return a promise (#5626)
fix(5620): runValidators now returns a promise Updated runValidator to return a promise and updated unit tests. Issue #5620
1 parent 1e99d93 commit f3bf313

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/features/validate/js/gridValidate.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,16 +356,21 @@
356356
};
357357
};
358358

359+
var promises = [];
360+
359361
for (var validatorName in colDef.validators) {
360362
service.clearError(rowEntity, colDef, validatorName);
361363
var msg;
362364
var validatorFunction = service.getValidator(validatorName, colDef.validators[validatorName]);
363365
// We pass the arguments as oldValue, newValue so they are in the same order
364366
// as ng-model validators (modelValue, viewValue)
365-
$q.when(validatorFunction(oldValue, newValue, rowEntity, colDef))
366-
.then(validateClosureFactory(rowEntity, colDef, validatorName)
367-
);
367+
var promise = $q
368+
.when(validatorFunction(oldValue, newValue, rowEntity, colDef))
369+
.then(validateClosureFactory(rowEntity, colDef, validatorName));
370+
promises.push(promise);
368371
}
372+
373+
return $q.all(promises);
369374
},
370375

371376
/**
@@ -576,4 +581,4 @@
576581
}
577582
};
578583
}]);
579-
})();
584+
})();

src/features/validate/test/uiGridValidateService.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,28 @@ describe('ui.grid.validate uiGridValidateService', function () {
132132
expect(entity['$$errorstest'].foo).toBeFalsy();
133133

134134

135+
});
136+
137+
it('should return a promise when calling runValidators on a cell', function() {
138+
var colDef = {name: 'test', validators: {foo: 'foo', bar: 'bar'}};
139+
var entity = {};
140+
141+
var validatorFactory = function (argument) {return function() {return argument === 'foo';};};
142+
143+
uiGridValidateService.setValidator('foo', validatorFactory, angular.noop);
144+
uiGridValidateService.setValidator('bar', validatorFactory, angular.noop);
145+
146+
var promise = uiGridValidateService.runValidators(entity, colDef, 1, 0);
147+
148+
expect(promise).toBeDefined();
149+
150+
$rootScope.$apply();
151+
152+
expect(entity['$$errorstest'].bar).toBe(true);
153+
expect(entity['$$invalidtest']).toBe(true);
154+
155+
expect(entity['$$errorstest'].foo).toBeFalsy();
156+
135157
});
136158

137159
it('should not execute any validator when calling runValidators with newValue === oldValue', function() {

0 commit comments

Comments
 (0)