positioning-tests.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. module('Dropdown - attachBody - positioning');
  2. test('appends to the dropdown parent', function (assert) {
  3. assert.expect(4);
  4. var $ = require('jquery');
  5. var $select = $('<select></select>');
  6. var $parent = $('<div></div>');
  7. var $container = $('<span></span>');
  8. var container = new MockContainer();
  9. $parent.appendTo($('#qunit-fixture'));
  10. $select.appendTo($parent);
  11. var Utils = require('select2/utils');
  12. var Options = require('select2/options');
  13. var Dropdown = require('select2/dropdown');
  14. var AttachBody = require('select2/dropdown/attachBody');
  15. var DropdownAdapter = Utils.Decorate(Dropdown, AttachBody);
  16. var dropdown = new DropdownAdapter($select, new Options({
  17. dropdownParent: $parent
  18. }));
  19. assert.equal(
  20. $parent.children().length,
  21. 1,
  22. 'Only the select should be in the container'
  23. );
  24. var $dropdown = dropdown.render();
  25. dropdown.bind(container, $container);
  26. dropdown.position($dropdown, $container);
  27. assert.equal(
  28. $parent.children().length,
  29. 1,
  30. 'The dropdown should not be placed until after it is opened'
  31. );
  32. dropdown._showDropdown();
  33. assert.equal(
  34. $parent.children().length,
  35. 2,
  36. 'The dropdown should now be in the container as well'
  37. );
  38. assert.ok(
  39. $.contains($parent[0], $dropdown[0]),
  40. 'The dropdown should be contained within the parent container'
  41. );
  42. });
  43. test('dropdown is positioned down with static margins', function (assert) {
  44. var $ = require('jquery');
  45. var $select = $('<select></select>');
  46. var $parent = $('<div></div>');
  47. $parent.css({
  48. position: 'static',
  49. marginTop: '5px',
  50. marginLeft: '10px'
  51. });
  52. var $container = $('<span>test</span>');
  53. var container = new MockContainer();
  54. $('#qunit-fixture').empty();
  55. $parent.appendTo($('#qunit-fixture'));
  56. $container.appendTo($parent);
  57. var Utils = require('select2/utils');
  58. var Options = require('select2/options');
  59. var Dropdown = require('select2/dropdown');
  60. var AttachBody = require('select2/dropdown/attachBody');
  61. var DropdownAdapter = Utils.Decorate(Dropdown, AttachBody);
  62. var dropdown = new DropdownAdapter($select, new Options({
  63. dropdownParent: $parent
  64. }));
  65. var $dropdown = dropdown.render();
  66. assert.equal(
  67. $dropdown[0].style.top,
  68. 0,
  69. 'The drodpown should not have any offset before it is displayed'
  70. );
  71. dropdown.bind(container, $container);
  72. dropdown.position($dropdown, $container);
  73. dropdown._showDropdown();
  74. assert.ok(
  75. dropdown.$dropdown.hasClass('select2-dropdown--below'),
  76. 'The dropdown should be forced down'
  77. );
  78. assert.equal(
  79. $dropdown.css('top').substring(0, 2),
  80. $container.outerHeight() + 5,
  81. 'The offset should be 5px at the top'
  82. );
  83. assert.equal(
  84. $dropdown.css('left'),
  85. '10px',
  86. 'The offset should be 10px on the left'
  87. );
  88. });
  89. test('dropdown is positioned down with absolute offsets', function (assert) {
  90. var $ = require('jquery');
  91. var $select = $('<select></select>');
  92. var $parent = $('<div></div>');
  93. $parent.css({
  94. position: 'absolute',
  95. top: '10px',
  96. left: '5px'
  97. });
  98. var $container = $('<span>test</span>');
  99. var container = new MockContainer();
  100. $parent.appendTo($('#qunit-fixture'));
  101. $container.appendTo($parent);
  102. var Utils = require('select2/utils');
  103. var Options = require('select2/options');
  104. var Dropdown = require('select2/dropdown');
  105. var AttachBody = require('select2/dropdown/attachBody');
  106. var DropdownAdapter = Utils.Decorate(Dropdown, AttachBody);
  107. var dropdown = new DropdownAdapter($select, new Options({
  108. dropdownParent: $parent
  109. }));
  110. var $dropdown = dropdown.render();
  111. assert.equal(
  112. $dropdown[0].style.top,
  113. 0,
  114. 'The drodpown should not have any offset before it is displayed'
  115. );
  116. dropdown.bind(container, $container);
  117. dropdown.position($dropdown, $container);
  118. dropdown._showDropdown();
  119. assert.ok(
  120. dropdown.$dropdown.hasClass('select2-dropdown--below'),
  121. 'The dropdown should be forced down'
  122. );
  123. assert.equal(
  124. $dropdown.css('top').substring(0, 2),
  125. $container.outerHeight(),
  126. 'There should not be an extra top offset'
  127. );
  128. assert.equal(
  129. $dropdown.css('left'),
  130. '0px',
  131. 'There should not be an extra left offset'
  132. );
  133. });