submit.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. describe('submit', function() {
  2. var submitted;
  3. // Override the options
  4. $.extend($.fn.bootstrapValidator.DEFAULT_OPTIONS, {
  5. feedbackIcons: {
  6. valid: 'glyphicon glyphicon-ok',
  7. invalid: 'glyphicon glyphicon-remove',
  8. validating: 'glyphicon glyphicon-refresh'
  9. }
  10. });
  11. $.fn.bootstrapValidator.validators.fake_remote = {
  12. validate: function(validator, $field, options) {
  13. var dfd = new $.Deferred();
  14. setTimeout(function() {
  15. dfd.resolve($field, 'fake_remote', { valid: options.valid });
  16. }, 0);
  17. return dfd;
  18. }
  19. };
  20. beforeEach(function() {
  21. $([
  22. '<form id="submitForm" class="form-horizontal" role="form">',
  23. '<div class="form-group">',
  24. '<input name="username" type="text" class="form-control" value="me" required />',
  25. '</div>',
  26. '<button id="sendButton" type="submit" class="btn btn-default">Send</button>',
  27. '</form>'
  28. ].join('\n')).appendTo('body');
  29. this.$form = $('#submitForm');
  30. this.$form.bootstrapValidator()
  31. .on('success.form.bv', function(e) {
  32. e.preventDefault();
  33. ++submitted;
  34. });
  35. this.$form.submit(function(e) {
  36. e.preventDefault();
  37. });
  38. submitted = 0;
  39. this.bv = this.$form.data('bootstrapValidator');
  40. this.$username = this.bv.getFieldElements('username');
  41. });
  42. afterEach(function() {
  43. $('#submitForm').bootstrapValidator('destroy').remove();
  44. });
  45. // #481
  46. it('without callback nor remote', function(done) {
  47. $('#sendButton').click();
  48. setTimeout(function() {
  49. expect(submitted).toBe(1);
  50. done();
  51. }, 0);
  52. });
  53. // #481
  54. it('with callback returning true', function(done) {
  55. this.bv.addField('username', {
  56. validators: {
  57. callback: {
  58. message: 'Please enter an username',
  59. callback: function (value, validator, $field) {
  60. return true;
  61. }
  62. }
  63. }
  64. });
  65. $('#sendButton').click();
  66. setTimeout(function() {
  67. expect(submitted).toBe(1);
  68. done();
  69. }, 0);
  70. });
  71. // #481
  72. it('with fake remote returning true', function(done) {
  73. this.bv.addField('username', {
  74. validators: {
  75. fake_remote: {
  76. message: 'Please enter an username',
  77. valid: true
  78. }
  79. }
  80. });
  81. $('#sendButton').click();
  82. setTimeout(function() {
  83. expect(submitted).toBe(1);
  84. done();
  85. }, 100);
  86. });
  87. // #481
  88. it('with callback returning false', function(done) {
  89. this.bv.addField('username', {
  90. validators: {
  91. callback: {
  92. message: 'Please enter an username',
  93. callback: function (value, validator, $field) {
  94. return false;
  95. }
  96. }
  97. }
  98. });
  99. $('#sendButton').click();
  100. setTimeout(function() {
  101. expect(submitted).toBe(0);
  102. done();
  103. }, 0);
  104. });
  105. // #481
  106. it('with fake remote returning false', function(done) {
  107. this.bv.addField('username', {
  108. validators: {
  109. fake_remote: {
  110. message: 'Please enter an username',
  111. valid: false
  112. }
  113. }
  114. });
  115. $('#sendButton').click();
  116. setTimeout(function() {
  117. expect(submitted).toBe(0);
  118. done();
  119. }, 100);
  120. });
  121. });