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

Commit 3e725ab

Browse files
committed
Merge pull request #123 from VividCortex/ericmdantas-tests
Test suite
2 parents ec705f8 + 115d26a commit 3e725ab

File tree

9 files changed

+398
-7
lines changed

9 files changed

+398
-7
lines changed

.gitignore

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# IDE Files
2-
.idea/*
3-
angular-recaptcha.iml
2+
.idea
43

54
# OS Files
65
.DS_Store
76

87
# Node, Grunt, Bower, general build process files
9-
bower_components/*
10-
node_modules/*
8+
bower_components
9+
node_modules
10+
11+
# Coverage folder
12+
coverage

.travis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
language: node_js
2+
3+
node_js:
4+
- 'stable'
5+
6+
before_script:
7+
- npm install -g grunt-cli bower
8+
- bower install
9+
- "export DISPLAY=:99.0"
10+
- "sh -e /etc/init.d/xvfb start"
11+
12+
script:
13+
- grunt karma:ci

Gruntfile.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,36 @@ module.exports = function (grunt) {
4444
gitDescribeOptions: '--tags --always --abbrev=1 --dirty=-d' // options to use with '$ git describe'
4545
}
4646
},
47-
47+
karma: {
48+
unit: {
49+
configFile: 'karma.conf.js',
50+
browsers: ['Chrome'],
51+
singleRun: true
52+
},
53+
ci: {
54+
configFile: 'karma.conf.js',
55+
browsers: ['Chrome', 'Firefox', 'FirefoxNightly'],
56+
singleRun: true
57+
}
58+
},
59+
coveralls: {
60+
options: {
61+
coverageDir: 'coverage'
62+
}
63+
}
4864
});
4965

5066
// Load the plugin that provides the needed tasks.
5167
grunt.loadNpmTasks('grunt-contrib-concat');
5268
grunt.loadNpmTasks('grunt-contrib-uglify');
5369
grunt.loadNpmTasks('grunt-bump');
70+
grunt.loadNpmTasks('grunt-karma');
71+
grunt.loadNpmTasks('grunt-karma-coveralls');
5472

5573
// Default task(s).
56-
grunt.registerTask('default', ['concat', 'uglify']);
74+
grunt.registerTask('default', ['karma:unit', 'concat', 'uglify']);
5775

76+
// Unit Test task(s).
77+
grunt.registerTask('test', ['karma:unit']);
78+
grunt.registerTask('coverage', ['coveralls']);
5879
};

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
AngularJS reCaptcha
22
===================
33

4+
[![Build Status](https://travis-ci.org/VividCortex/angular-recaptcha.svg?branch=master)](https://travis-ci.org/VividCortex/angular-recaptcha)
5+
[![Coverage Status](https://coveralls.io/repos/VividCortex/angular-recaptcha/badge.svg?branch=master)](https://coveralls.io/r/VividCortex/angular-recaptcha?branch=master)
6+
47
Add a [reCaptcha](https://www.google.com/recaptcha/intro/index.html) to your [AngularJS](angularjs.org) project.
58

69

bower.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,9 @@
1212
],
1313
"dependencies": {
1414
"angular": "1.*"
15+
},
16+
"devDependencies": {
17+
"angular-mocks": "~1.*",
18+
"jquery": "~2.1.3"
1519
}
1620
}

karma.conf.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Karma configuration
2+
// Generated on Wed Dec 24 2014 19:30:10 GMT-0200 (Horário brasileiro de verão)
3+
4+
module.exports = function (config) {
5+
config.set({
6+
7+
// base path that will be used to resolve all patterns (eg. files, exclude)
8+
basePath: '',
9+
10+
11+
// frameworks to use
12+
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
13+
frameworks: ['jasmine'],
14+
15+
16+
// list of files / patterns to load in the browser
17+
files: [
18+
'bower_components/jquery/dist/jquery.min.js',
19+
'bower_components/angular/angular.js',
20+
21+
'bower_components/angular-mocks/angular-mocks.js',
22+
23+
'src/module.js',
24+
'src/*.js',
25+
26+
'tests/*_test.js'
27+
],
28+
29+
30+
// list of files to exclude
31+
exclude: [],
32+
33+
34+
// preprocess matching files before serving them to the browser
35+
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
36+
preprocessors: {
37+
'src/*.js': 'coverage'
38+
},
39+
40+
coverageReporter: {
41+
type: 'lcov',
42+
dir: 'coverage'
43+
},
44+
45+
46+
// test results reporter to use
47+
// possible values: 'dots', 'progress'
48+
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
49+
reporters: ['progress', 'coverage'],
50+
51+
52+
// web server port
53+
port: 9876,
54+
55+
56+
// enable / disable colors in the output (reporters and logs)
57+
colors: true,
58+
59+
60+
// level of logging
61+
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
62+
logLevel: config.LOG_INFO,
63+
64+
65+
// enable / disable watching file and executing tests whenever any file changes
66+
autoWatch: true,
67+
68+
69+
// start these browsers
70+
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
71+
browsers: ['PhantomJS', 'Chrome', 'IE', 'Safari', 'Firefox', 'FirefoxNightly', 'ChromeCanary'],
72+
73+
74+
// Continuous Integration mode
75+
// if true, Karma captures browsers, runs the tests and exits
76+
singleRun: false
77+
});
78+
};

package.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,27 @@
1010
"type": "git",
1111
"url": "git://github.com/vividcortex/angular-recaptcha.git"
1212
},
13+
"scripts": {
14+
"test": "grunt test"
15+
},
1316
"devDependencies": {
1417
"bower": "^1.3.3",
1518
"grunt": "~0.4.2",
1619
"grunt-bump": "0.0.13",
1720
"grunt-cli": "~0.1.11",
1821
"grunt-contrib-concat": "~0.3.0",
1922
"grunt-contrib-jshint": "~0.8.0",
20-
"grunt-contrib-uglify": "~0.4.0"
23+
"grunt-contrib-uglify": "~0.4.0",
24+
"grunt-karma": "^0.10.1",
25+
"grunt-karma-coveralls": "^2.5.3",
26+
"jasmine-core": "^2.2.0",
27+
"karma": "^0.12.31",
28+
"karma-chrome-launcher": "^0.1.7",
29+
"karma-coverage": "^0.2.7",
30+
"karma-firefox-launcher": "^0.1.4",
31+
"karma-ie-launcher": "^0.1.5",
32+
"karma-jasmine": "^0.3.5",
33+
"karma-phantomjs-launcher": "^0.1.4",
34+
"karma-safari-launcher": "^0.1.1"
2135
}
2236
}

tests/directive_test.js

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
describe('directive: vcRecaptcha', function () {
2+
'use strict';
3+
4+
5+
var $scope, $compile, $timeout, vcRecaptchaService,
6+
7+
TIMEOUT_SESSION_CAPTCHA = 2 * 60 * 1000, // 2 minutes
8+
VALID_KEY = '1234567890123456789012345678901234567890';
9+
10+
beforeEach(function () {
11+
module('vcRecaptcha');
12+
13+
14+
inject(function ($rootScope, _$compile_, _$timeout_, _vcRecaptchaService_) {
15+
$scope = $rootScope.$new();
16+
$compile = _$compile_;
17+
$timeout = _$timeout_;
18+
19+
vcRecaptchaService = _vcRecaptchaService_;
20+
});
21+
});
22+
23+
describe('invalid key', function () {
24+
var elementHtml, expectedMessage;
25+
26+
afterEach(function () {
27+
expect(function () {
28+
var element = angular.element(elementHtml);
29+
30+
$compile(element)($scope);
31+
$scope.$digest();
32+
}).toThrow(new Error(expectedMessage));
33+
});
34+
35+
it('should throw an error - no key', function () {
36+
elementHtml = '<div vc-recaptcha></div>';
37+
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';
38+
});
39+
40+
it('should throw an error - key length is not 40 caracters long', function () {
41+
elementHtml = '<div vc-recaptcha key="key"></div>';
42+
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';
43+
44+
$scope.key = 'abc';
45+
});
46+
47+
it('should throw an error - key length is not 40 caracters long - key changed', function () {
48+
elementHtml = '<div vc-recaptcha key="key"></div>';
49+
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';
50+
51+
$scope.key = 'abc1';
52+
});
53+
});
54+
55+
describe('widgetId', function () {
56+
it('should be null at start', function () {
57+
var element = angular.element('<div vc-recaptcha key="key"></div>');
58+
59+
$scope.key = VALID_KEY;
60+
61+
expect(function () {
62+
$compile(element)($scope);
63+
$scope.$digest();
64+
}).not.toThrow();
65+
66+
expect(element.isolateScope().widgetId).toBeNull();
67+
});
68+
});
69+
70+
describe('form validation', function () {
71+
beforeEach(function () {
72+
$scope.key = VALID_KEY;
73+
$scope.onCreate = jasmine.createSpy('onCreate');
74+
$scope.onSuccess = jasmine.createSpy('onSuccess');
75+
});
76+
77+
afterEach(function () {
78+
expect(vcRecaptchaService.create).toHaveBeenCalled();
79+
});
80+
81+
it('should change the validation to false, widget just created', function () {
82+
var element = angular.element(
83+
'<form name="form">' +
84+
'<input type="text" ng-model="something" />' +
85+
'<div vc-recaptcha key="key" on-create="onCreate({widgetId: widgetId})" />' +
86+
'</form>'
87+
),
88+
89+
_fakeCreate = function () {
90+
return {
91+
then: function (cb) {
92+
var _widgetId = 'a';
93+
94+
cb(_widgetId);
95+
}
96+
};
97+
};
98+
99+
spyOn(vcRecaptchaService, 'create').and.callFake(_fakeCreate);
100+
101+
$compile(element)($scope);
102+
$scope.$digest();
103+
104+
expect($scope.form.$valid).toBeFalsy(); // widgetCreated
105+
expect($scope.onCreate).toHaveBeenCalledWith({widgetId: 'a'});
106+
});
107+
108+
it('should change the validation to true - first timeout flushed', function () {
109+
var element = angular.element('<form name="form">' +
110+
'<input type="text" ng-model="something" />' +
111+
'<div vc-recaptcha key="k" on-create="onCreate()" on-success="onSuccess()"/>' +
112+
'</form>'),
113+
114+
_fakeCreate = function (element, config) {
115+
config.callback('response from google');
116+
117+
return {
118+
then: function (cb) {
119+
cb();
120+
}
121+
};
122+
};
123+
124+
spyOn(vcRecaptchaService, 'create').and.callFake(_fakeCreate);
125+
126+
$compile(element)($scope);
127+
$scope.$digest();
128+
129+
$timeout.flush(TIMEOUT_SESSION_CAPTCHA - 1);
130+
131+
expect($scope.form.$valid).toBeTruthy();
132+
});
133+
134+
it('should change the validation to false - session expired', function () {
135+
var element = angular.element('<form name="form">' +
136+
'<input type="text" ng-model="something" />' +
137+
'<div vc-recaptcha key="k" on-create="onCreate()" on-success="onSuccess()"/>' +
138+
'</form>'),
139+
140+
_fakeCreate = function (element, config) {
141+
// Call the expiration callback as recaptcha would do.
142+
config['expired-callback']();
143+
144+
return {
145+
then: function (cb) {
146+
cb();
147+
}
148+
};
149+
};
150+
151+
spyOn(vcRecaptchaService, 'create').and.callFake(_fakeCreate);
152+
153+
154+
$compile(element)($scope);
155+
$scope.$digest();
156+
157+
$timeout.flush(TIMEOUT_SESSION_CAPTCHA + 1);
158+
159+
expect($scope.form.$valid).toBeFalsy(); // widgetCreated
160+
});
161+
162+
it('should call the onSuccess callback with the right params', function () {
163+
var element = angular.element('<form name="form">' +
164+
'<input type="text" ng-model="something" />' +
165+
'<div vc-recaptcha key="key" on-create="onCreate()" on-success="onSuccess({response: response, widgetId: id})"/>' +
166+
'</form>'),
167+
168+
_fakeCreate = function (element, config) {
169+
config.callback('response from google');
170+
171+
return {
172+
then: function (cb) {
173+
cb();
174+
}
175+
};
176+
};
177+
178+
spyOn(vcRecaptchaService, 'create').and.callFake(_fakeCreate);
179+
180+
$compile(element)($scope);
181+
$scope.$digest();
182+
$timeout.flush();
183+
184+
expect($scope.onSuccess).toHaveBeenCalledWith({
185+
response: 'response from google',
186+
widgetId: undefined
187+
});
188+
});
189+
});
190+
});

0 commit comments

Comments
 (0)