greaterThan.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. function greaterThanCompare() {
  2. var compareTo = $('#greaterThanForm').find('[name="minAge"]').val();
  3. $('#msg').html('greaterThanCompare() called; compare to ' + compareTo);
  4. return compareTo;
  5. };
  6. TestSuite = $.extend({}, TestSuite, {
  7. greaterThan: {
  8. compareTo: function(value, validator, $field) {
  9. var compareTo = $('#greaterThanForm').find('[name="minAge"]').val();
  10. $('#msg').html('TestSuite.greaterThan.compareTo() called; compare to ' + compareTo);
  11. return compareTo;
  12. }
  13. }
  14. });
  15. describe('greaterThan', function() {
  16. beforeEach(function() {
  17. $([
  18. '<form class="form-horizontal" id="greaterThanForm">',
  19. '<div id="msg"></div>',
  20. '<div class="form-group">',
  21. '<input type="text" name="minAge" />',
  22. '</div>',
  23. '<div class="form-group">',
  24. '<input type="text" name="age" data-bv-greaterthan data-bv-greaterthan-value="18" />',
  25. '</div>',
  26. '</form>'
  27. ].join('\n')).appendTo('body');
  28. $('#greaterThanForm').bootstrapValidator();
  29. this.bv = $('#greaterThanForm').data('bootstrapValidator');
  30. this.$minAge = this.bv.getFieldElements('minAge');
  31. this.$age = this.bv.getFieldElements('age');
  32. });
  33. afterEach(function() {
  34. $('#greaterThanForm').bootstrapValidator('destroy').remove();
  35. });
  36. it('compare to value', function() {
  37. this.$age.val(10);
  38. this.bv.validate();
  39. expect(this.bv.isValid()).toEqual(false);
  40. this.bv.resetForm();
  41. this.$age.val(20);
  42. this.bv.validate();
  43. expect(this.bv.isValid()).toBeTruthy();
  44. });
  45. it('compare to other field', function() {
  46. this.$age.attr('data-bv-greaterthan-value', 'minAge');
  47. this.bv.destroy();
  48. this.bv = $('#greaterThanForm').bootstrapValidator().data('bootstrapValidator');
  49. this.$minAge.val(10);
  50. this.$age.val(20);
  51. this.bv.validate();
  52. expect(this.bv.isValid()).toBeTruthy();
  53. this.bv.resetForm();
  54. this.$minAge.val(20);
  55. this.$age.val(10);
  56. this.bv.validate();
  57. expect(this.bv.isValid()).toEqual(false);
  58. expect(this.bv.getMessages('age', 'greaterThan')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.greaterThan['default'], this.$minAge.val()));
  59. });
  60. it('compare to return value of a function', function() {
  61. this.$age.attr('data-bv-greaterthan-value', 'greaterThanCompare');
  62. this.bv.destroy();
  63. this.bv = $('#greaterThanForm').bootstrapValidator().data('bootstrapValidator');
  64. this.$minAge.val(20);
  65. this.$age.val(18);
  66. this.bv.validate();
  67. expect($('#msg').html()).toEqual('greaterThanCompare() called; compare to 20');
  68. expect(this.bv.isValid()).toEqual(false);
  69. expect(this.bv.getMessages('age', 'greaterThan')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.greaterThan['default'], this.$minAge.val()));
  70. this.bv.resetForm();
  71. this.$minAge.val(18);
  72. this.$age.val(20);
  73. this.bv.validate();
  74. expect($('#msg').html()).toEqual('greaterThanCompare() called; compare to 18');
  75. expect(this.bv.isValid()).toBeTruthy();
  76. });
  77. it('compare to return value of a namespace function', function() {
  78. this.$age.attr('data-bv-greaterthan-value', 'TestSuite.greaterThan.compareTo');
  79. this.bv.destroy();
  80. this.bv = $('#greaterThanForm').bootstrapValidator().data('bootstrapValidator');
  81. this.$minAge.val(20);
  82. this.$age.val(18);
  83. this.bv.validate();
  84. expect($('#msg').html()).toEqual('TestSuite.greaterThan.compareTo() called; compare to 20');
  85. expect(this.bv.isValid()).toEqual(false);
  86. expect(this.bv.getMessages('age', 'greaterThan')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.greaterThan['default'], this.$minAge.val()));
  87. this.bv.resetForm();
  88. this.$minAge.val(18);
  89. this.$age.val(20);
  90. this.bv.validate();
  91. expect($('#msg').html()).toEqual('TestSuite.greaterThan.compareTo() called; compare to 18');
  92. expect(this.bv.isValid()).toBeTruthy();
  93. });
  94. });