toast.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. $(function () {
  2. 'use strict'
  3. if (typeof bootstrap !== 'undefined') {
  4. window.Toast = bootstrap.Toast
  5. }
  6. QUnit.module('toast plugin')
  7. QUnit.test('should be defined on jquery object', function (assert) {
  8. assert.expect(1)
  9. assert.ok($(document.body).toast, 'toast method is defined')
  10. })
  11. QUnit.module('toast', {
  12. beforeEach: function () {
  13. // Run all tests in noConflict mode -- it's the only way to ensure that the plugin works in noConflict mode
  14. $.fn.bootstrapToast = $.fn.toast.noConflict()
  15. },
  16. afterEach: function () {
  17. $.fn.toast = $.fn.bootstrapToast
  18. delete $.fn.bootstrapToast
  19. $('#qunit-fixture').html('')
  20. }
  21. })
  22. QUnit.test('should provide no conflict', function (assert) {
  23. assert.expect(1)
  24. assert.strictEqual(typeof $.fn.toast, 'undefined', 'toast was set back to undefined (org value)')
  25. })
  26. QUnit.test('should return the current version', function (assert) {
  27. assert.expect(1)
  28. assert.strictEqual(typeof Toast.VERSION, 'string')
  29. })
  30. QUnit.test('should throw explicit error on undefined method', function (assert) {
  31. assert.expect(1)
  32. var $el = $('<div/>')
  33. $el.bootstrapToast()
  34. try {
  35. $el.bootstrapToast('noMethod')
  36. } catch (err) {
  37. assert.strictEqual(err.message, 'No method named "noMethod"')
  38. }
  39. })
  40. QUnit.test('should return jquery collection containing the element', function (assert) {
  41. assert.expect(2)
  42. var $el = $('<div/>')
  43. var $toast = $el.bootstrapToast()
  44. assert.ok($toast instanceof $, 'returns jquery collection')
  45. assert.strictEqual($toast[0], $el[0], 'collection contains element')
  46. })
  47. QUnit.test('should auto hide', function (assert) {
  48. assert.expect(1)
  49. var done = assert.async()
  50. var toastHtml =
  51. '<div class="toast" data-delay="1">' +
  52. '<div class="toast-body">' +
  53. 'a simple toast' +
  54. '</div>' +
  55. '</div>'
  56. var $toast = $(toastHtml)
  57. .bootstrapToast()
  58. .appendTo($('#qunit-fixture'))
  59. $toast.on('hidden.bs.toast', function () {
  60. assert.strictEqual($toast.hasClass('show'), false)
  61. done()
  62. })
  63. .bootstrapToast('show')
  64. })
  65. QUnit.test('should not add fade class', function (assert) {
  66. assert.expect(1)
  67. var done = assert.async()
  68. var toastHtml =
  69. '<div class="toast" data-delay="1" data-animation="false">' +
  70. '<div class="toast-body">' +
  71. 'a simple toast' +
  72. '</div>' +
  73. '</div>'
  74. var $toast = $(toastHtml)
  75. .bootstrapToast()
  76. .appendTo($('#qunit-fixture'))
  77. $toast.on('shown.bs.toast', function () {
  78. assert.strictEqual($toast.hasClass('fade'), false)
  79. done()
  80. })
  81. .bootstrapToast('show')
  82. })
  83. QUnit.test('should allow to hide toast manually', function (assert) {
  84. assert.expect(1)
  85. var done = assert.async()
  86. var toastHtml =
  87. '<div class="toast" data-delay="1" data-autohide="false">' +
  88. '<div class="toast-body">' +
  89. 'a simple toast' +
  90. '</div>' +
  91. '</div>'
  92. var $toast = $(toastHtml)
  93. .bootstrapToast()
  94. .appendTo($('#qunit-fixture'))
  95. $toast
  96. .on('shown.bs.toast', function () {
  97. $toast.bootstrapToast('hide')
  98. })
  99. .on('hidden.bs.toast', function () {
  100. assert.strictEqual($toast.hasClass('show'), false)
  101. done()
  102. })
  103. .bootstrapToast('show')
  104. })
  105. QUnit.test('should do nothing when we call hide on a non shown toast', function (assert) {
  106. assert.expect(1)
  107. var $toast = $('<div />')
  108. .bootstrapToast()
  109. .appendTo($('#qunit-fixture'))
  110. var spy = sinon.spy($toast[0].classList, 'contains')
  111. $toast.bootstrapToast('hide')
  112. assert.strictEqual(spy.called, true)
  113. })
  114. QUnit.test('should allow to destroy toast', function (assert) {
  115. assert.expect(2)
  116. var $toast = $('<div />')
  117. .bootstrapToast()
  118. .appendTo($('#qunit-fixture'))
  119. assert.ok(typeof $toast.data('bs.toast') !== 'undefined')
  120. $toast.bootstrapToast('dispose')
  121. assert.ok(typeof $toast.data('bs.toast') === 'undefined')
  122. })
  123. QUnit.test('should allow to destroy toast and hide it before that', function (assert) {
  124. assert.expect(4)
  125. var done = assert.async()
  126. var toastHtml =
  127. '<div class="toast" data-delay="0" data-autohide="false">' +
  128. '<div class="toast-body">' +
  129. 'a simple toast' +
  130. '</div>' +
  131. '</div>'
  132. var $toast = $(toastHtml)
  133. .bootstrapToast()
  134. .appendTo($('#qunit-fixture'))
  135. $toast.one('shown.bs.toast', function () {
  136. setTimeout(function () {
  137. assert.ok($toast.hasClass('show'))
  138. assert.ok(typeof $toast.data('bs.toast') !== 'undefined')
  139. $toast.bootstrapToast('dispose')
  140. assert.ok(typeof $toast.data('bs.toast') === 'undefined')
  141. assert.ok($toast.hasClass('show') === false)
  142. done()
  143. }, 1)
  144. })
  145. .bootstrapToast('show')
  146. })
  147. QUnit.test('should allow to config in js', function (assert) {
  148. assert.expect(1)
  149. var done = assert.async()
  150. var toastHtml =
  151. '<div class="toast">' +
  152. '<div class="toast-body">' +
  153. 'a simple toast' +
  154. '</div>' +
  155. '</div>'
  156. var $toast = $(toastHtml)
  157. .bootstrapToast({
  158. delay: 1
  159. })
  160. .appendTo($('#qunit-fixture'))
  161. $toast.on('shown.bs.toast', function () {
  162. assert.strictEqual($toast.hasClass('show'), true)
  163. done()
  164. })
  165. .bootstrapToast('show')
  166. })
  167. QUnit.test('should close toast when close element with data-dismiss attribute is set', function (assert) {
  168. assert.expect(2)
  169. var done = assert.async()
  170. var toastHtml =
  171. '<div class="toast" data-delay="1" data-autohide="false" data-animation="false">' +
  172. '<button type="button" class="ml-2 mb-1 close" data-dismiss="toast">' +
  173. 'close' +
  174. '</button>' +
  175. '</div>'
  176. var $toast = $(toastHtml)
  177. .bootstrapToast()
  178. .appendTo($('#qunit-fixture'))
  179. $toast
  180. .on('shown.bs.toast', function () {
  181. assert.strictEqual($toast.hasClass('show'), true)
  182. var button = $toast.find('.close')
  183. button.trigger('click')
  184. })
  185. .on('hidden.bs.toast', function () {
  186. assert.strictEqual($toast.hasClass('show'), false)
  187. done()
  188. })
  189. .bootstrapToast('show')
  190. })
  191. QUnit.test('should expose default setting to allow to override them', function (assert) {
  192. assert.expect(1)
  193. var defaultDelay = 1000
  194. Toast.Default.delay = defaultDelay
  195. var toastHtml =
  196. '<div class="toast" data-autohide="false" data-animation="false">' +
  197. '<button type="button" class="ml-2 mb-1 close" data-dismiss="toast">' +
  198. 'close' +
  199. '</button>' +
  200. '</div>'
  201. var $toast = $(toastHtml)
  202. .bootstrapToast()
  203. var toast = $toast.data('bs.toast')
  204. assert.strictEqual(toast._config.delay, defaultDelay)
  205. })
  206. })