2015-12-31 21:04:38 +03:00
|
|
|
module('Dropdown - attachBody - positioning');
|
|
|
|
|
|
|
|
test('appends to the dropdown parent', function (assert) {
|
2016-05-24 06:38:45 +03:00
|
|
|
assert.expect(4);
|
2015-12-31 21:04:38 +03:00
|
|
|
|
|
|
|
var $ = require('jquery');
|
|
|
|
|
|
|
|
var $select = $('<select></select>');
|
|
|
|
var $parent = $('<div></div>');
|
|
|
|
|
|
|
|
var $container = $('<span></span>');
|
|
|
|
var container = new MockContainer();
|
|
|
|
|
|
|
|
$parent.appendTo($('#qunit-fixture'));
|
|
|
|
$select.appendTo($parent);
|
|
|
|
|
|
|
|
var Utils = require('select2/utils');
|
|
|
|
var Options = require('select2/options');
|
|
|
|
|
|
|
|
var Dropdown = require('select2/dropdown');
|
|
|
|
var AttachBody = require('select2/dropdown/attachBody');
|
|
|
|
|
|
|
|
var DropdownAdapter = Utils.Decorate(Dropdown, AttachBody);
|
|
|
|
|
|
|
|
var dropdown = new DropdownAdapter($select, new Options({
|
|
|
|
dropdownParent: $parent
|
|
|
|
}));
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
$parent.children().length,
|
|
|
|
1,
|
|
|
|
'Only the select should be in the container'
|
|
|
|
);
|
|
|
|
|
|
|
|
var $dropdown = dropdown.render();
|
|
|
|
|
|
|
|
dropdown.bind(container, $container);
|
|
|
|
|
|
|
|
dropdown.position($dropdown, $container);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
$parent.children().length,
|
|
|
|
1,
|
|
|
|
'The dropdown should not be placed until after it is opened'
|
|
|
|
);
|
|
|
|
|
|
|
|
dropdown._showDropdown();
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
$parent.children().length,
|
|
|
|
2,
|
|
|
|
'The dropdown should now be in the container as well'
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
$.contains($parent[0], $dropdown[0]),
|
|
|
|
'The dropdown should be contained within the parent container'
|
|
|
|
);
|
2016-01-02 00:47:49 +03:00
|
|
|
});
|
|
|
|
|
2016-04-18 00:15:44 +03:00
|
|
|
test('dropdown is positioned down with static margins', function (assert) {
|
2016-01-02 00:47:49 +03:00
|
|
|
var $ = require('jquery');
|
|
|
|
var $select = $('<select></select>');
|
2016-01-02 01:09:43 +03:00
|
|
|
var $parent = $('<div></div>');
|
|
|
|
$parent.css({
|
|
|
|
position: 'static',
|
|
|
|
marginTop: '5px',
|
|
|
|
marginLeft: '10px'
|
|
|
|
});
|
2016-01-02 00:47:49 +03:00
|
|
|
|
2016-01-04 01:46:01 +03:00
|
|
|
var $container = $('<span>test</span>');
|
2016-01-02 00:47:49 +03:00
|
|
|
var container = new MockContainer();
|
|
|
|
|
|
|
|
$('#qunit-fixture').empty();
|
|
|
|
|
|
|
|
$parent.appendTo($('#qunit-fixture'));
|
|
|
|
$container.appendTo($parent);
|
|
|
|
|
|
|
|
var Utils = require('select2/utils');
|
|
|
|
var Options = require('select2/options');
|
|
|
|
|
|
|
|
var Dropdown = require('select2/dropdown');
|
|
|
|
var AttachBody = require('select2/dropdown/attachBody');
|
|
|
|
|
|
|
|
var DropdownAdapter = Utils.Decorate(Dropdown, AttachBody);
|
|
|
|
|
|
|
|
var dropdown = new DropdownAdapter($select, new Options({
|
|
|
|
dropdownParent: $parent
|
|
|
|
}));
|
|
|
|
|
|
|
|
var $dropdown = dropdown.render();
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
$dropdown[0].style.top,
|
|
|
|
0,
|
|
|
|
'The drodpown should not have any offset before it is displayed'
|
|
|
|
);
|
|
|
|
|
|
|
|
dropdown.bind(container, $container);
|
|
|
|
dropdown.position($dropdown, $container);
|
|
|
|
dropdown._showDropdown();
|
|
|
|
|
2016-04-18 00:15:44 +03:00
|
|
|
assert.ok(
|
|
|
|
dropdown.$dropdown.hasClass('select2-dropdown--below'),
|
|
|
|
'The dropdown should be forced down'
|
|
|
|
);
|
|
|
|
|
2016-01-02 00:47:49 +03:00
|
|
|
assert.equal(
|
2019-06-26 05:05:15 +03:00
|
|
|
$dropdown.css('top').replace(/\D+/, ''),
|
2016-01-04 01:46:01 +03:00
|
|
|
$container.outerHeight() + 5,
|
2016-01-02 00:47:49 +03:00
|
|
|
'The offset should be 5px at the top'
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
$dropdown.css('left'),
|
|
|
|
'10px',
|
|
|
|
'The offset should be 10px on the left'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2016-04-18 00:15:44 +03:00
|
|
|
test('dropdown is positioned down with absolute offsets', function (assert) {
|
2016-01-02 00:47:49 +03:00
|
|
|
var $ = require('jquery');
|
|
|
|
var $select = $('<select></select>');
|
2016-01-02 01:09:43 +03:00
|
|
|
var $parent = $('<div></div>');
|
|
|
|
$parent.css({
|
|
|
|
position: 'absolute',
|
|
|
|
top: '10px',
|
|
|
|
left: '5px'
|
|
|
|
});
|
2016-01-02 00:47:49 +03:00
|
|
|
|
2016-01-04 01:46:01 +03:00
|
|
|
var $container = $('<span>test</span>');
|
2016-01-02 00:47:49 +03:00
|
|
|
var container = new MockContainer();
|
|
|
|
|
|
|
|
$parent.appendTo($('#qunit-fixture'));
|
|
|
|
$container.appendTo($parent);
|
|
|
|
|
|
|
|
var Utils = require('select2/utils');
|
|
|
|
var Options = require('select2/options');
|
|
|
|
|
|
|
|
var Dropdown = require('select2/dropdown');
|
|
|
|
var AttachBody = require('select2/dropdown/attachBody');
|
|
|
|
|
|
|
|
var DropdownAdapter = Utils.Decorate(Dropdown, AttachBody);
|
|
|
|
|
|
|
|
var dropdown = new DropdownAdapter($select, new Options({
|
|
|
|
dropdownParent: $parent
|
|
|
|
}));
|
|
|
|
|
|
|
|
var $dropdown = dropdown.render();
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
$dropdown[0].style.top,
|
|
|
|
0,
|
|
|
|
'The drodpown should not have any offset before it is displayed'
|
|
|
|
);
|
|
|
|
|
|
|
|
dropdown.bind(container, $container);
|
|
|
|
dropdown.position($dropdown, $container);
|
|
|
|
dropdown._showDropdown();
|
|
|
|
|
2016-04-18 00:15:44 +03:00
|
|
|
assert.ok(
|
|
|
|
dropdown.$dropdown.hasClass('select2-dropdown--below'),
|
|
|
|
'The dropdown should be forced down'
|
|
|
|
);
|
|
|
|
|
2016-01-02 00:47:49 +03:00
|
|
|
assert.equal(
|
2019-06-26 05:05:15 +03:00
|
|
|
$dropdown.css('top').replace(/\D+/, ''),
|
2016-01-04 01:46:01 +03:00
|
|
|
$container.outerHeight(),
|
2016-01-02 00:47:49 +03:00
|
|
|
'There should not be an extra top offset'
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
$dropdown.css('left'),
|
|
|
|
'0px',
|
|
|
|
'There should not be an extra left offset'
|
|
|
|
);
|
2015-12-31 21:04:38 +03:00
|
|
|
});
|