Browse Source

#874: Add test suite for stringLength validator, thanks to @thx2001r

phuoc 11 years ago
parent
commit
f62003e156

+ 1 - 1
CHANGELOG.md

@@ -5,7 +5,7 @@
 __New Features__
 
 * [#807](https://github.com/nghuuphuoc/bootstrapvalidator/issues/807), [#821](https://github.com/nghuuphuoc/bootstrapvalidator/pull/821): Add ```min```, ```max``` options for the [date](http://bootstrapvalidator.com/validators/date/) validator, thanks to [@Arkni](https://github.com/Arkni)
-* [#844](https://github.com/nghuuphuoc/bootstrapvalidator/pull/844): The [stringLength](http://bootstrapvalidator.com/validators/stringLength/) validator adds option to evaluate length in UTF-8 bytes, thanks to [@thx2001r](https://github.com/thx2001r)
+* [#844](https://github.com/nghuuphuoc/bootstrapvalidator/pull/844), [#874](https://github.com/nghuuphuoc/bootstrapvalidator/pull/874): The [stringLength](http://bootstrapvalidator.com/validators/stringLength/) validator adds option to evaluate length in UTF-8 bytes, thanks to [@thx2001r](https://github.com/thx2001r)
 
 __Language Packages__
 

+ 1 - 1
dist/css/bootstrapValidator.min.css

@@ -2,7 +2,7 @@
  * BootstrapValidator (http://bootstrapvalidator.com)
  * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  *
- * @version     v0.5.3-dev, built on 2014-09-27 8:17:53 PM
+ * @version     v0.5.3-dev, built on 2014-09-27 8:24:59 PM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT

+ 2 - 3
dist/js/bootstrapValidator.js

@@ -2,7 +2,7 @@
  * BootstrapValidator (http://bootstrapvalidator.com)
  * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  *
- * @version     v0.5.3-dev, built on 2014-09-27 8:17:54 PM
+ * @version     v0.5.3-dev, built on 2014-09-27 8:24:59 PM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT
@@ -6082,7 +6082,6 @@ if (typeof jQuery === 'undefined') {
          * @param {Object} options Consists of following keys:
          * - min
          * - max
-         * - utf8Bytes
          * At least one of two keys is required
          * The min, max keys define the number which the field value compares to. min, max can be
          *      - A number
@@ -6091,7 +6090,7 @@ if (typeof jQuery === 'undefined') {
          *      - A callback function that returns the number
          *
          * - message: The invalid message
-         * - utf8bytes: Evaluate string length in UTF-8 bytes, default to false (no changes to existing scripts)
+         * - utf8bytes: Evaluate string length in UTF-8 bytes, default to false
          * @returns {Object}
          */
         validate: function(validator, $field, options) {

+ 1 - 1
dist/js/bootstrapValidator.min.js

@@ -2,7 +2,7 @@
  * BootstrapValidator (http://bootstrapvalidator.com)
  * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  *
- * @version     v0.5.3-dev, built on 2014-09-27 8:17:54 PM
+ * @version     v0.5.3-dev, built on 2014-09-27 8:24:59 PM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT

+ 1 - 1
src/js/validator/stringLength.js

@@ -44,7 +44,7 @@
          *      - A callback function that returns the number
          *
          * - message: The invalid message
-         * - utf8bytes: Evaluate string length in UTF-8 bytes, default to false (no changes to existing scripts)
+         * - utf8bytes: Evaluate string length in UTF-8 bytes, default to false
          * @returns {Object}
          */
         validate: function(validator, $field, options) {

+ 183 - 0
test/spec.js

@@ -5659,6 +5659,189 @@ describe('phone', function() {
     });
 });
 
+describe('stringLength', function() {
+    beforeEach(function() {
+        $([
+            '<form class="form-horizontal" id="stringLengthForm">',
+                '<div class="form-group">',
+                    '<input type="text" name="textCharMaxLength" data-bv-stringlength data-bv-stringlength-max="10" />',
+                '</div>',
+                '<div class="form-group">',
+                    '<textarea name="textareaCharMaxLength" data-bv-stringlength data-bv-stringlength-max="10"></textarea>',
+                '</div>',
+                '<div class="form-group">',
+                    '<input type="text" name="textUTF8BytesMaxLength" data-bv-stringlength data-bv-stringlength-utf8bytes="true" data-bv-stringlength-max="10" />',
+                '</div>',
+                '<div class="form-group">',
+                    '<textarea name="textareaUTF8BytesMaxLength" data-bv-stringlength data-bv-stringlength-utf8bytes="true" data-bv-stringlength-max="10"></textarea>',
+                '</div>',
+                '<div class="form-group">',
+                    '<input type="text" name="textCharMinLength" data-bv-stringlength data-bv-stringlength-min="5" />',
+                '</div>',
+                '<div class="form-group">',
+                    '<textarea name="textareaCharMinLength" data-bv-stringlength data-bv-stringlength-min="5"></textarea>',
+                '</div>',
+                '<div class="form-group">',
+                    '<input type="text" name="textUTF8BytesMinLength" data-bv-stringlength data-bv-stringlength-utf8bytes="true" data-bv-stringlength-min="5" />',
+                '</div>',
+                '<div class="form-group">',
+                    '<textarea name="textareaUTF8BytesMinLength" data-bv-stringlength data-bv-stringlength-utf8bytes="true" data-bv-stringlength-min="5"></textarea>',
+                '</div>',
+                '<div class="form-group">',
+                    '<input type="text" name="textCharMinMaxLength" data-bv-stringlength data-bv-stringlength-min="5" data-bv-stringlength-max="10" />',
+                '</div>',
+                '<div class="form-group">',
+                    '<textarea name="textareaCharMinMaxLength" data-bv-stringlength data-bv-stringlength-min="5" data-bv-stringlength-max="10"></textarea>',
+                '</div>',
+                '<div class="form-group">',
+                    '<input type="text" name="textUTF8BytesMinMaxLength" data-bv-stringlength data-bv-stringlength-utf8bytes="true" data-bv-stringlength-min="5" data-bv-stringlength-max="10" />',
+                '</div>',
+                '<div class="form-group">',
+                    '<textarea name="textareaUTF8BytesMinMaxLength" data-bv-stringlength data-bv-stringlength-utf8bytes="true" data-bv-stringlength-min="5" data-bv-stringlength-max="10"></textarea>',
+                '</div>',
+            '</form>'
+        ].join('\n')).appendTo('body');
+
+        $('#stringLengthForm').bootstrapValidator();
+
+        this.bv                             = $('#stringLengthForm').data('bootstrapValidator');
+        this.$textCharMaxLength             = this.bv.getFieldElements('textCharMaxLength');
+        this.$textareaCharMaxLength         = this.bv.getFieldElements('textareaCharMaxLength');
+        this.$textUTF8BytesMaxLength        = this.bv.getFieldElements('textUTF8BytesMaxLength');
+        this.$textareaUTF8BytesMaxLength    = this.bv.getFieldElements('textareaUTF8BytesMaxLength');
+        this.$textCharMinLength             = this.bv.getFieldElements('textCharMinLength');
+        this.$textareaCharMinLength         = this.bv.getFieldElements('textareaCharMinLength');
+        this.$textUTF8BytesMinLength        = this.bv.getFieldElements('textUTF8BytesMinLength');
+        this.$textareaUTF8BytesMinLength    = this.bv.getFieldElements('textareaUTF8BytesMinLength');
+        this.$textCharMinMaxLength          = this.bv.getFieldElements('textCharMinMaxLength');
+        this.$textareaCharMinMaxLength      = this.bv.getFieldElements('textareaCharMinMaxLength');
+        this.$textUTF8BytesMinMaxLength     = this.bv.getFieldElements('textUTF8BytesMinMaxLength');
+        this.$textareaUTF8BytesMinMaxLength = this.bv.getFieldElements('textareaUTF8BytesMinMaxLength');
+    });
+
+    afterEach(function() {
+        $('#stringLengthForm').bootstrapValidator('destroy').remove();
+    });
+
+    it('Valid max lengths', function() {
+        this.$textCharMaxLength.val('123456789♥');
+        this.$textareaCharMaxLength.val('123456789♥');
+        this.$textUTF8BytesMaxLength.val('1234567♥');
+        this.$textareaUTF8BytesMaxLength.val('1234567♥');
+        this.bv.validate();
+        expect(this.bv.isValid()).toBeTruthy();
+    });
+
+    it('Valid min lengths', function() {
+        this.$textCharMinLength.val('1234♥');
+        this.$textareaCharMinLength.val('1234♥');
+        this.$textUTF8BytesMinLength.val('12♥');
+        this.$textareaUTF8BytesMinLength.val('12♥');
+        this.bv.validate();
+        expect(this.bv.isValid()).toBeTruthy();
+    });
+
+    it('Valid min and max lengths', function() {
+        this.$textCharMinMaxLength.val('1234♥');
+        this.$textareaCharMinMaxLength.val('1234♥');
+        this.$textUTF8BytesMinMaxLength.val('12♥');
+        this.$textareaUTF8BytesMinMaxLength.val('12♥');
+        this.bv.validate();
+        expect(this.bv.isValid()).toBeTruthy();
+
+        this.bv.resetForm();
+        this.$textCharMinMaxLength.val('123456789♥');
+        this.$textareaCharMinMaxLength.val('123456789♥');
+        this.$textUTF8BytesMinMaxLength.val('1234567♥');
+        this.$textareaUTF8BytesMinMaxLength.val('1234567♥');
+        this.bv.validate();
+        expect(this.bv.isValid()).toBeTruthy();
+    });
+
+    it('Invalid max lengths', function() {
+        this.$textCharMaxLength.val('1234567890♥');           // 11 chars when max is 10
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$textareaCharMaxLength.val('1234567890♥');       // 11 chars when max is 10
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$textUTF8BytesMaxLength.val('12345678♥');        // 11 UTF-8 bytes when max is 10
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$textareaUTF8BytesMaxLength.val('12345678♥');    // 11 UTF-8 bytes when max is 10
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+    });
+
+    it('Invalid min lengths', function() {
+        this.$textCharMinLength.val('123♥');                  // 4 chars when min is 5
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$textareaCharMinLength.val('123♥');              // 4 chars when min is 5
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$textUTF8BytesMinLength.val('1♥');               // 4 UTF-8 bytes when min is 5
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$textareaUTF8BytesMinLength.val('1♥');           // 4 UTF-8 bytes when min is 5
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+    });
+
+    it('Invalid min and max lengths', function() {
+        this.$textCharMinMaxLength.val('123♥');               // 4 chars when min is 5 and max is 10
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$textareaCharMinMaxLength.val('123♥');           // 4 chars when min is 5 and max is 10
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$textUTF8BytesMinMaxLength.val('1♥');            // 4 UTF-8 bytes when min is 5 and max is 10
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$textareaUTF8BytesMinMaxLength.val('1♥');        // 4 UTF-8 bytes when min is 5 and max is 10
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$textCharMinMaxLength.val('1234567890♥');        // 11 chars when min is 5 and max is 10
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$textareaCharMinMaxLength.val('1234567890♥');    // 11 chars when min is 5 and max is 10
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$textUTF8BytesMinMaxLength.val('12345678♥');     // 11 UTF-8 bytes when min is 5 and max is 10
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$textareaUTF8BytesMinMaxLength.val('12345678♥'); // 11 UTF-8 bytes when min is 5 and max is 10
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+    });
+});
+
 describe('uri', function() {
     beforeEach(function() {
         $([

+ 6 - 7
test/spec/validator/stringLength.js

@@ -62,7 +62,7 @@ describe('stringLength', function() {
         $('#stringLengthForm').bootstrapValidator('destroy').remove();
     });
 
-    it('Valid Max Lengths', function() {
+    it('Valid max lengths', function() {
         this.$textCharMaxLength.val('123456789♥');
         this.$textareaCharMaxLength.val('123456789♥');
         this.$textUTF8BytesMaxLength.val('1234567♥');
@@ -71,7 +71,7 @@ describe('stringLength', function() {
         expect(this.bv.isValid()).toBeTruthy();
     });
 
-    it('Valid Min Lengths', function() {
+    it('Valid min lengths', function() {
         this.$textCharMinLength.val('1234♥');
         this.$textareaCharMinLength.val('1234♥');
         this.$textUTF8BytesMinLength.val('12♥');
@@ -80,7 +80,7 @@ describe('stringLength', function() {
         expect(this.bv.isValid()).toBeTruthy();
     });
 
-    it('Valid Min and Max Lengths', function() {
+    it('Valid min and max lengths', function() {
         this.$textCharMinMaxLength.val('1234♥');
         this.$textareaCharMinMaxLength.val('1234♥');
         this.$textUTF8BytesMinMaxLength.val('12♥');
@@ -97,7 +97,7 @@ describe('stringLength', function() {
         expect(this.bv.isValid()).toBeTruthy();
     });
 
-    it('Invalid Max Lengths', function() {
+    it('Invalid max lengths', function() {
         this.$textCharMaxLength.val('1234567890♥');           // 11 chars when max is 10
         this.bv.validate();
         expect(this.bv.isValid()).toEqual(false);
@@ -118,7 +118,7 @@ describe('stringLength', function() {
         expect(this.bv.isValid()).toEqual(false);
     });
 
-    it('Invalid Min Lengths', function() {
+    it('Invalid min lengths', function() {
         this.$textCharMinLength.val('123♥');                  // 4 chars when min is 5
         this.bv.validate();
         expect(this.bv.isValid()).toEqual(false);
@@ -139,7 +139,7 @@ describe('stringLength', function() {
         expect(this.bv.isValid()).toEqual(false);
     });
 
-    it('Invalid Min and Max Lengths', function() {
+    it('Invalid min and max lengths', function() {
         this.$textCharMinMaxLength.val('123♥');               // 4 chars when min is 5 and max is 10
         this.bv.validate();
         expect(this.bv.isValid()).toEqual(false);
@@ -179,5 +179,4 @@ describe('stringLength', function() {
         this.bv.validate();
         expect(this.bv.isValid()).toEqual(false);
     });
-
 });