Commit 4d1a3a04 4d1a3a042f9d98baa7ba38bb5e417e6c03e76a49 by Vincent Peybernes

#1 - Implement tests

1 parent 001f446c
...@@ -3,7 +3,10 @@ ...@@ -3,7 +3,10 @@
3 */ 3 */
4 4
5 const gulp = require('gulp'), 5 const gulp = require('gulp'),
6 del = require('del'); 6 del = require('del'),
7 sourcemaps = require('gulp-sourcemaps'),
8 babel = require('gulp-babel'),
9 rename = require('gulp-rename');
7 10
8 const distPath = './dist/'; 11 const distPath = './dist/';
9 const srcPath = './src/matrix-validation.js'; 12 const srcPath = './src/matrix-validation.js';
...@@ -14,5 +17,12 @@ gulp.task('clean', () => { ...@@ -14,5 +17,12 @@ gulp.task('clean', () => {
14 17
15 gulp.task('build', () => { 18 gulp.task('build', () => {
16 gulp.src(srcPath) 19 gulp.src(srcPath)
20 .pipe(gulp.dest(distPath))
21 .pipe(sourcemaps.init())
22 .pipe(babel({presets:['es2015']}))
23 .pipe(rename(path => {
24 path.basename += '-es2015'
25 }))
26 .pipe(sourcemaps.write())
17 .pipe(gulp.dest(distPath)); 27 .pipe(gulp.dest(distPath));
18 }); 28 });
...\ No newline at end of file ...\ No newline at end of file
......
1 // Karma configuration
2 // Generated on Thu Jan 12 2017 19:57:06 GMT+0100 (Paris, Madrid)
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 'node_modules/angular/angular.js',
19 'node_modules/angular-mocks/angular-mocks.js',
20 'dist/*es2015.js',
21 'tests/*Dep.js',
22 'tests/*Spec.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 }
...@@ -13,10 +13,19 @@ ...@@ -13,10 +13,19 @@
13 "email": "contact@techniv.fr" 13 "email": "contact@techniv.fr"
14 }, 14 },
15 "license": "MIT", 15 "license": "MIT",
16
17 "devDependencies": { 16 "devDependencies": {
18 "gulp": "*", 17 "angular": "*",
18 "angular-mocks": "*",
19 "del": "*", 19 "del": "*",
20 "angular": "*" 20 "gulp": "*",
21 "gulp-sourcemaps": "*",
22 "gulp-babel": "*",
23 "babel-preset-es2015": "*",
24 "gulp-rename": "*",
25 "karma": "*",
26 "karma-chrome-launcher": "^2.0.0",
27 "karma-jasmine": "^1.1.0",
28 "karma-phantomjs-launcher": "*",
29 "jasmine-core": "*"
21 } 30 }
22 } 31 }
......
...@@ -33,16 +33,22 @@ angular.module('matrixValidation', []).directive('matrixValidator', [ ...@@ -33,16 +33,22 @@ angular.module('matrixValidation', []).directive('matrixValidator', [
33 33
34 inputCtrl.$validators.matrixValidator = validationHandler.bind(inputCtrl); 34 inputCtrl.$validators.matrixValidator = validationHandler.bind(inputCtrl);
35 } 35 }
36
37 validationHandler.call(false);
36 }); 38 });
37 39
38 40
39 function validationHandler(){ 41 function validationHandler(){
40 let returnValidation = false; 42 let returnValidation, input, scope, xInput, yInput;
41 var input = this.$$element[0]; 43
42 var scope = this.$$scope; 44 if(this && this.valueOf()){
45 returnValidation = false;
46 input = this.$$element[0];
47 scope = this.$$scope;
43 48
44 let xInput = parseInt(scope.$eval(input.dataset.row)); 49 xInput = parseInt(scope.$eval(input.dataset.row));
45 let yInput = parseInt(scope.$eval(input.dataset.col)); 50 yInput = parseInt(scope.$eval(input.dataset.col));
51 }
46 52
47 let matrix = inputMatrix.map((row) => { 53 let matrix = inputMatrix.map((row) => {
48 return row.map((value) => value.$$rawModelValue); 54 return row.map((value) => value.$$rawModelValue);
...@@ -66,8 +72,6 @@ angular.module('matrixValidation', []).directive('matrixValidator', [ ...@@ -66,8 +72,6 @@ angular.module('matrixValidation', []).directive('matrixValidator', [
66 } 72 }
67 } 73 }
68 74
69 debugger;
70
71 return returnValidation; 75 return returnValidation;
72 } 76 }
73 } 77 }
......
1 /**
2 * Created by Techniv on 12/01/2017.
3 */
4
5 describe('matrix-validation Spec', function(){
6
7 var $compile, $rootScope, $timeout, template;
8
9 beforeEach(module('matrixValidation'));
10 beforeEach(inject(function (_$compile_, _$rootScope_, _$timeout_) {
11 $compile = _$compile_;
12 $rootScope = _$rootScope_;
13 $timeout = _$timeout_;
14 }));
15 beforeEach(function () {
16 template = $compile(
17 '<form name="mForm" matrix-validator data-all="validator">\
18 <span ng-repeat="row in matrix" ng-init="rowId = $index">\
19 <input name="i{{rowId}}{{$index}}" ng-repeat="item in row track by $index" data-row="rowId" data-col="$index" ng-model="row[$index]">\
20 </span>\
21 </form>');
22 });
23
24 beforeEach(function () {
25 jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000;
26 });
27
28 it('test', function(done){
29
30 var $scope = $rootScope.$new();
31 $scope.matrix = [
32 [1,2,3],
33 [2,3,1],
34 [3,1,2]
35 ];
36 $scope.validator = validator;
37
38 var element = template($scope);
39 $scope.$digest();
40
41 $timeout.flush();
42
43 var inputs = element.find('input');
44
45 for(var i = 0; i < inputs.length; i++){
46 var input = angular.element(inputs[i]);
47 var scope = input.scope();
48 var name = inputs[i].name;
49 var ngInput = scope.mForm[name];
50 expect(ngInput.$valid).toEqual(validator.last[scope.rowId][scope.$index]);
51 }
52
53 var input = angular.element(inputs[6]);
54 input.val(1);
55 input.triggerHandler('change');
56
57 setTimeout(function () {
58 expect(scope.mForm.i20.$valid).toBe(false);
59 expect(scope.mForm.i00.$valid).toBe(false);
60 done();
61 },500);
62
63 });
64 });
...\ No newline at end of file ...\ No newline at end of file
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
26 </ng-form> 26 </ng-form>
27 27
28 <script src="../node_modules/angular/angular.js"></script> 28 <script src="../node_modules/angular/angular.js"></script>
29 <script src="validatorDep.js"></script>
29 <script src="../src/matrix-validation.js"></script> 30 <script src="../src/matrix-validation.js"></script>
30 <script> 31 <script>
31 angular.module('test', ['matrixValidation']).controller('ctrlTest', [ 32 angular.module('test', ['matrixValidation']).controller('ctrlTest', [
...@@ -39,31 +40,7 @@ ...@@ -39,31 +40,7 @@
39 40
40 $scope.test=2; 41 $scope.test=2;
41 42
42 $scope.validator = (matrix, validationMatrix) => { 43 $scope.validator = validator;
43 for(let x = 0; x < matrix.length; x++){
44 let row = matrix[x];
45 for(let y = 0; y < row.length; y++){
46 let value = row[y];
47
48 if(validationMatrix[x][y] != null) continue;
49
50 for(
51 let z = (x < matrix.length - 1 ) ? x+1 : 0;
52 z < ((x < matrix.length -1 ) ? matrix.length : matrix.length - 1);
53 z++
54 ){
55 if(value == matrix[z][y]){
56 validationMatrix[x][y] = false;
57 validationMatrix[z][y] = false;
58 break;
59 }
60 }
61
62 if(validationMatrix[x][y] === null) validationMatrix[x][y] = true;
63 }
64 }
65 return validationMatrix;
66 };
67 } 44 }
68 ]); 45 ]);
69 </script> 46 </script>
......
1 /**
2 * Created by Techniv on 12/01/2017.
3 */
4
5 function validator(matrix, validationMatrix){
6 for(var x = 0; x < matrix.length; x++){
7 var row = matrix[x];
8 for(var y = 0; y < row.length; y++){
9 var value = row[y];
10
11 if(validationMatrix[x][y] != null) continue;
12
13 for(
14 var z = (x < matrix.length - 1 ) ? x+1 : 0;
15 z < ((x < matrix.length -1 ) ? matrix.length : matrix.length - 1);
16 z++
17 ){
18 if(value == matrix[z][y]){
19 validationMatrix[x][y] = false;
20 validationMatrix[z][y] = false;
21 break;
22 }
23 }
24
25 if(validationMatrix[x][y] === null) validationMatrix[x][y] = true;
26 }
27 }
28
29 validator.last = validationMatrix;
30
31 return validationMatrix;
32 }
...\ No newline at end of file ...\ No newline at end of file