-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #250 from sfount/testing-unit-tests
Testing: unit tests
- Loading branch information
Showing
6 changed files
with
199 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
describe('Integer validation directive', function () { | ||
var $scope; | ||
var form; | ||
|
||
beforeEach(module('bhima.directives')); | ||
|
||
// $complile and $rootScope are injected using angular name based dependency | ||
// injection | ||
beforeEach(inject(function ($compile, $rootScope) { | ||
$scope = $rootScope; | ||
|
||
var element = angular.element( | ||
'<form name="form">' + | ||
'<input ng-model="models.intValue" name="intValue" bh-integer />' + | ||
'</form>' | ||
); | ||
|
||
// initialise models that will be used | ||
$scope.models = { | ||
intValue : null | ||
}; | ||
|
||
// compile angular element in with the context of $rootScope | ||
$compile(element)($scope); | ||
form = $scope.form; | ||
})); | ||
|
||
|
||
it('validates an integer value', function () { | ||
var correctIntegerValue = 10; | ||
|
||
form.intValue.$setViewValue(correctIntegerValue); | ||
$scope.$digest(); | ||
|
||
expect($scope.models.intValue).to.equal(correctIntegerValue); | ||
expect(form.intValue.$valid).to.be.true; | ||
}); | ||
|
||
it('blocks non integer values (string/decimal)', function () { | ||
var incorrectDecimalValue = 10.23; | ||
var incorrectStringValue = 'value'; | ||
|
||
form.intValue.$setViewValue(incorrectDecimalValue); | ||
$scope.$digest(); | ||
|
||
expect($scope.models.intValue).to.be.undefined; | ||
expect(form.intValue.$valid).to.be.false; | ||
|
||
form.intValue.$setViewValue(incorrectStringValue); | ||
$scope.$digest(); | ||
|
||
expect($scope.models.intValue).to.be.undefined; | ||
expect(form.intValue.$valid).to.be.false; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
describe('Unique (async) validation directive', function () { | ||
var $scope, form; | ||
|
||
var MockUniqueValidatorService; | ||
|
||
// these represent values that the external $http request would return as | ||
// already registered in the database | ||
var existingValues = [100, 110, 120]; | ||
|
||
beforeEach(module('bhima.directives', 'bhima.services')); | ||
|
||
beforeEach(module(function ($provide) { | ||
|
||
// if this service is used by multiple directives/ controllers the mock | ||
// can be defined in an external /shared folder | ||
MockUniqueValidatorService = function ($q) { | ||
return { | ||
check : function (validationUrl, viewValue) { | ||
return existingValues.includes(viewValue) ? $q.reject() : $q.resolve(); | ||
} | ||
}; | ||
}; | ||
|
||
$provide.service('UniqueValidatorService', MockUniqueValidatorService); | ||
})); | ||
|
||
beforeEach(inject(function ($compile, $rootScope) { | ||
$scope = $rootScope; | ||
|
||
var element = angular.element( | ||
'<form name="form">' + | ||
'<input ng-model="models.uniqueValue" name="uniqueValue" bh-unique="/validation_path">' + | ||
'</form>' | ||
); | ||
|
||
$scope.models = { | ||
uniqueValue : null | ||
}; | ||
|
||
$compile(element)($scope); | ||
form = $scope.form; | ||
})); | ||
|
||
it('rejects a value that already exists', function () { | ||
|
||
// take the first exisitng value | ||
var existingValue = existingValues[0]; | ||
|
||
form.uniqueValue.$setViewValue(existingValue); | ||
$scope.$digest(); | ||
|
||
expect($scope.models.uniqueValue).to.be.undefined; | ||
expect(form.uniqueValue.$valid).to.be.false; | ||
}); | ||
|
||
it('accepts a value that is unique', function () { | ||
var uniqueValue = 200; | ||
|
||
form.uniqueValue.$setViewValue(uniqueValue); | ||
$scope.$digest(); | ||
|
||
expect($scope.models.uniqueValue).to.equal(uniqueValue); | ||
expect(form.uniqueValue.$valid).to.equal.true; | ||
|
||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Karma configuration | ||
// Generated on Wed Mar 30 2016 15:50:28 GMT+0100 (WAT) | ||
|
||
module.exports = function(config) { | ||
config.set({ | ||
|
||
// base path that will be used to resolve all patterns (eg. files, exclude) | ||
basePath: '', | ||
|
||
|
||
// frameworks to use | ||
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter | ||
frameworks: ['mocha', 'chai'], | ||
|
||
|
||
// list of files / patterns to load in the browser | ||
files: [ | ||
'client/vendor/angular/angular.js', | ||
'client/vendor/angular-mocks/angular-mocks.js', | ||
'bin/client/js/bhima.min.js', | ||
'client/test/unit/**/*.spec.js' | ||
], | ||
|
||
|
||
// list of files to exclude | ||
exclude: [ | ||
], | ||
|
||
|
||
// preprocess matching files before serving them to the browser | ||
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor | ||
preprocessors: { | ||
}, | ||
|
||
// test results reporter to use | ||
// possible values: 'dots', 'progress' | ||
// available reporters: https://npmjs.org/browse/keyword/karma-reporter | ||
reporters: ['dots', 'progress'], | ||
|
||
// web server port | ||
port: 9876, | ||
|
||
|
||
// enable / disable colors in the output (reporters and logs) | ||
colors: true, | ||
|
||
|
||
// level of logging | ||
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG | ||
logLevel: config.LOG_INFO, | ||
|
||
|
||
// enable / disable watching file and executing tests whenever any file changes | ||
autoWatch: true, | ||
|
||
|
||
// start these browsers | ||
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher | ||
browsers: ['Chrome'], | ||
|
||
|
||
// Continuous Integration mode | ||
// if true, Karma captures browsers, runs the tests and exits | ||
singleRun: false, | ||
|
||
// Concurrency level | ||
// how many browser should be started simultaneous | ||
concurrency: Infinity | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters