Skip to content
This repository has been archived by the owner on Aug 17, 2021. It is now read-only.

feat(vcRecaptcha): add error callback support #230

Merged
merged 2 commits into from Jul 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/directive.js
Expand Up @@ -22,7 +22,8 @@
required: '=?',
onCreate: '&',
onSuccess: '&',
onExpire: '&'
onExpire: '&',
onError: '&'
},
link: function (scope, elm, attrs, ctrl) {
scope.widgetId = null;
Expand Down Expand Up @@ -54,7 +55,8 @@
tabindex: scope.tabindex || attrs.tabindex || null,
size: scope.size || attrs.size || null,
badge: scope.badge || attrs.badge || null,
'expired-callback': expired
'expired-callback': expired,
'error-callback': attrs.onError ? error : undefined

}).then(function (widgetId) {
// The widget has been created
Expand Down Expand Up @@ -96,6 +98,17 @@
scope.onExpire({ widgetId: scope.widgetId });
});
}

function error() {
var args = arguments;
$timeout(function () {
scope.response = "";
validate();

// Notify about the response availability
scope.onError({ widgetId: scope.widgetId, arguments: args });
});
}

function validate(){
if(ctrl){
Expand Down
26 changes: 26 additions & 0 deletions tests/directive_test.js
Expand Up @@ -58,6 +58,7 @@ describe('directive: vcRecaptcha', function () {
$scope.key = VALID_KEY;
$scope.onCreate = jasmine.createSpy('onCreate');
$scope.onSuccess = jasmine.createSpy('onSuccess');
$scope.onError = jasmine.createSpy('onError');
});

afterEach(function () {
Expand Down Expand Up @@ -173,6 +174,31 @@ describe('directive: vcRecaptcha', function () {
});
});

it('should call the onError callback', function () {
var element = angular.element('<form name="form">' +
'<input type="text" ng-model="something" />' +
'<div vc-recaptcha key="key" on-create="onCreate()" on-error="onError({arguments: args, widgetId: id})"/>' +
'</form>'),

_fakeCreate = function (element, config) {
config['error-callback']('something about the error');

return {
then: function (cb) {
cb();
}
};
};

spyOn(vcRecaptchaService, 'create').and.callFake(_fakeCreate);

$compile(element)($scope);
$scope.$digest();
$timeout.flush();

expect($scope.onError).toHaveBeenCalled();
});

it('the widget should be using the setted language', function () {
var element = angular.element('<form name="form">' +
'<input type="text" ng-model="something" />' +
Expand Down