greaterThan.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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('not a number', function() {
  37. this.$age.val('20abc');
  38. this.bv.validate();
  39. expect(this.bv.isValid()).toEqual(false);
  40. });
  41. it('compare to value', function() {
  42. this.$age.val(10);
  43. this.bv.validate();
  44. expect(this.bv.isValid()).toEqual(false);
  45. this.bv.resetForm();
  46. this.$age.val(20);
  47. this.bv.validate();
  48. expect(this.bv.isValid()).toBeTruthy();
  49. });
  50. it('value with coma separator', function() {
  51. this.$age.val('10,4');
  52. this.bv.validate();
  53. expect(this.bv.isValid()).toEqual(false);
  54. this.bv.resetForm();
  55. this.$age.val('18,678');
  56. this.bv.validate();
  57. expect(this.bv.isValid()).toBeTruthy();
  58. });
  59. it('compare to other field', function() {
  60. this.$age.attr('data-bv-greaterthan-value', 'minAge');
  61. this.bv.destroy();
  62. this.bv = $('#greaterThanForm').bootstrapValidator().data('bootstrapValidator');
  63. this.$minAge.val(10);
  64. this.$age.val(20);
  65. this.bv.validate();
  66. expect(this.bv.isValid()).toBeTruthy();
  67. this.bv.resetForm();
  68. this.$minAge.val(20);
  69. this.$age.val(10);
  70. this.bv.validate();
  71. expect(this.bv.isValid()).toEqual(false);
  72. expect(this.bv.getMessages('age', 'greaterThan')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.greaterThan['default'], this.$minAge.val()));
  73. });
  74. it('compare to return value of a function', function() {
  75. this.$age.attr('data-bv-greaterthan-value', 'greaterThanCompare');
  76. this.bv.destroy();
  77. this.bv = $('#greaterThanForm').bootstrapValidator().data('bootstrapValidator');
  78. this.$minAge.val(20);
  79. this.$age.val(18);
  80. this.bv.validate();
  81. expect($('#msg').html()).toEqual('greaterThanCompare() called; compare to 20');
  82. expect(this.bv.isValid()).toEqual(false);
  83. expect(this.bv.getMessages('age', 'greaterThan')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.greaterThan['default'], this.$minAge.val()));
  84. this.bv.resetForm();
  85. this.$minAge.val(18);
  86. this.$age.val(20);
  87. this.bv.validate();
  88. expect($('#msg').html()).toEqual('greaterThanCompare() called; compare to 18');
  89. expect(this.bv.isValid()).toBeTruthy();
  90. });
  91. it('compare to return value of a namespace function', function() {
  92. this.$age.attr('data-bv-greaterthan-value', 'TestSuite.greaterThan.compareTo');
  93. this.bv.destroy();
  94. this.bv = $('#greaterThanForm').bootstrapValidator().data('bootstrapValidator');
  95. this.$minAge.val(20);
  96. this.$age.val(18);
  97. this.bv.validate();
  98. expect($('#msg').html()).toEqual('TestSuite.greaterThan.compareTo() called; compare to 20');
  99. expect(this.bv.isValid()).toEqual(false);
  100. expect(this.bv.getMessages('age', 'greaterThan')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.greaterThan['default'], this.$minAge.val()));
  101. this.bv.resetForm();
  102. this.$minAge.val(18);
  103. this.$age.val(20);
  104. this.bv.validate();
  105. expect($('#msg').html()).toEqual('TestSuite.greaterThan.compareTo() called; compare to 18');
  106. expect(this.bv.isValid()).toBeTruthy();
  107. });
  108. });