stringLength.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. describe('stringLength', function() {
  2. beforeEach(function() {
  3. $([
  4. '<form class="form-horizontal" id="stringLengthForm">',
  5. '<div class="form-group">',
  6. '<input type="text" name="textCharMaxLength" data-bv-stringlength data-bv-stringlength-max="10" />',
  7. '</div>',
  8. '<div class="form-group">',
  9. '<textarea name="textareaCharMaxLength" data-bv-stringlength data-bv-stringlength-max="10"></textarea>',
  10. '</div>',
  11. '<div class="form-group">',
  12. '<input type="text" name="textUTF8BytesMaxLength" data-bv-stringlength data-bv-stringlength-utf8bytes="true" data-bv-stringlength-max="10" />',
  13. '</div>',
  14. '<div class="form-group">',
  15. '<textarea name="textareaUTF8BytesMaxLength" data-bv-stringlength data-bv-stringlength-utf8bytes="true" data-bv-stringlength-max="10"></textarea>',
  16. '</div>',
  17. '<div class="form-group">',
  18. '<input type="text" name="textCharMinLength" data-bv-stringlength data-bv-stringlength-min="5" />',
  19. '</div>',
  20. '<div class="form-group">',
  21. '<textarea name="textareaCharMinLength" data-bv-stringlength data-bv-stringlength-min="5"></textarea>',
  22. '</div>',
  23. '<div class="form-group">',
  24. '<input type="text" name="textUTF8BytesMinLength" data-bv-stringlength data-bv-stringlength-utf8bytes="true" data-bv-stringlength-min="5" />',
  25. '</div>',
  26. '<div class="form-group">',
  27. '<textarea name="textareaUTF8BytesMinLength" data-bv-stringlength data-bv-stringlength-utf8bytes="true" data-bv-stringlength-min="5"></textarea>',
  28. '</div>',
  29. '<div class="form-group">',
  30. '<input type="text" name="textCharMinMaxLength" data-bv-stringlength data-bv-stringlength-min="5" data-bv-stringlength-max="10" />',
  31. '</div>',
  32. '<div class="form-group">',
  33. '<textarea name="textareaCharMinMaxLength" data-bv-stringlength data-bv-stringlength-min="5" data-bv-stringlength-max="10"></textarea>',
  34. '</div>',
  35. '<div class="form-group">',
  36. '<input type="text" name="textUTF8BytesMinMaxLength" data-bv-stringlength data-bv-stringlength-utf8bytes="true" data-bv-stringlength-min="5" data-bv-stringlength-max="10" />',
  37. '</div>',
  38. '<div class="form-group">',
  39. '<textarea name="textareaUTF8BytesMinMaxLength" data-bv-stringlength data-bv-stringlength-utf8bytes="true" data-bv-stringlength-min="5" data-bv-stringlength-max="10"></textarea>',
  40. '</div>',
  41. '</form>'
  42. ].join('\n')).appendTo('body');
  43. $('#stringLengthForm').bootstrapValidator();
  44. this.bv = $('#stringLengthForm').data('bootstrapValidator');
  45. this.$textCharMaxLength = this.bv.getFieldElements('textCharMaxLength');
  46. this.$textareaCharMaxLength = this.bv.getFieldElements('textareaCharMaxLength');
  47. this.$textUTF8BytesMaxLength = this.bv.getFieldElements('textUTF8BytesMaxLength');
  48. this.$textareaUTF8BytesMaxLength = this.bv.getFieldElements('textareaUTF8BytesMaxLength');
  49. this.$textCharMinLength = this.bv.getFieldElements('textCharMinLength');
  50. this.$textareaCharMinLength = this.bv.getFieldElements('textareaCharMinLength');
  51. this.$textUTF8BytesMinLength = this.bv.getFieldElements('textUTF8BytesMinLength');
  52. this.$textareaUTF8BytesMinLength = this.bv.getFieldElements('textareaUTF8BytesMinLength');
  53. this.$textCharMinMaxLength = this.bv.getFieldElements('textCharMinMaxLength');
  54. this.$textareaCharMinMaxLength = this.bv.getFieldElements('textareaCharMinMaxLength');
  55. this.$textUTF8BytesMinMaxLength = this.bv.getFieldElements('textUTF8BytesMinMaxLength');
  56. this.$textareaUTF8BytesMinMaxLength = this.bv.getFieldElements('textareaUTF8BytesMinMaxLength');
  57. });
  58. afterEach(function() {
  59. $('#stringLengthForm').bootstrapValidator('destroy').remove();
  60. });
  61. it('Valid Max Lengths', function() {
  62. this.$textCharMaxLength.val('123456789♥');
  63. this.$textareaCharMaxLength.val('123456789♥');
  64. this.$textUTF8BytesMaxLength.val('1234567♥');
  65. this.$textareaUTF8BytesMaxLength.val('1234567♥');
  66. this.bv.validate();
  67. expect(this.bv.isValid()).toBeTruthy();
  68. });
  69. it('Valid Min Lengths', function() {
  70. this.$textCharMinLength.val('1234♥');
  71. this.$textareaCharMinLength.val('1234♥');
  72. this.$textUTF8BytesMinLength.val('12♥');
  73. this.$textareaUTF8BytesMinLength.val('12♥');
  74. this.bv.validate();
  75. expect(this.bv.isValid()).toBeTruthy();
  76. });
  77. it('Valid Min and Max Lengths', function() {
  78. this.$textCharMinMaxLength.val('1234♥');
  79. this.$textareaCharMinMaxLength.val('1234♥');
  80. this.$textUTF8BytesMinMaxLength.val('12♥');
  81. this.$textareaUTF8BytesMinMaxLength.val('12♥');
  82. this.bv.validate();
  83. expect(this.bv.isValid()).toBeTruthy();
  84. this.bv.resetForm();
  85. this.$textCharMinMaxLength.val('123456789♥');
  86. this.$textareaCharMinMaxLength.val('123456789♥');
  87. this.$textUTF8BytesMinMaxLength.val('1234567♥');
  88. this.$textareaUTF8BytesMinMaxLength.val('1234567♥');
  89. this.bv.validate();
  90. expect(this.bv.isValid()).toBeTruthy();
  91. });
  92. it('Invalid Max Lengths', function() {
  93. this.$textCharMaxLength.val('1234567890♥'); // 11 chars when max is 10
  94. this.bv.validate();
  95. expect(this.bv.isValid()).toEqual(false);
  96. this.bv.resetForm();
  97. this.$textareaCharMaxLength.val('1234567890♥'); // 11 chars when max is 10
  98. this.bv.validate();
  99. expect(this.bv.isValid()).toEqual(false);
  100. this.bv.resetForm();
  101. this.$textUTF8BytesMaxLength.val('12345678♥'); // 11 UTF-8 bytes when max is 10
  102. this.bv.validate();
  103. expect(this.bv.isValid()).toEqual(false);
  104. this.bv.resetForm();
  105. this.$textareaUTF8BytesMaxLength.val('12345678♥'); // 11 UTF-8 bytes when max is 10
  106. this.bv.validate();
  107. expect(this.bv.isValid()).toEqual(false);
  108. });
  109. it('Invalid Min Lengths', function() {
  110. this.$textCharMinLength.val('123♥'); // 4 chars when min is 5
  111. this.bv.validate();
  112. expect(this.bv.isValid()).toEqual(false);
  113. this.bv.resetForm();
  114. this.$textareaCharMinLength.val('123♥'); // 4 chars when min is 5
  115. this.bv.validate();
  116. expect(this.bv.isValid()).toEqual(false);
  117. this.bv.resetForm();
  118. this.$textUTF8BytesMinLength.val('1♥'); // 4 UTF-8 bytes when min is 5
  119. this.bv.validate();
  120. expect(this.bv.isValid()).toEqual(false);
  121. this.bv.resetForm();
  122. this.$textareaUTF8BytesMinLength.val('1♥'); // 4 UTF-8 bytes when min is 5
  123. this.bv.validate();
  124. expect(this.bv.isValid()).toEqual(false);
  125. });
  126. it('Invalid Min and Max Lengths', function() {
  127. this.$textCharMinMaxLength.val('123♥'); // 4 chars when min is 5 and max is 10
  128. this.bv.validate();
  129. expect(this.bv.isValid()).toEqual(false);
  130. this.bv.resetForm();
  131. this.$textareaCharMinMaxLength.val('123♥'); // 4 chars when min is 5 and max is 10
  132. this.bv.validate();
  133. expect(this.bv.isValid()).toEqual(false);
  134. this.bv.resetForm();
  135. this.$textUTF8BytesMinMaxLength.val('1♥'); // 4 UTF-8 bytes when min is 5 and max is 10
  136. this.bv.validate();
  137. expect(this.bv.isValid()).toEqual(false);
  138. this.bv.resetForm();
  139. this.$textareaUTF8BytesMinMaxLength.val('1♥'); // 4 UTF-8 bytes when min is 5 and max is 10
  140. this.bv.validate();
  141. expect(this.bv.isValid()).toEqual(false);
  142. this.bv.resetForm();
  143. this.$textCharMinMaxLength.val('1234567890♥'); // 11 chars when min is 5 and max is 10
  144. this.bv.validate();
  145. expect(this.bv.isValid()).toEqual(false);
  146. this.bv.resetForm();
  147. this.$textareaCharMinMaxLength.val('1234567890♥'); // 11 chars when min is 5 and max is 10
  148. this.bv.validate();
  149. expect(this.bv.isValid()).toEqual(false);
  150. this.bv.resetForm();
  151. this.$textUTF8BytesMinMaxLength.val('12345678♥'); // 11 UTF-8 bytes when min is 5 and max is 10
  152. this.bv.validate();
  153. expect(this.bv.isValid()).toEqual(false);
  154. this.bv.resetForm();
  155. this.$textareaUTF8BytesMinMaxLength.val('12345678♥'); // 11 UTF-8 bytes when min is 5 and max is 10
  156. this.bv.validate();
  157. expect(this.bv.isValid()).toEqual(false);
  158. });
  159. });