Skip to content

Commit 32ab52b

Browse files
Johann-SXhmikosR
authored andcommitted
Add test to make sure we enforce focus on modal (#27723)
1 parent 72bd3f5 commit 32ab52b

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

js/tests/unit/modal.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,4 +735,45 @@ $(function () {
735735
done()
736736
}).bootstrapModal('show')
737737
})
738+
739+
QUnit.test('should enforce focus', function (assert) {
740+
assert.expect(4)
741+
var done = assert.async()
742+
743+
var $modal = $([
744+
'<div id="modal-test" data-show="false">',
745+
' <div class="modal-dialog">',
746+
' <div class="modal-content">',
747+
' <div class="modal-body" />',
748+
' </div>',
749+
' </div>',
750+
'</div>'
751+
].join(''))
752+
.bootstrapModal()
753+
.appendTo('#qunit-fixture')
754+
755+
var modal = $modal.data('bs.modal')
756+
var spy = sinon.spy(modal, '_enforceFocus')
757+
var spyDocOff = sinon.spy($(document), 'off')
758+
var spyDocOn = sinon.spy($(document), 'on')
759+
760+
$modal.one('shown.bs.modal', function () {
761+
assert.ok(spy.called, '_enforceFocus called')
762+
assert.ok(spyDocOff.withArgs('focusin.bs.modal'))
763+
assert.ok(spyDocOn.withArgs('focusin.bs.modal'))
764+
765+
var spyFocus = sinon.spy(modal._element, 'focus')
766+
var event = $.Event('focusin', {
767+
target: $('#qunit-fixture')[0]
768+
})
769+
770+
$(document).one('focusin', function () {
771+
assert.ok(spyFocus.called)
772+
done()
773+
})
774+
775+
$(document).trigger(event)
776+
})
777+
.bootstrapModal('show')
778+
})
738779
})

0 commit comments

Comments
 (0)