From 3425fee3c639c401af2f0d19003d1cf3edc70bda Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sun, 8 Feb 2015 07:44:13 +0100 Subject: [PATCH] tests: added tests for netteForms.js --- .travis.yml | 15 +- src/assets/netteForms.js | 11 +- tests/.gitignore | 2 + tests/netteForms/.jshintrc | 44 +++ tests/netteForms/Gruntfile.js | 30 ++ tests/netteForms/SpecRunner.html | 21 ++ tests/netteForms/bower.json | 7 + tests/netteForms/package.json | 8 + .../netteForms/spec/Nette.validateRuleSpec.js | 299 ++++++++++++++++++ 9 files changed, 428 insertions(+), 9 deletions(-) create mode 100644 tests/netteForms/.jshintrc create mode 100644 tests/netteForms/Gruntfile.js create mode 100644 tests/netteForms/SpecRunner.html create mode 100644 tests/netteForms/bower.json create mode 100644 tests/netteForms/package.json create mode 100644 tests/netteForms/spec/Nette.validateRuleSpec.js diff --git a/.travis.yml b/.travis.yml index 2b165be32..f91f4f9e1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,9 +10,14 @@ matrix: allow_failures: - php: hhvm + include: + - php: 5.5 + env: netteForms=1 + script: - - vendor/bin/tester tests -s -p php -c tests/php-unix.ini - - php code-checker/src/code-checker.php + - if [ "$netteForms" != 1 ]; then vendor/bin/tester tests -s -p php -c tests/php-unix.ini; fi + - if [ "$netteForms" != 1 ]; then php code-checker/src/code-checker.php; fi + - if [ "$netteForms" == 1 ]; then grunt --gruntfile tests/netteForms/Gruntfile.js test; fi after_failure: # Print *.actual content @@ -20,5 +25,7 @@ after_failure: before_script: # Install Nette Tester & Code Checker - - composer install --no-interaction --dev --prefer-source - - composer create-project nette/code-checker code-checker ~2.3 --no-interaction --prefer-source + - if [ "$netteForms" != 1 ]; then composer install --no-interaction --dev --prefer-source; fi + - if [ "$netteForms" != 1 ]; then composer create-project nette/code-checker code-checker ~2.3 --no-interaction --prefer-source; fi + - if [ "$netteForms" == 1 ]; then npm install -g grunt-cli; cd tests/netteForms; npm install; bower install; cd ../..; fi + diff --git a/src/assets/netteForms.js b/src/assets/netteForms.js index fe45f9a7a..2cb055cf1 100644 --- a/src/assets/netteForms.js +++ b/src/assets/netteForms.js @@ -257,6 +257,7 @@ Nette.validators = { }, equal: function(elem, arg, val) { + /* jshint eqeqeq: false */ if (arg === undefined) { return null; } @@ -301,9 +302,9 @@ Nette.validators = { regexp: function(elem, arg, val) { var parts = typeof arg === 'string' ? arg.match(/^\/(.*)\/([imu]*)$/) : false; - if (parts) { try { - return (new RegExp(parts[1], parts[2].replace('u', ''))).test(val); - } catch (e) {} } + try { + return parts && (new RegExp(parts[1], parts[2].replace('u', ''))).test(val); + } catch (e) {} }, pattern: function(elem, arg, val) { @@ -385,7 +386,7 @@ Nette.toggleForm = function(form, elem) { */ Nette.toggleControl = function(elem, rules, topSuccess, firsttime) { rules = rules || Nette.parseJSON(elem.getAttribute('data-nette-rules')); - var has = false, __hasProp = Object.prototype.hasOwnProperty, handler = function() { + var has = false, hasProp = Object.prototype.hasOwnProperty, handler = function () { Nette.toggleForm(elem.form, elem); }, handled = []; @@ -424,7 +425,7 @@ Nette.toggleControl = function(elem, rules, topSuccess, firsttime) { } } for (var id2 in rule.toggle || []) { - if (__hasProp.call(rule.toggle, id2)) { + if (hasProp.call(rule.toggle, id2)) { Nette.toggles[id2] = Nette.toggles[id2] || (rule.toggle[id2] ? success : !success); } } diff --git a/tests/.gitignore b/tests/.gitignore index a7ffcfd8f..122fbcfca 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -1,3 +1,5 @@ /*/output /test.log /tmp +/netteForms/node_modules +/netteForms/bower_components diff --git a/tests/netteForms/.jshintrc b/tests/netteForms/.jshintrc new file mode 100644 index 000000000..1525e0919 --- /dev/null +++ b/tests/netteForms/.jshintrc @@ -0,0 +1,44 @@ +{ + // Predefined globals whom JSHint will ignore. + "browser" : true, // Standard browser globals e.g. `window`, `document`. + "predef" : [ // Custom globals. + "FileList" + ], + + "debug" : false, // Allow debugger statements e.g. browser breakpoints. + "devel" : true, // Allow developments statements e.g. `console.log();`. + + // ECMAScript 5. + "es5" : false, // Allow ECMAScript 5 syntax. + + // The Good Parts. + "asi" : true, // Tolerate Automatic Semicolon Insertion (no semicolons). + "laxbreak" : true, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons. + "bitwise" : true, // Prohibit bitwise operators (&, |, ^, etc.). + "boss" : false, // Tolerate assignments inside if, for & while. Usually conditions & loops are for comparison, not assignments. + "curly" : true, // Require {} for every new block or scope. + "eqeqeq" : true, // Require triple equals i.e. `===`. + "eqnull" : false, // Tolerate use of `== null`. + "evil" : true, // Tolerate use of `eval`. + "expr" : false, // Tolerate `ExpressionStatement` as Programs. + "forin" : false, // Tolerate `for in` loops without `hasOwnPrototype`. + "immed" : true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );` + "latedef" : true, // Prohipit variable use before definition. + "loopfunc" : true, // Allow functions to be defined within loops. + "noarg" : true, // Prohibit use of `arguments.caller` and `arguments.callee`. + "regexp" : true, // Prohibit `.` and `[^...]` in regular expressions. + "regexdash" : false, // Tolerate unescaped last dash i.e. `[-...]`. + "scripturl" : true, // Tolerate script-targeted URLs. + "shadow" : false, // Allows re-define variables later in code e.g. `var x=1; x=2;`. + "supernew" : false, // Tolerate `new function () { ... };` and `new Object;`. + "undef" : true, // Require all non-global variables be declared before they are used. + + // Personal styling preferences. + "newcap" : true, // Require capitalization of all constructor functions e.g. `new F()`. + "noempty" : true, // Prohibit use of empty blocks. + "nonew" : true, // Prohibit use of constructors for side-effects. + "nomen" : true, // Prohibit use of initial or trailing underbars in names. + "onevar" : false, // Allow only one `var` statement per function. + "plusplus" : false, // Prohibit use of `++` & `--`. + "sub" : false // Tolerate all forms of subscript notation besides dot notation e.g. `dict['key']` instead of `dict.key`. +} diff --git a/tests/netteForms/Gruntfile.js b/tests/netteForms/Gruntfile.js new file mode 100644 index 000000000..d733eaee6 --- /dev/null +++ b/tests/netteForms/Gruntfile.js @@ -0,0 +1,30 @@ +module.exports = function(grunt) { + grunt.initConfig({ + jshint: { + options: { + jshintrc: '.jshintrc' + }, + all: [ + '../../src/assets/*.js' + ] + }, + jasmine: { + test: { + src: '../../src/assets/*.js', + options: { + vendor: [ + 'bower_components/fixtures/fixtures.js' + ], + specs: 'spec/*Spec.js' + } + } + } + }); + + grunt.loadNpmTasks('grunt-contrib-jshint'); + grunt.loadNpmTasks('grunt-contrib-jasmine'); + + grunt.registerTask('test', [ + 'jshint','jasmine' + ]); +}; diff --git a/tests/netteForms/SpecRunner.html b/tests/netteForms/SpecRunner.html new file mode 100644 index 000000000..0b31ba550 --- /dev/null +++ b/tests/netteForms/SpecRunner.html @@ -0,0 +1,21 @@ + + + + + Jasmine Spec Runner v2.2.0 + + + + + + + + + + + + + + + + diff --git a/tests/netteForms/bower.json b/tests/netteForms/bower.json new file mode 100644 index 000000000..d1c677ed6 --- /dev/null +++ b/tests/netteForms/bower.json @@ -0,0 +1,7 @@ +{ + "name": "netteForms.js tests", + "dependencies": { + "jasmine": "~2.2.1", + "fixtures": "~1.5.3" + } +} diff --git a/tests/netteForms/package.json b/tests/netteForms/package.json new file mode 100644 index 000000000..2523f7afc --- /dev/null +++ b/tests/netteForms/package.json @@ -0,0 +1,8 @@ +{ + "devDependencies": { + "bower": "^1.3.12", + "grunt": "^0.4.5", + "grunt-contrib-jasmine": "^0.8.2", + "grunt-contrib-jshint": "^0.11.0" + } +} diff --git a/tests/netteForms/spec/Nette.validateRuleSpec.js b/tests/netteForms/spec/Nette.validateRuleSpec.js new file mode 100644 index 000000000..5b0973bf3 --- /dev/null +++ b/tests/netteForms/spec/Nette.validateRuleSpec.js @@ -0,0 +1,299 @@ +describe('Nette.getValue & validateRule', function() { + fixtures.path = 'spec/fixtures'; + + beforeEach(function() { + fixtures.cleanUp(); + }); + + + it('text input', function() { + fixtures.set('
'); + + var doc = fixtures.window().document, + form = doc.forms[0], + el = form.input; + + expect(Nette.getValue(el)).toBe(''); + expect(Nette.validateRule(el, 'filled')).toBe(false); + expect(Nette.validateRule(el, 'blank')).toBe(true); + expect(Nette.validateRule(el, 'equal', '')).toBe(true); + + el.value = ' hello '; + expect(Nette.getValue(el)).toBe('hello'); + expect(Nette.validateRule(el, 'filled')).toBe(true); + expect(Nette.validateRule(el, 'blank')).toBe(false); + expect(Nette.validateRule(el, 'equal', '')).toBe(false); + expect(Nette.validateRule(el, 'equal', 'hello')).toBe(true); + expect(Nette.validateRule(el, 'equal', ['a', 'b'])).toBe(false); + expect(Nette.validateRule(el, 'equal', ['a', 'hello', 'b'])).toBe(true); + expect(Nette.validateRule(el, 'notEqual', 'hello')).toBe(false); + expect(Nette.validateRule(el, 'minLength', 1)).toBe(true); + expect(Nette.validateRule(el, 'minLength', 6)).toBe(false); + expect(Nette.validateRule(el, 'maxLength', 1)).toBe(false); + expect(Nette.validateRule(el, 'maxLength', 6)).toBe(true); + expect(Nette.validateRule(el, 'length', 1)).toBe(false); + expect(Nette.validateRule(el, 'length', 5)).toBe(true); + expect(Nette.validateRule(el, 'length', 6)).toBe(false); + expect(Nette.validateRule(el, 'length', [1, 6])).toBe(true); + expect(Nette.validateRule(el, 'length', [3, 4])).toBe(false); + expect(Nette.validateRule(el, 'email')).toBe(false); + expect(Nette.validateRule(el, 'url')).toBe(false); + expect(Nette.validateRule(el, 'regexp', '/\\d+/')).toBe(false); + expect(Nette.validateRule(el, 'regexp', '/\\w+/')).toBe(true); + expect(Nette.validateRule(el, 'pattern', '\\d+')).toBe(false); + expect(Nette.validateRule(el, 'pattern', '\\d')).toBe(false); + expect(Nette.validateRule(el, 'pattern', '\\w')).toBe(false); + expect(Nette.validateRule(el, 'pattern', '\\w+')).toBe(true); + expect(Nette.validateRule(el, 'integer')).toBe(false); + expect(Nette.validateRule(el, 'float')).toBe(false); + + el.value = 'john@doe.com'; + expect(Nette.validateRule(el, 'email')).toBe(true); + + el.value = 'nette.org'; + expect(Nette.validateRule(el, 'url')).toBe(true); + + el.value = '-1234'; + expect(Nette.validateRule(el, 'integer')).toBe(true); + expect(Nette.validateRule(el, 'float')).toBe(true); + expect(Nette.validateRule(el, 'min', -2000)).toBe(true); + expect(Nette.validateRule(el, 'min', -1000)).toBe(false); + expect(Nette.validateRule(el, 'max', -2000)).toBe(false); + expect(Nette.validateRule(el, 'max', -1000)).toBe(true); + expect(Nette.validateRule(el, 'range', ['-2000', '-1000'])).toBe(true); + expect(Nette.validateRule(el, 'range', [10, null])).toBe(false); + + el.value = '-12.5'; + expect(Nette.validateRule(el, 'integer')).toBe(false); + expect(Nette.validateRule(el, 'float')).toBe(true); + expect(Nette.validateRule(el, 'min', -2000)).toBe(true); + expect(Nette.validateRule(el, 'min', -10)).toBe(false); + expect(Nette.validateRule(el, 'max', -2000)).toBe(false); + expect(Nette.validateRule(el, 'max', -10)).toBe(true); + expect(Nette.validateRule(el, 'range', ['-12.6', '-12.4'])).toBe(true); + expect(Nette.validateRule(el, 'range', [-5, 10])).toBe(false); + }); + + + it('text area', function() { + fixtures.set('
'); + + var doc = fixtures.window().document, + form = doc.forms[0], + el = form.input; + + expect(Nette.getValue(el)).toBe(''); + expect(Nette.validateRule(el, 'filled')).toBe(false); + expect(Nette.validateRule(el, 'blank')).toBe(true); + expect(Nette.validateRule(el, 'equal', '')).toBe(true); + + el.value = ' hello '; + expect(Nette.getValue(el)).toBe(' hello '); + expect(Nette.validateRule(el, 'filled')).toBe(true); + expect(Nette.validateRule(el, 'blank')).toBe(false); + expect(Nette.validateRule(el, 'equal', 'hello')).toBe(false); + expect(Nette.validateRule(el, 'equal', ' hello ')).toBe(true); + }); + + + it('upload', function() { + fixtures.set('
'); + + var doc = fixtures.window().document, + form = doc.forms[0], + el = form.input; + + expect(Nette.getValue(el) instanceof fixtures.window().FileList).toBe(true); + expect(Nette.getValue(el).length).toBe(0); + }); + + + it('multi upload', function() { + fixtures.set('
'); + + var doc = fixtures.window().document, + form = doc.forms[0], + el = form['input[]']; + + expect(Nette.getValue(el) instanceof fixtures.window().FileList).toBe(true); + expect(Nette.getValue(el).length).toBe(0); + }); + + + it('checkbox', function() { + fixtures.set('
'); + + var doc = fixtures.window().document, + form = doc.forms[0], + el = form.input; + + expect(Nette.getValue(el)).toBe(false); + expect(Nette.validateRule(el, 'filled')).toBe(false); + expect(Nette.validateRule(el, 'blank')).toBe(true); + expect(Nette.validateRule(el, 'equal', false)).toBe(true); + + el.checked = true; + expect(Nette.getValue(el)).toBe(true); + expect(Nette.validateRule(el, 'filled')).toBe(true); + expect(Nette.validateRule(el, 'blank')).toBe(false); + expect(Nette.validateRule(el, 'equal', true)).toBe(true); + }); + + + it('checkbox list', function() { + fixtures.set('
\ + \ + \ + \ +
'); + + var doc = fixtures.window().document, + form = doc.forms[0], + el = form['input[]']; + + expect(Nette.getValue(el)).toEqual([]); + expect(Nette.validateRule(el, 'filled')).toBe(false); + expect(Nette.validateRule(el, 'blank')).toBe(true); + expect(Nette.validateRule(el, 'equal', ['r', 'g', 'b'])).toBe(true); + + doc.getElementById('input-r').checked = true; + expect(Nette.getValue(el)).toEqual(['r']); + expect(Nette.validateRule(el, 'filled')).toBe(true); + expect(Nette.validateRule(el, 'blank')).toBe(false); + expect(Nette.validateRule(el, 'equal', 'r')).toBe(true); + expect(Nette.validateRule(el, 'equal', 'g')).toBe(false); + expect(Nette.validateRule(el, 'equal', ['r', 'g'])).toBe(true); + expect(Nette.validateRule(el, 'minLength', 1)).toBe(true); + expect(Nette.validateRule(el, 'minLength', 2)).toBe(false); + + doc.getElementById('input-g').checked = true; + expect(Nette.getValue(el)).toEqual(['r', 'g']); + expect(Nette.validateRule(el, 'filled')).toBe(true); + expect(Nette.validateRule(el, 'blank')).toBe(false); + expect(Nette.validateRule(el, 'equal', 'r')).toBe(false); + expect(Nette.validateRule(el, 'equal', ['r', 'x'])).toBe(false); + expect(Nette.validateRule(el, 'equal', ['r', 'g'])).toBe(true); + expect(Nette.validateRule(el, 'equal', ['r', 'g', 'b'])).toBe(true); + expect(Nette.validateRule(el, 'minLength', 2)).toBe(true); + expect(Nette.validateRule(el, 'minLength', 3)).toBe(false); + }); + + + it('radio', function() { + fixtures.set('
'); + + var doc = fixtures.window().document, + form = doc.forms[0], + el = form.input; + + expect(Nette.getValue(el)).toBe(false); + expect(Nette.validateRule(el, 'filled')).toBe(false); + expect(Nette.validateRule(el, 'blank')).toBe(true); + expect(Nette.validateRule(el, 'equal', ['f', 'm'])).toBe(false); + + el.checked = true; + expect(Nette.getValue(el)).toBe('f'); + expect(Nette.validateRule(el, 'filled')).toBe(true); + expect(Nette.validateRule(el, 'blank')).toBe(false); + expect(Nette.validateRule(el, 'equal', 'f')).toBe(true); + expect(Nette.validateRule(el, 'equal', 'm')).toBe(false); + expect(Nette.validateRule(el, 'equal', ['f', 'm'])).toBe(true); + }); + + + it('radio list', function() { + fixtures.set(' \ + \ + \ +
'); + + var doc = fixtures.window().document, + form = doc.forms[0], + el = form.input; + + expect(Nette.getValue(el)).toBe(null); + expect(Nette.validateRule(el, 'filled')).toBe(false); + expect(Nette.validateRule(el, 'blank')).toBe(true); + expect(Nette.validateRule(el, 'equal', ['f', 'm'])).toBe(false); + + doc.getElementById('input-m').checked = true; + expect(Nette.getValue(el)).toBe('m'); + + doc.getElementById('input-f').checked = true; + expect(Nette.getValue(el)).toBe('f'); + expect(Nette.validateRule(el, 'filled')).toBe(true); + expect(Nette.validateRule(el, 'blank')).toBe(false); + expect(Nette.validateRule(el, 'equal', 'f')).toBe(true); + expect(Nette.validateRule(el, 'equal', 'm')).toBe(false); + expect(Nette.validateRule(el, 'equal', ['f', 'm'])).toBe(true); + }); + + + it('selectbox', function() { + fixtures.set('
\ + \ +
'); + + var doc = fixtures.window().document, + form = doc.forms[0], + el = form.input; + + expect(Nette.getValue(el)).toBe(''); + expect(Nette.validateRule(el, 'filled')).toBe(false); + expect(Nette.validateRule(el, 'blank')).toBe(true); + + doc.getElementById('option-2').selected = true; + expect(Nette.getValue(el)).toBe('bu'); + expect(Nette.validateRule(el, 'filled')).toBe(true); + expect(Nette.validateRule(el, 'blank')).toBe(false); + expect(Nette.validateRule(el, 'equal', 'bu')).toBe(true); + expect(Nette.validateRule(el, 'equal', 'x')).toBe(false); + expect(Nette.validateRule(el, 'equal', ['bu', 'x'])).toBe(true); + + doc.getElementById('option-3').selected = true; + expect(Nette.getValue(el)).toBe('?'); + }); + + + it('multi selectbox', function() { + fixtures.set('
\ + \ +
'); + + var doc = fixtures.window().document, + form = doc.forms[0], + el = form.input; + + expect(Nette.getValue(el)).toEqual([]); + expect(Nette.validateRule(el, 'filled')).toBe(false); + expect(Nette.validateRule(el, 'blank')).toBe(true); + + doc.getElementById('option-2').selected = true; + expect(Nette.getValue(el)).toEqual(['bu']); + expect(Nette.validateRule(el, 'filled')).toBe(true); + expect(Nette.validateRule(el, 'blank')).toBe(false); + expect(Nette.validateRule(el, 'equal', 'bu')).toBe(true); + expect(Nette.validateRule(el, 'equal', 'x')).toBe(false); + expect(Nette.validateRule(el, 'equal', ['bu', 'x'])).toBe(true); + expect(Nette.validateRule(el, 'minLength', 1)).toBe(true); + expect(Nette.validateRule(el, 'minLength', 2)).toBe(false); + + doc.getElementById('option-3').selected = true; + expect(Nette.getValue(el)).toEqual(['bu', '?']); + expect(Nette.validateRule(el, 'filled')).toBe(true); + expect(Nette.validateRule(el, 'blank')).toBe(false); + expect(Nette.validateRule(el, 'equal', 'bu')).toBe(false); + expect(Nette.validateRule(el, 'equal', ['bu', 'x'])).toBe(false); + expect(Nette.validateRule(el, 'equal', ['bu', '?'])).toBe(true); + expect(Nette.validateRule(el, 'equal', ['bu', '?', 'x'])).toBe(true); + expect(Nette.validateRule(el, 'minLength', 2)).toBe(true); + expect(Nette.validateRule(el, 'minLength', 3)).toBe(false); + }); +});