Commit 4d1a3a04 4d1a3a042f9d98baa7ba38bb5e417e6c03e76a49 by Vincent Peybernes

#1 - Implement tests

1 parent 001f446c
......@@ -3,7 +3,10 @@
*/
const gulp = require('gulp'),
del = require('del');
del = require('del'),
sourcemaps = require('gulp-sourcemaps'),
babel = require('gulp-babel'),
rename = require('gulp-rename');
const distPath = './dist/';
const srcPath = './src/matrix-validation.js';
......@@ -14,5 +17,12 @@ gulp.task('clean', () => {
gulp.task('build', () => {
gulp.src(srcPath)
.pipe(gulp.dest(distPath))
.pipe(sourcemaps.init())
.pipe(babel({presets:['es2015']}))
.pipe(rename(path => {
path.basename += '-es2015'
}))
.pipe(sourcemaps.write())
.pipe(gulp.dest(distPath));
});
\ No newline at end of file
......
// Karma configuration
// Generated on Thu Jan 12 2017 19:57:06 GMT+0100 (Paris, Madrid)
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: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'node_modules/angular/angular.js',
'node_modules/angular-mocks/angular-mocks.js',
'dist/*es2015.js',
'tests/*Dep.js',
'tests/*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: ['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: ['PhantomJS'],
// 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
})
}
......@@ -13,10 +13,19 @@
"email": "contact@techniv.fr"
},
"license": "MIT",
"devDependencies": {
"gulp": "*",
"angular": "*",
"angular-mocks": "*",
"del": "*",
"angular": "*"
"gulp": "*",
"gulp-sourcemaps": "*",
"gulp-babel": "*",
"babel-preset-es2015": "*",
"gulp-rename": "*",
"karma": "*",
"karma-chrome-launcher": "^2.0.0",
"karma-jasmine": "^1.1.0",
"karma-phantomjs-launcher": "*",
"jasmine-core": "*"
}
}
......
......@@ -33,16 +33,22 @@ angular.module('matrixValidation', []).directive('matrixValidator', [
inputCtrl.$validators.matrixValidator = validationHandler.bind(inputCtrl);
}
validationHandler.call(false);
});
function validationHandler(){
let returnValidation = false;
var input = this.$$element[0];
var scope = this.$$scope;
let returnValidation, input, scope, xInput, yInput;
if(this && this.valueOf()){
returnValidation = false;
input = this.$$element[0];
scope = this.$$scope;
let xInput = parseInt(scope.$eval(input.dataset.row));
let yInput = parseInt(scope.$eval(input.dataset.col));
xInput = parseInt(scope.$eval(input.dataset.row));
yInput = parseInt(scope.$eval(input.dataset.col));
}
let matrix = inputMatrix.map((row) => {
return row.map((value) => value.$$rawModelValue);
......@@ -66,8 +72,6 @@ angular.module('matrixValidation', []).directive('matrixValidator', [
}
}
debugger;
return returnValidation;
}
}
......
/**
* Created by Techniv on 12/01/2017.
*/
describe('matrix-validation Spec', function(){
var $compile, $rootScope, $timeout, template;
beforeEach(module('matrixValidation'));
beforeEach(inject(function (_$compile_, _$rootScope_, _$timeout_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
$timeout = _$timeout_;
}));
beforeEach(function () {
template = $compile(
'<form name="mForm" matrix-validator data-all="validator">\
<span ng-repeat="row in matrix" ng-init="rowId = $index">\
<input name="i{{rowId}}{{$index}}" ng-repeat="item in row track by $index" data-row="rowId" data-col="$index" ng-model="row[$index]">\
</span>\
</form>');
});
beforeEach(function () {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000;
});
it('test', function(done){
var $scope = $rootScope.$new();
$scope.matrix = [
[1,2,3],
[2,3,1],
[3,1,2]
];
$scope.validator = validator;
var element = template($scope);
$scope.$digest();
$timeout.flush();
var inputs = element.find('input');
for(var i = 0; i < inputs.length; i++){
var input = angular.element(inputs[i]);
var scope = input.scope();
var name = inputs[i].name;
var ngInput = scope.mForm[name];
expect(ngInput.$valid).toEqual(validator.last[scope.rowId][scope.$index]);
}
var input = angular.element(inputs[6]);
input.val(1);
input.triggerHandler('change');
setTimeout(function () {
expect(scope.mForm.i20.$valid).toBe(false);
expect(scope.mForm.i00.$valid).toBe(false);
done();
},500);
});
});
\ No newline at end of file
......@@ -26,6 +26,7 @@
</ng-form>
<script src="../node_modules/angular/angular.js"></script>
<script src="validatorDep.js"></script>
<script src="../src/matrix-validation.js"></script>
<script>
angular.module('test', ['matrixValidation']).controller('ctrlTest', [
......@@ -39,31 +40,7 @@
$scope.test=2;
$scope.validator = (matrix, validationMatrix) => {
for(let x = 0; x < matrix.length; x++){
let row = matrix[x];
for(let y = 0; y < row.length; y++){
let value = row[y];
if(validationMatrix[x][y] != null) continue;
for(
let z = (x < matrix.length - 1 ) ? x+1 : 0;
z < ((x < matrix.length -1 ) ? matrix.length : matrix.length - 1);
z++
){
if(value == matrix[z][y]){
validationMatrix[x][y] = false;
validationMatrix[z][y] = false;
break;
}
}
if(validationMatrix[x][y] === null) validationMatrix[x][y] = true;
}
}
return validationMatrix;
};
$scope.validator = validator;
}
]);
</script>
......
/**
* Created by Techniv on 12/01/2017.
*/
function validator(matrix, validationMatrix){
for(var x = 0; x < matrix.length; x++){
var row = matrix[x];
for(var y = 0; y < row.length; y++){
var value = row[y];
if(validationMatrix[x][y] != null) continue;
for(
var z = (x < matrix.length - 1 ) ? x+1 : 0;
z < ((x < matrix.length -1 ) ? matrix.length : matrix.length - 1);
z++
){
if(value == matrix[z][y]){
validationMatrix[x][y] = false;
validationMatrix[z][y] = false;
break;
}
}
if(validationMatrix[x][y] === null) validationMatrix[x][y] = true;
}
}
validator.last = validationMatrix;
return validationMatrix;
}
\ No newline at end of file