tests.js 3.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
// swal() sould add the modal to the DOM + make it visible
test("modal shows up", function() {
  equal($('.sweet-alert').length, 0);

  swal("Hello world!");
  
  ok($('.sweet-alert').is(':visible'));
});

// Clicking the confirm-button should dismiss the modal
test("dismiss modal with confirm-button", function(assert) {
  var done = assert.async();
  swal("Dismiss me");

  var $modal = $('.sweet-alert');
  $modal.find('button.confirm').click();
  
  setTimeout(function() {
    assert.ok($modal.is(':hidden'));
    done();
  }, 500);
});

test("dismiss modal with esc-key", function(assert) {
  var done = assert.async();
  swal("Dismiss me");

  var $modal = $('.sweet-alert');
  $(document).trigger($.Event('keydown', {
    keyCode: 27 
  }));

  setTimeout(function() {
    assert.ok($modal.is(':hidden'));
    done();
  }, 500);
});

test("modals stays on with esc-key if allowEscapeKey is false", function(assert) {
  var done = assert.async();
  swal({
    title: "Dismiss me",
    allowEscapeKey: false
  });

  var $modal = $('.sweet-alert');
  $(document).trigger($.Event('keydown', {
    keyCode: 27 
  }));

  setTimeout(function() {
    assert.ok($modal.is(':visible'));
    done();
  }, 500);
});

/*
 * Make sure that when using { showCancelButton: true }:
 * - The cancel-button is visible on the modal
 * - Clicking on it dismisses the modal
 */
test("cancel-button works", function(assert) {
  var done = assert.async();
  swal({
    title: "Test",
    showCancelButton: true
  });
  
  var $modal = $('.sweet-alert');
  var $cancelBtn = $modal.find('button.cancel');
  ok($cancelBtn.is(':visible'));

  $cancelBtn.click();

  setTimeout(function() {
    assert.ok($modal.is(':hidden'));
    done();
  }, 500);
});

// Clicking the overlay should not dismiss the modal...
test("clicking the overlay does not dismiss modal", function(assert) {
  var done = assert.async();
  swal("Test");

  var $modal = $('.sweet-alert');
  $('.sweet-overlay').click();

  setTimeout(function() {
    assert.ok($modal.is(':visible'));
    done();
  }, 500);
});


// ...except if we pass allowOutsideClick: true
test("clicking the overlay (with allowOutsideClick option) dismisses modal", function(assert) {
  var done = assert.async();
  swal({
    title: "Test",
    allowOutsideClick: true 
  });

  var $modal = $('.sweet-alert');
  $('.sweet-overlay').click();

  setTimeout(function() {
    assert.ok($modal.is(':hidden'));
    done();
  }, 500);
});

test("timer works", function(assert) {
  var done = assert.async();
  swal({
    title: "Timer test",
    showConfirmButton: false,
    timer: 500
  });

  var $modal = $('.sweet-alert');
  assert.ok($modal.find('button.cancel, button.confirm').is(':hidden'));

  setTimeout(function() {
    assert.ok($modal.is(':hidden'));
    done();
  }, 1000);
});

test("prompt functionality works", function() {
  swal({
    title: "Prompt test",
    type: "input",
    inputPlaceholder: "Placeholder text"
  });

  var $modal = $('.sweet-alert');

  ok($modal.find('fieldset input').is(':visible'));
  equal($modal.find('fieldset input').attr('placeholder'), "Placeholder text");
});