This repository has been archived by the owner on Aug 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 260
/
directive_test.js
229 lines (174 loc) · 7.84 KB
/
directive_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
describe('directive: vcRecaptcha', function () {
'use strict';
var $scope, $compile, $timeout, vcRecaptchaService,
TIMEOUT_SESSION_CAPTCHA = 2 * 60 * 1000, // 2 minutes
VALID_KEY = '1234567890123456789012345678901234567890';
beforeEach(function () {
module('vcRecaptcha');
inject(function ($rootScope, _$compile_, _$timeout_, _vcRecaptchaService_) {
$scope = $rootScope.$new();
$compile = _$compile_;
$timeout = _$timeout_;
vcRecaptchaService = _vcRecaptchaService_;
});
});
describe('invalid key', function () {
var elementHtml, expectedMessage;
afterEach(function () {
expect(function () {
var element = angular.element(elementHtml);
$compile(element)($scope);
$scope.$digest();
}).toThrow(new Error(expectedMessage));
});
it('should throw an error - no key', function () {
elementHtml = '<div vc-recaptcha></div>';
expectedMessage = 'You need to set the "key" attribute to your public reCaptcha key. If you don\'t have a key, please get one from https://www.google.com/recaptcha/admin/create';
});
});
describe('widgetId', function () {
it('should be null at start', function () {
var element = angular.element('<div vc-recaptcha key="key"></div>');
$scope.key = VALID_KEY;
expect(function () {
$compile(element)($scope);
$scope.$digest();
}).not.toThrow();
expect(element.isolateScope().widgetId).toBeNull();
});
});
describe('form validation', function () {
beforeEach(function () {
$scope.key = VALID_KEY;
$scope.onCreate = jasmine.createSpy('onCreate');
$scope.onSuccess = jasmine.createSpy('onSuccess');
$scope.onError = jasmine.createSpy('onError');
});
afterEach(function () {
expect(vcRecaptchaService.create).toHaveBeenCalled();
});
it('should change the validation to false, widget just created', function () {
var element = angular.element(
'<form name="form">' +
'<input type="text" ng-model="something" />' +
'<div vc-recaptcha key="key" on-create="onCreate({widgetId: widgetId})" />' +
'</form>'
),
_fakeCreate = function () {
return {
then: function (cb) {
var _widgetId = 'a';
cb(_widgetId);
}
};
};
spyOn(vcRecaptchaService, 'create').and.callFake(_fakeCreate);
$compile(element)($scope);
$scope.$digest();
expect($scope.form.$valid).toBeFalsy(); // widgetCreated
expect($scope.onCreate).toHaveBeenCalledWith({widgetId: 'a'});
});
it('should change the validation to true - first timeout flushed', function () {
var element = angular.element('<form name="form">' +
'<input type="text" ng-model="something" />' +
'<div vc-recaptcha key="k" on-create="onCreate()" on-success="onSuccess()"/>' +
'</form>'),
_fakeCreate = function (element, config) {
config.callback('response from google');
return {
then: function (cb) {
cb();
}
};
};
spyOn(vcRecaptchaService, 'create').and.callFake(_fakeCreate);
$compile(element)($scope);
$scope.$digest();
$timeout.flush(TIMEOUT_SESSION_CAPTCHA - 1);
expect($scope.form.$valid).toBeTruthy();
});
it('should change the validation to false - session expired', function () {
var element = angular.element('<form name="form">' +
'<input type="text" ng-model="something" />' +
'<div vc-recaptcha key="k" on-create="onCreate()" on-success="onSuccess()"/>' +
'</form>'),
_fakeCreate = function (element, config) {
// Call the expiration callback as recaptcha would do.
config['expired-callback']();
return {
then: function (cb) {
cb();
}
};
};
spyOn(vcRecaptchaService, 'create').and.callFake(_fakeCreate);
$compile(element)($scope);
$scope.$digest();
$timeout.flush(TIMEOUT_SESSION_CAPTCHA + 1);
expect($scope.form.$valid).toBeFalsy(); // widgetCreated
});
it('should call the onSuccess callback with the right params', function () {
var element = angular.element('<form name="form">' +
'<input type="text" ng-model="something" />' +
'<div vc-recaptcha key="key" on-create="onCreate()" on-success="onSuccess({response: response, widgetId: id})"/>' +
'</form>'),
_fakeCreate = function (element, config) {
config.callback('response from google');
return {
then: function (cb) {
cb();
}
};
};
spyOn(vcRecaptchaService, 'create').and.callFake(_fakeCreate);
$compile(element)($scope);
$scope.$digest();
$timeout.flush();
expect($scope.onSuccess).toHaveBeenCalledWith({
response: 'response from google',
widgetId: undefined
});
});
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" />' +
'<div vc-recaptcha key="key" on-create="onCreate()" lang="es" on-success="onSuccess({response: response, widgetId: id})"/>' +
'</form>'),
_fakeCreate = function (element, config) {
config.callback(config.lang);
return {
then: function (cb) {
cb();
}
};
};
spyOn(vcRecaptchaService, 'create').and.callFake(_fakeCreate);
$compile(element)($scope);
$scope.$digest();
$timeout.flush();
expect($scope.onSuccess).toHaveBeenCalledWith({
response: 'es',
widgetId: undefined
});
});
});
});