]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
test(Modal): check if modal is disposed
authorAndrew Luca <thendrluca@gmail.com>
Thu, 18 Oct 2018 22:26:11 +0000 (01:26 +0300)
committerJohann-S <johann.servoire@gmail.com>
Tue, 30 Oct 2018 15:28:32 +0000 (16:28 +0100)
js/src/modal.js
js/tests/unit/modal.js

index 704b022472ec066ffb45cc331b21105189228c72..0004fe8bbefc38d596f2a09ec38b2259bdb58088 100644 (file)
@@ -196,9 +196,17 @@ class Modal {
   }
 
   dispose() {
-    $.removeData(this._element, DATA_KEY)
+    [window, this._element, this._dialog]
+      .forEach((htmlElement) => $(htmlElement).off(EVENT_KEY))
+
+    /**
+     * `document` has 2 events `Event.FOCUSIN` and `Event.CLICK_DATA_API`
+     * Do not move `document` in `htmlElements` array
+     * It will remove `Event.CLICK_DATA_API` event that should remain
+     */
+    $(document).off(Event.FOCUSIN)
 
-    $(window, document, this._element, this._backdrop).off(EVENT_KEY)
+    $.removeData(this._element, DATA_KEY)
 
     this._config              = null
     this._element             = null
index 7c8299109e75b6773ff5c07a5fb67a2c063f048b..4c857105b8384aeeb96e63a466e5e55d46549b4e 100644 (file)
@@ -697,4 +697,37 @@ $(function () {
 
     beginTimestamp = Date.now()
   })
+
+  QUnit.test('should dispose modal', function (assert) {
+    assert.expect(3)
+    var done = assert.async()
+
+    var $modal = $([
+      '<div id="modal-test">',
+      '  <div class="modal-dialog">',
+      '    <div class="modal-content">',
+      '      <div class="modal-body" />',
+      '    </div>',
+      '  </div>',
+      '</div>'
+    ].join('')).appendTo('#qunit-fixture')
+
+    $modal.on('shown.bs.modal', function () {
+      var spy = sinon.spy($.fn, 'off')
+
+      $(this).bootstrapModal('dispose')
+
+      const modalDataApiEvent = $._data(document, 'events').click
+        .find((e) => e.namespace === 'bs.data-api.modal')
+
+      assert.ok(typeof $(this).data('bs.modal') === 'undefined', 'modal data object was disposed')
+
+      assert.ok(spy.callCount === 4, '`jQuery.off` was called')
+
+      assert.ok(typeof modalDataApiEvent !== 'undefined', '`Event.CLICK_DATA_API` on `document` was not removed')
+
+      $.fn.off.restore()
+      done()
+    }).bootstrapModal('show')
+  })
 })