tests.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // swal() sould add the modal to the DOM + make it visible
  2. test("modal shows up", function() {
  3. equal($('.sweet-alert').length, 0);
  4. swal("Hello world!");
  5. ok($('.sweet-alert').is(':visible'));
  6. });
  7. // Clicking the confirm-button should dismiss the modal
  8. test("dismiss modal with confirm-button", function(assert) {
  9. var done = assert.async();
  10. swal("Dismiss me");
  11. var $modal = $('.sweet-alert');
  12. $modal.find('button.confirm').click();
  13. setTimeout(function() {
  14. assert.ok($modal.is(':hidden'));
  15. done();
  16. }, 500);
  17. });
  18. test("dismiss modal with esc-key", function(assert) {
  19. var done = assert.async();
  20. swal("Dismiss me");
  21. var $modal = $('.sweet-alert');
  22. $(document).trigger($.Event('keydown', {
  23. keyCode: 27
  24. }));
  25. setTimeout(function() {
  26. assert.ok($modal.is(':hidden'));
  27. done();
  28. }, 500);
  29. });
  30. test("modals stays on with esc-key if allowEscapeKey is false", function(assert) {
  31. var done = assert.async();
  32. swal({
  33. title: "Dismiss me",
  34. allowEscapeKey: false
  35. });
  36. var $modal = $('.sweet-alert');
  37. $(document).trigger($.Event('keydown', {
  38. keyCode: 27
  39. }));
  40. setTimeout(function() {
  41. assert.ok($modal.is(':visible'));
  42. done();
  43. }, 500);
  44. });
  45. /*
  46. * Make sure that when using { showCancelButton: true }:
  47. * - The cancel-button is visible on the modal
  48. * - Clicking on it dismisses the modal
  49. */
  50. test("cancel-button works", function(assert) {
  51. var done = assert.async();
  52. swal({
  53. title: "Test",
  54. showCancelButton: true
  55. });
  56. var $modal = $('.sweet-alert');
  57. var $cancelBtn = $modal.find('button.cancel');
  58. ok($cancelBtn.is(':visible'));
  59. $cancelBtn.click();
  60. setTimeout(function() {
  61. assert.ok($modal.is(':hidden'));
  62. done();
  63. }, 500);
  64. });
  65. // Clicking the overlay should not dismiss the modal...
  66. test("clicking the overlay does not dismiss modal", function(assert) {
  67. var done = assert.async();
  68. swal("Test");
  69. var $modal = $('.sweet-alert');
  70. $('.sweet-overlay').click();
  71. setTimeout(function() {
  72. assert.ok($modal.is(':visible'));
  73. done();
  74. }, 500);
  75. });
  76. // ...except if we pass allowOutsideClick: true
  77. test("clicking the overlay (with allowOutsideClick option) dismisses modal", function(assert) {
  78. var done = assert.async();
  79. swal({
  80. title: "Test",
  81. allowOutsideClick: true
  82. });
  83. var $modal = $('.sweet-alert');
  84. $('.sweet-overlay').click();
  85. setTimeout(function() {
  86. assert.ok($modal.is(':hidden'));
  87. done();
  88. }, 500);
  89. });
  90. test("timer works", function(assert) {
  91. var done = assert.async();
  92. swal({
  93. title: "Timer test",
  94. showConfirmButton: false,
  95. timer: 500
  96. });
  97. var $modal = $('.sweet-alert');
  98. assert.ok($modal.find('button.cancel, button.confirm').is(':hidden'));
  99. setTimeout(function() {
  100. assert.ok($modal.is(':hidden'));
  101. done();
  102. }, 1000);
  103. });
  104. test("prompt functionality works", function() {
  105. swal({
  106. title: "Prompt test",
  107. type: "input",
  108. inputPlaceholder: "Placeholder text"
  109. });
  110. var $modal = $('.sweet-alert');
  111. ok($modal.find('fieldset input').is(':visible'));
  112. equal($modal.find('fieldset input').attr('placeholder'), "Placeholder text");
  113. });