between.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.$age.attr('data-bv-between-min', 'minAge')
  71. .attr('data-bv-between-max', 'maxAge');
  72. this.bv.destroy();
  73. this.bv = $('#betweenForm').bootstrapValidator().data('bootstrapValidator');
  74. this.$minAge.val(2);
  75. this.$maxAge.val(10);
  76. this.$age.val(5);
  77. this.bv.validate();
  78. expect(this.bv.isValid()).toBeTruthy();
  79. this.bv.resetForm();
  80. this.$minAge.val(20);
  81. this.$maxAge.val(40);
  82. this.$age.val(50);
  83. this.bv.validate();
  84. expect(this.bv.isValid()).toEqual(false);
  85. expect(this.bv.getMessages('age', 'between')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.between['default'], [this.$minAge.val(), this.$maxAge.val()]));
  86. });
  87. it('compare to return value of a function', function() {
  88. this.$age.attr('data-bv-between-min', 'betweenCompareMin')
  89. .attr('data-bv-between-max', 'betweenCompareMax');
  90. this.bv.destroy();
  91. this.bv = $('#betweenForm').bootstrapValidator().data('bootstrapValidator');
  92. this.$minAge.val(20);
  93. this.$maxAge.val(30);
  94. this.$age.val(18);
  95. this.bv.validate();
  96. expect($('#msgMin').html()).toEqual('betweenCompareMin() called; compare to 20');
  97. expect($('#msgMax').html()).toEqual('betweenCompareMax() called; compare to 30');
  98. expect(this.bv.isValid()).toEqual(false);
  99. expect(this.bv.getMessages('age', 'between')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.between['default'], [this.$minAge.val(), this.$maxAge.val()]));
  100. this.bv.resetForm();
  101. this.$minAge.val(2);
  102. this.$maxAge.val(10);
  103. this.$age.val(6);
  104. this.bv.validate();
  105. expect($('#msgMin').html()).toEqual('betweenCompareMin() called; compare to 2');
  106. expect($('#msgMax').html()).toEqual('betweenCompareMax() called; compare to 10');
  107. expect(this.bv.isValid()).toBeTruthy();
  108. });
  109. it('compare to return value of a namespace function', function() {
  110. this.$age.attr('data-bv-between-min', 'TestSuite.between.compareToMin')
  111. .attr('data-bv-between-max', 'TestSuite.between.compareToMax');
  112. this.bv.destroy();
  113. this.bv = $('#betweenForm').bootstrapValidator().data('bootstrapValidator');
  114. this.$minAge.val(20);
  115. this.$maxAge.val(30);
  116. this.$age.val(40);
  117. this.bv.validate();
  118. expect($('#msgMin').html()).toEqual('TestSuite.between.compareToMin() called; compare to 20');
  119. expect($('#msgMax').html()).toEqual('TestSuite.between.compareToMax() called; compare to 30');
  120. expect(this.bv.isValid()).toEqual(false);
  121. expect(this.bv.getMessages('age', 'between')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.between['default'], [this.$minAge.val(), this.$maxAge.val()]));
  122. this.bv.resetForm();
  123. this.$minAge.val(2);
  124. this.$maxAge.val(10);
  125. this.$age.val(5);
  126. this.bv.validate();
  127. expect($('#msgMin').html()).toEqual('TestSuite.between.compareToMin() called; compare to 2');
  128. expect($('#msgMax').html()).toEqual('TestSuite.between.compareToMax() called; compare to 10');
  129. expect(this.bv.isValid()).toBeTruthy();
  130. });
  131. });