alert.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. $(function () {
  2. 'use strict';
  3. QUnit.module('alert plugin')
  4. QUnit.test('should be defined on jquery object', function (assert) {
  5. assert.expect(1)
  6. assert.ok($(document.body).alert, 'alert method is defined')
  7. })
  8. QUnit.module('alert', {
  9. beforeEach: function () {
  10. // Run all tests in noConflict mode -- it's the only way to ensure that the plugin works in noConflict mode
  11. $.fn.bootstrapAlert = $.fn.alert.noConflict()
  12. },
  13. afterEach: function () {
  14. $.fn.alert = $.fn.bootstrapAlert
  15. delete $.fn.bootstrapAlert
  16. }
  17. })
  18. QUnit.test('should provide no conflict', function (assert) {
  19. assert.expect(1)
  20. assert.strictEqual($.fn.alert, undefined, 'alert was set back to undefined (org value)')
  21. })
  22. QUnit.test('should return jquery collection containing the element', function (assert) {
  23. assert.expect(2)
  24. var $el = $('<div/>')
  25. var $alert = $el.bootstrapAlert()
  26. assert.ok($alert instanceof $, 'returns jquery collection')
  27. assert.strictEqual($alert[0], $el[0], 'collection contains element')
  28. })
  29. QUnit.test('should fade element out on clicking .close', function (assert) {
  30. assert.expect(1)
  31. var alertHTML = '<div class="alert alert-danger fade in">'
  32. + '<a class="close" href="#" data-dismiss="alert">×</a>'
  33. + '<p><strong>Holy guacamole!</strong> Best check yo self, you\'re not looking too good.</p>'
  34. + '</div>'
  35. var $alert = $(alertHTML).bootstrapAlert()
  36. $alert.find('.close').trigger('click')
  37. assert.strictEqual($alert.hasClass('in'), false, 'remove .in class on .close click')
  38. })
  39. QUnit.test('should remove element when clicking .close', function (assert) {
  40. assert.expect(2)
  41. var done = assert.async()
  42. var alertHTML = '<div class="alert alert-danger fade in">'
  43. + '<a class="close" href="#" data-dismiss="alert">×</a>'
  44. + '<p><strong>Holy guacamole!</strong> Best check yo self, you\'re not looking too good.</p>'
  45. + '</div>'
  46. var $alert = $(alertHTML).appendTo('#qunit-fixture').bootstrapAlert()
  47. assert.notEqual($('#qunit-fixture').find('.alert').length, 0, 'element added to dom')
  48. $alert.on('closed.bs.alert', function () {
  49. assert.strictEqual($('#qunit-fixture').find('.alert').length, 0, 'element removed from dom')
  50. done()
  51. })
  52. $alert.find('.close').trigger('click')
  53. })
  54. QUnit.test('should not fire closed when close is prevented', function (assert) {
  55. assert.expect(1)
  56. var done = assert.async()
  57. $('<div class="alert"/>')
  58. .on('close.bs.alert', function (e) {
  59. e.preventDefault()
  60. assert.ok(true, 'close event fired')
  61. done()
  62. })
  63. .on('closed.bs.alert', function () {
  64. assert.ok(false, 'closed event fired')
  65. })
  66. .bootstrapAlert('close')
  67. })
  68. })