positioning-tests.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. module('Dropdown - attachBody - positioning');
  2. test('appends to the dropdown parent', function (assert) {
  3. 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 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.equal(
  75. $dropdown.css('top').substring(0, 2),
  76. $container.outerHeight() + 5,
  77. 'The offset should be 5px at the top'
  78. );
  79. assert.equal(
  80. $dropdown.css('left'),
  81. '10px',
  82. 'The offset should be 10px on the left'
  83. );
  84. });
  85. test('dropdown is positioned with absolute offsets', function (assert) {
  86. var $ = require('jquery');
  87. var $select = $('<select></select>');
  88. var $parent = $('<div></div>');
  89. $parent.css({
  90. position: 'absolute',
  91. top: '10px',
  92. left: '5px'
  93. });
  94. var $container = $('<span>test</span>');
  95. var container = new MockContainer();
  96. $parent.appendTo($('#qunit-fixture'));
  97. $container.appendTo($parent);
  98. var Utils = require('select2/utils');
  99. var Options = require('select2/options');
  100. var Dropdown = require('select2/dropdown');
  101. var AttachBody = require('select2/dropdown/attachBody');
  102. var DropdownAdapter = Utils.Decorate(Dropdown, AttachBody);
  103. var dropdown = new DropdownAdapter($select, new Options({
  104. dropdownParent: $parent
  105. }));
  106. var $dropdown = dropdown.render();
  107. assert.equal(
  108. $dropdown[0].style.top,
  109. 0,
  110. 'The drodpown should not have any offset before it is displayed'
  111. );
  112. dropdown.bind(container, $container);
  113. dropdown.position($dropdown, $container);
  114. dropdown._showDropdown();
  115. assert.equal(
  116. $dropdown.css('top').substring(0, 2),
  117. $container.outerHeight(),
  118. 'There should not be an extra top offset'
  119. );
  120. assert.equal(
  121. $dropdown.css('left'),
  122. '0px',
  123. 'There should not be an extra left offset'
  124. );
  125. });