submit.js 4.7 KB

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