Skip to content
This repository was archived by the owner on Feb 2, 2019. It is now read-only.

Commit 5d33442

Browse files
author
codekraft-studio
committed
added test environment + tests
1 parent 81f4302 commit 5d33442

File tree

4 files changed

+154
-1
lines changed

4 files changed

+154
-1
lines changed

bower.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@
2121
],
2222
"dependencies": {
2323
"angular": "^1.6.1"
24+
},
25+
"devDependencies": {
26+
"angular-mocks": "^1.6.4"
2427
}
2528
}

karma.conf.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Karma configuration
2+
// Generated on Thu May 04 2017 10:55:53 GMT+0200 (W. Europe Daylight Time)
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/angular/angular.js',
19+
'bower_components/angular-mocks/angular-mocks.js',
20+
'src/*.module.js',
21+
'src/*.js',
22+
'test/*.test.js'
23+
],
24+
25+
26+
// list of files to exclude
27+
exclude: [
28+
],
29+
30+
31+
// preprocess matching files before serving them to the browser
32+
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
33+
preprocessors: {
34+
},
35+
36+
37+
// test results reporter to use
38+
// possible values: 'dots', 'progress'
39+
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
40+
reporters: ['progress'],
41+
42+
43+
// web server port
44+
port: 9876,
45+
46+
47+
// enable / disable colors in the output (reporters and logs)
48+
colors: true,
49+
50+
51+
// level of logging
52+
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
53+
logLevel: config.LOG_INFO,
54+
55+
56+
// enable / disable watching file and executing tests whenever any file changes
57+
autoWatch: true,
58+
59+
60+
// start these browsers
61+
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
62+
browsers: ['PhantomJS'],
63+
64+
65+
// Continuous Integration mode
66+
// if true, Karma captures browsers, runs the tests and exits
67+
singleRun: false,
68+
69+
// Concurrency level
70+
// how many browser should be started simultaneous
71+
concurrency: Infinity
72+
})
73+
}

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
"grunt-contrib-jshint": "^1.1.0",
2929
"grunt-contrib-uglify": "^2.0.0",
3030
"grunt-contrib-watch": "^1.0.0",
31-
"grunt-ng-annotate": "^3.0.0"
31+
"grunt-ng-annotate": "^3.0.0",
32+
"jasmine-core": "^2.6.1",
33+
"karma": "^1.6.0",
34+
"karma-jasmine": "^1.1.0",
35+
"karma-phantomjs-launcher": "^1.0.4"
3236
}
3337
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
describe('Angular Async Validation: Directive', function() {
2+
3+
var $compile, $log, $rootScope;
4+
5+
beforeEach(module('angular-async-validation'));
6+
7+
beforeEach(inject(function(_$compile_, _$rootScope_, _$log_){
8+
$compile = _$compile_;
9+
$log = _$log_;
10+
$rootScope = _$rootScope_;
11+
}));
12+
13+
it('should throw an error if used without ngModel', function() {
14+
15+
expect( function() {
16+
$compile('<input async-validation />')($rootScope)
17+
} ).toThrow();
18+
19+
});
20+
21+
it('should log a warn message if nothing is passed', function() {
22+
23+
// Spy on $log service
24+
spyOn($log, 'warn');
25+
$rootScope.myModel = '';
26+
var element = $compile('<input ng-model="myModel" async-validation />')($rootScope);
27+
$rootScope.$digest();
28+
expect($log.warn).toHaveBeenCalled();
29+
30+
});
31+
32+
it('should work with string value as validation', function() {
33+
34+
// Spy on $log service
35+
spyOn($log, 'warn');
36+
$rootScope.myModel = '';
37+
$rootScope.validator = '/api/test/:value';
38+
var element = $compile('<input ng-model="myModel" async-validation="validator" />')($rootScope);
39+
$rootScope.$digest();
40+
expect($log.warn).not.toHaveBeenCalled();
41+
42+
});
43+
44+
it('should work with functions as validation if return promises', inject(function(_$q_) {
45+
46+
var $q = _$q_;
47+
// Spy on $log service
48+
spyOn($log, 'warn');
49+
$rootScope.myModel = '';
50+
$rootScope.validator = function() {
51+
return $q.defer().promise;
52+
};
53+
var element = $compile('<input ng-model="myModel" async-validation="validator" />')($rootScope);
54+
$rootScope.$digest();
55+
expect($log.warn).not.toHaveBeenCalled();
56+
57+
}));
58+
59+
it('should add validation function to model $asyncValidators', function() {
60+
61+
$rootScope.myModel = '';
62+
$rootScope.validator = 'myapi/:value';
63+
64+
var element = $compile('<input ng-model="myModel" async-validation="validator" />')($rootScope);
65+
var modelCtrl = element.controller('ngModel');
66+
67+
$rootScope.$digest();
68+
69+
expect( angular.isFunction(modelCtrl.$asyncValidators.asyncValidation) ).toBeTruthy();
70+
71+
});
72+
73+
});

0 commit comments

Comments
 (0)