between.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. function betweenCompareMin() {
  2. var compareTo = $('#betweenForm').find('[name="minAge"]').val();
  3. $('#msgMin').html('betweenCompareMin() called; compare to ' + compareTo);
  4. return compareTo;
  5. };
  6. function betweenCompareMax() {
  7. var compareTo = $('#betweenForm').find('[name="maxAge"]').val();
  8. $('#msgMax').html('betweenCompareMax() called; compare to ' + compareTo);
  9. return compareTo;
  10. };
  11. TestSuite = $.extend({}, TestSuite, {
  12. between: {
  13. compareToMin: function(value, validator, $field) {
  14. var compareTo = $('#betweenForm').find('[name="minAge"]').val();
  15. $('#msgMin').html('TestSuite.between.compareToMin() called; compare to ' + compareTo);
  16. return compareTo;
  17. },
  18. compareToMax: function(value, validator, $field) {
  19. var compareTo = $('#betweenForm').find('[name="maxAge"]').val();
  20. $('#msgMax').html('TestSuite.between.compareToMax() called; compare to ' + compareTo);
  21. return compareTo;
  22. }
  23. }
  24. });
  25. describe('between', function() {
  26. beforeEach(function() {
  27. $([
  28. '<form class="form-horizontal" id="betweenForm">',
  29. '<div id="msgMin"></div>',
  30. '<div id="msgMax"></div>',
  31. '<div class="form-group">',
  32. '<input type="text" name="minAge" />',
  33. '</div>',
  34. '<div class="form-group">',
  35. '<input type="text" name="maxAge" />',
  36. '</div>',
  37. '<div class="form-group">',
  38. '<input type="text" name="age" data-bv-between data-bv-between-min="18" data-bv-between-max="100" />',
  39. '</div>',
  40. '</form>'
  41. ].join('\n')).appendTo('body');
  42. $('#betweenForm').bootstrapValidator();
  43. this.bv = $('#betweenForm').data('bootstrapValidator');
  44. this.$minAge = this.bv.getFieldElements('minAge');
  45. this.$maxAge = this.bv.getFieldElements('maxAge');
  46. this.$age = this.bv.getFieldElements('age');
  47. });
  48. afterEach(function() {
  49. $('#betweenForm').bootstrapValidator('destroy').remove();
  50. });
  51. it('not a number', function() {
  52. this.$age.val('50abc');
  53. this.bv.validate();
  54. expect(this.bv.isValid()).toEqual(false);
  55. });
  56. it('compare to value', function() {
  57. this.$age.val(10);
  58. this.bv.validate();
  59. expect(this.bv.isValid()).toEqual(false);
  60. this.bv.resetForm();
  61. this.$age.val(120);
  62. this.bv.validate();
  63. expect(this.bv.isValid()).toEqual(false);
  64. this.bv.resetForm();
  65. this.$age.val(30);
  66. this.bv.validate();
  67. expect(this.bv.isValid()).toBeTruthy();
  68. });
  69. it('compare to other field', function() {
  70. this.bv.updateOption('age', 'between', 'min', 'minAge');
  71. this.bv.updateOption('age', 'between', 'max', 'maxAge');
  72. this.$minAge.val(2);
  73. this.$maxAge.val(10);
  74. this.$age.val(5);
  75. this.bv.validate();
  76. expect(this.bv.isValid()).toBeTruthy();
  77. this.bv.resetForm();
  78. this.$minAge.val(20);
  79. this.$maxAge.val(40);
  80. this.$age.val(50);
  81. this.bv.validate();
  82. expect(this.bv.isValid()).toEqual(false);
  83. expect(this.bv.getMessages('age', 'between')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.between['default'], [this.$minAge.val(), this.$maxAge.val()]));
  84. });
  85. // #1048
  86. it('compare to other field that value has comma', function() {
  87. this.bv.updateOption('age', 'between', 'min', 'minAge');
  88. this.bv.updateOption('age', 'between', 'max', 'maxAge');
  89. this.$minAge.val('2,5');
  90. this.$maxAge.val('10,5');
  91. this.$age.val(5);
  92. this.bv.validate();
  93. expect(this.bv.isValid()).toBeTruthy();
  94. this.bv.resetForm();
  95. this.$minAge.val('20,5');
  96. this.$maxAge.val('40,5');
  97. this.$age.val(50);
  98. this.bv.validate();
  99. expect(this.bv.isValid()).toEqual(false);
  100. expect(this.bv.getMessages('age', 'between')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.between['default'], [this.$minAge.val(), this.$maxAge.val()]));
  101. });
  102. it('compare to return value of a function', function() {
  103. this.bv.updateOption('age', 'between', 'min', 'betweenCompareMin');
  104. this.bv.updateOption('age', 'between', 'max', 'betweenCompareMax');
  105. this.$minAge.val(20);
  106. this.$maxAge.val(30);
  107. this.$age.val(18);
  108. this.bv.validate();
  109. expect($('#msgMin').html()).toEqual('betweenCompareMin() called; compare to 20');
  110. expect($('#msgMax').html()).toEqual('betweenCompareMax() called; compare to 30');
  111. expect(this.bv.isValid()).toEqual(false);
  112. expect(this.bv.getMessages('age', 'between')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.between['default'], [this.$minAge.val(), this.$maxAge.val()]));
  113. this.bv.resetForm();
  114. this.$minAge.val(2);
  115. this.$maxAge.val(10);
  116. this.$age.val(6);
  117. this.bv.validate();
  118. expect($('#msgMin').html()).toEqual('betweenCompareMin() called; compare to 2');
  119. expect($('#msgMax').html()).toEqual('betweenCompareMax() called; compare to 10');
  120. expect(this.bv.isValid()).toBeTruthy();
  121. });
  122. it('compare to return value of a namespace function', function() {
  123. this.bv.updateOption('age', 'between', 'min', 'TestSuite.between.compareToMin');
  124. this.bv.updateOption('age', 'between', 'max', 'TestSuite.between.compareToMax');
  125. this.$minAge.val(20);
  126. this.$maxAge.val(30);
  127. this.$age.val(40);
  128. this.bv.validate();
  129. expect($('#msgMin').html()).toEqual('TestSuite.between.compareToMin() called; compare to 20');
  130. expect($('#msgMax').html()).toEqual('TestSuite.between.compareToMax() called; compare to 30');
  131. expect(this.bv.isValid()).toEqual(false);
  132. expect(this.bv.getMessages('age', 'between')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.between['default'], [this.$minAge.val(), this.$maxAge.val()]));
  133. this.bv.resetForm();
  134. this.$minAge.val(2);
  135. this.$maxAge.val(10);
  136. this.$age.val(5);
  137. this.bv.validate();
  138. expect($('#msgMin').html()).toEqual('TestSuite.between.compareToMin() called; compare to 2');
  139. expect($('#msgMax').html()).toEqual('TestSuite.between.compareToMax() called; compare to 10');
  140. expect(this.bv.isValid()).toBeTruthy();
  141. });
  142. });