between.js 5.8 KB

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