date.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. describe('date', function() {
  2. beforeEach(function () {
  3. $([
  4. '<form class="form-horizontal" id="dateForm">',
  5. '<div id="msg"></div>',
  6. '<div class="form-group">',
  7. '<input type="text" name="date" data-bv-date />',
  8. '</div>',
  9. '</form>'
  10. ].join('\n')).appendTo('body');
  11. $('#dateForm').bootstrapValidator();
  12. this.bv = $('#dateForm').data('bootstrapValidator');
  13. this.$date = this.bv.getFieldElements('date');
  14. });
  15. afterEach(function () {
  16. $('#dateForm').bootstrapValidator('destroy').remove();
  17. });
  18. it('YYYY/MM/DD', function() {
  19. this.bv.updateOption('date', 'date', 'format', 'YYYY/MM/DD');
  20. this.$date.val('2000/01/30');
  21. this.bv.validate();
  22. expect(this.bv.isValid()).toBeTruthy();
  23. // Invalid year
  24. this.bv.resetForm();
  25. this.$date.val('100/10/20');
  26. this.bv.validate();
  27. expect(this.bv.isValid()).toEqual(false);
  28. // Invalid month
  29. this.bv.resetForm();
  30. this.$date.val('2000/00/10');
  31. this.bv.validate();
  32. expect(this.bv.isValid()).toEqual(false);
  33. this.bv.resetForm();
  34. this.$date.val('2000/15/10');
  35. this.bv.validate();
  36. expect(this.bv.isValid()).toEqual(false);
  37. // Invalid day
  38. this.bv.resetForm();
  39. this.$date.val('2000/03/00');
  40. this.bv.validate();
  41. expect(this.bv.isValid()).toEqual(false);
  42. this.bv.resetForm();
  43. this.$date.val('2000/10/32');
  44. this.bv.validate();
  45. expect(this.bv.isValid()).toEqual(false);
  46. // Consist invalid characters
  47. // #310
  48. this.bv.resetForm();
  49. this.$date.val('aaaa/');
  50. this.bv.validate();
  51. expect(this.bv.isValid()).toEqual(false);
  52. this.bv.resetForm();
  53. this.$date.val('2004df/1dd1/5ffg');
  54. this.bv.validate();
  55. expect(this.bv.isValid()).toEqual(false);
  56. // #475
  57. this.bv.resetForm();
  58. this.$date.val('2014/09');
  59. this.bv.validate();
  60. expect(this.bv.isValid()).toEqual(false);
  61. this.bv.resetForm();
  62. this.$date.val('2014/09/');
  63. this.bv.validate();
  64. expect(this.bv.isValid()).toEqual(false);
  65. this.bv.resetForm();
  66. this.$date.val('2014//15');
  67. this.bv.validate();
  68. expect(this.bv.isValid()).toEqual(false);
  69. this.bv.resetForm();
  70. this.$date.val('/09/15');
  71. this.bv.validate();
  72. expect(this.bv.isValid()).toEqual(false);
  73. });
  74. it('MM/DD/YYYY', function() {
  75. this.bv.updateOption('date', 'date', 'format', 'MM/DD/YYYY');
  76. this.$date.val('09/15/2020');
  77. this.bv.validate();
  78. expect(this.bv.isValid()).toBeTruthy();
  79. this.bv.resetForm();
  80. this.$date.val('09/15');
  81. this.bv.validate();
  82. expect(this.bv.isValid()).toEqual(false);
  83. this.bv.resetForm();
  84. this.$date.val('09/15/');
  85. this.bv.validate();
  86. expect(this.bv.isValid()).toEqual(false);
  87. });
  88. it('number of days in February', function() {
  89. this.bv.updateOption('date', 'date', 'format', 'YYYY/MM/DD');
  90. this.$date.val('2000/02/28');
  91. this.bv.validate();
  92. expect(this.bv.isValid()).toBeTruthy();
  93. this.bv.resetForm();
  94. this.$date.val('2000/02/29');
  95. this.bv.validate();
  96. expect(this.bv.isValid()).toBeTruthy();
  97. this.bv.resetForm();
  98. this.$date.val('2001/02/29');
  99. this.bv.validate();
  100. expect(this.bv.isValid()).toEqual(false);
  101. });
  102. });