Skip to content

Commit 3196aa0

Browse files
committed
Datepicker: Added test to ensure proper positioning of datepicker
1 parent 6a2872c commit 3196aa0

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

tests/unit/datepicker/core.js

+79
Original file line numberDiff line numberDiff line change
@@ -540,4 +540,83 @@ QUnit.test( "mouse", function( assert ) {
540540
"Mouse click inline - next" );
541541
} );
542542

543+
QUnit.test( "position", function( assert ) {
544+
assert.expect( 4 );
545+
546+
var $container = $( "#qunit-fixture" ),
547+
$modal = $container.children('div'),
548+
$inputOne = testHelper.init( "#inp" ),
549+
$inputTwo = testHelper.init( "#alt" );
550+
551+
// Undo the weird positioning on the container
552+
$container.css({
553+
position: "static",
554+
top: 0,
555+
left: 0,
556+
height: "5000px",
557+
});
558+
559+
// Position the modal in the middle of the screen
560+
$modal.css({
561+
display: "none", // Initially must be hidden
562+
position: "fixed",
563+
"overflow": "hidden auto",
564+
top: "50%",
565+
left: "50%",
566+
right: "auto",
567+
width: "500px",
568+
height: "500px",
569+
marginTop: "-100px",
570+
marginLeft: "-100px"
571+
});
572+
573+
// Add an internal wrapper around all the children nodes
574+
$modal.wrapInner(`<div id="relative-wrapper" style="position: relative"></div>`)
575+
var $wrapper = $modal.children('#relative-wrapper');
576+
$wrapper.prepend(`<div style="height: 300px;"></div>`);
577+
578+
// Wrap inputs with a container
579+
$inputOne.wrap(`<div id="input-one-container" style="height: 200px;"></div>`);
580+
$inputTwo.wrap(`<div id="input-two-container" style="height: 200px;"></div>`);
581+
582+
assert.ok( $modal.is(":hidden"), "Modal is hidden" );
583+
584+
// Set the page scroll position way down the page
585+
var done = assert.async();
586+
$(document).ready(function() {
587+
$(window).scrollTop(2000);
588+
589+
// Disable the scrollbar
590+
$('body').css('overflow', 'hidden');
591+
592+
// Now show the wrapper with the input
593+
$modal.show();
594+
595+
// Initialize datepickers
596+
$modal.find('input').datepicker();
597+
598+
setTimeout(() => {
599+
$("#input-one-container").hide();
600+
$("#input-two-container").css("padding-top", "100px");
601+
602+
var $inputTwo = $("#input-two-container").find("input");
603+
604+
$inputTwo.focus();
605+
assert.ok( $("#ui-datepicker-div").is(":visible"), "Datepicker is visible" );
606+
done();
607+
608+
// Get the top position of the input and compare it to the datepicker to ensure they're
609+
// both positioned correctly
610+
var inputTop = $inputTwo.offset().top;
611+
var dpTop = $("#ui-datepicker-div").offset().top;
612+
assert.ok( Math.abs(inputTop - dpTop) < 25, "Datepicker is positioned correctly" );
613+
614+
// It should also be left aligned with the input
615+
var inputLeft = $inputTwo.offset().left;
616+
var dpLeft = $("#ui-datepicker-div").offset().left;
617+
assert.ok( Math.abs(inputLeft - dpLeft) < 5, "Datepicker is left aligned with the input" );
618+
}, 200);
619+
});
620+
} );
621+
543622
} );

0 commit comments

Comments
 (0)