Skip to content

Commit

Permalink
Merge pull request #250 from sfount/testing-unit-tests
Browse files Browse the repository at this point in the history
Testing: unit tests
  • Loading branch information
sfount committed Apr 4, 2016
2 parents 0bb43bc + 6bd6aaf commit 0fed278
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 73 deletions.
5 changes: 4 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
"angular-chart.js": "^0.9.0"
},
"resolutions": {
"angular": "^1.5.0"
"angular": "^1.5.0",
},
"devDependencies": {
"angular-mocks": "^1.5.3"
}
}
55 changes: 55 additions & 0 deletions client/test/unit/directives/bhInteger.spec.js
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;
});
});
66 changes: 66 additions & 0 deletions client/test/unit/directives/bhUnique.spec.js
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;

});
});
72 changes: 0 additions & 72 deletions client/test/unit/login/sessionservice.spec.js

This file was deleted.

70 changes: 70 additions & 0 deletions karma.conf.js
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
})
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
"gulp-uglify": "^1.5.2",
"gulp-util": "^3.0.7",
"jshint": "^2.8.0",
"karma": "^0.13.22",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^0.2.3",
"karma-mocha": "^0.2.2",
"mocha": "^2.4.5",
"protractor": "^3.1.1",
"rimraf": "^2.4.3"
Expand Down

0 comments on commit 0fed278

Please sign in to comment.