]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
Add test to make sure we enforce focus on modal (#27723)
authorJohann-S <johann.servoire@gmail.com>
Sun, 25 Nov 2018 16:20:55 +0000 (17:20 +0100)
committerXhmikosR <xhmikosr@gmail.com>
Sun, 25 Nov 2018 16:20:55 +0000 (18:20 +0200)
js/tests/unit/modal.js

index 1156ce0c7009b13698d2b292fe1063fcb7230096..8e67d83a046fc9bf349692b085cba256b0640f7f 100644 (file)
@@ -735,4 +735,45 @@ $(function () {
       done()
     }).bootstrapModal('show')
   })
+
+  QUnit.test('should enforce focus', function (assert) {
+    assert.expect(4)
+    var done = assert.async()
+
+    var $modal = $([
+      '<div id="modal-test" data-show="false">',
+      '  <div class="modal-dialog">',
+      '    <div class="modal-content">',
+      '      <div class="modal-body" />',
+      '    </div>',
+      '  </div>',
+      '</div>'
+    ].join(''))
+      .bootstrapModal()
+      .appendTo('#qunit-fixture')
+
+    var modal = $modal.data('bs.modal')
+    var spy = sinon.spy(modal, '_enforceFocus')
+    var spyDocOff = sinon.spy($(document), 'off')
+    var spyDocOn = sinon.spy($(document), 'on')
+
+    $modal.one('shown.bs.modal', function () {
+      assert.ok(spy.called, '_enforceFocus called')
+      assert.ok(spyDocOff.withArgs('focusin.bs.modal'))
+      assert.ok(spyDocOn.withArgs('focusin.bs.modal'))
+
+      var spyFocus = sinon.spy(modal._element, 'focus')
+      var event = $.Event('focusin', {
+        target: $('#qunit-fixture')[0]
+      })
+
+      $(document).one('focusin', function () {
+        assert.ok(spyFocus.called)
+        done()
+      })
+
+      $(document).trigger(event)
+    })
+      .bootstrapModal('show')
+  })
 })