positioning-tests.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 style="position: static; margin-top: 5px; margin-left: 10px;"></div>');
  47. var $container = $('<span></span>');
  48. var container = new MockContainer();
  49. $('#qunit-fixture').empty();
  50. $parent.appendTo($('#qunit-fixture'));
  51. $container.appendTo($parent);
  52. var Utils = require('select2/utils');
  53. var Options = require('select2/options');
  54. var Dropdown = require('select2/dropdown');
  55. var AttachBody = require('select2/dropdown/attachBody');
  56. var DropdownAdapter = Utils.Decorate(Dropdown, AttachBody);
  57. var dropdown = new DropdownAdapter($select, new Options({
  58. dropdownParent: $parent
  59. }));
  60. var $dropdown = dropdown.render();
  61. assert.equal(
  62. $dropdown[0].style.top,
  63. 0,
  64. 'The drodpown should not have any offset before it is displayed'
  65. );
  66. dropdown.bind(container, $container);
  67. dropdown.position($dropdown, $container);
  68. dropdown._showDropdown();
  69. assert.equal(
  70. $dropdown.css('top'),
  71. '5px',
  72. 'The offset should be 5px at the top'
  73. );
  74. assert.equal(
  75. $dropdown.css('left'),
  76. '10px',
  77. 'The offset should be 10px on the left'
  78. );
  79. });
  80. test('dropdown is positioned with absolute offsets', function (assert) {
  81. var $ = require('jquery');
  82. var $select = $('<select></select>');
  83. var $parent = $('<div style="position: absolute; top: 10px; left: 5px;"></div>');
  84. var $container = $('<span></span>');
  85. var container = new MockContainer();
  86. $parent.appendTo($('#qunit-fixture'));
  87. $container.appendTo($parent);
  88. var Utils = require('select2/utils');
  89. var Options = require('select2/options');
  90. var Dropdown = require('select2/dropdown');
  91. var AttachBody = require('select2/dropdown/attachBody');
  92. var DropdownAdapter = Utils.Decorate(Dropdown, AttachBody);
  93. var dropdown = new DropdownAdapter($select, new Options({
  94. dropdownParent: $parent
  95. }));
  96. var $dropdown = dropdown.render();
  97. assert.equal(
  98. $dropdown[0].style.top,
  99. 0,
  100. 'The drodpown should not have any offset before it is displayed'
  101. );
  102. dropdown.bind(container, $container);
  103. dropdown.position($dropdown, $container);
  104. dropdown._showDropdown();
  105. assert.equal(
  106. $dropdown.css('top'),
  107. '0px',
  108. 'There should not be an extra top offset'
  109. );
  110. assert.equal(
  111. $dropdown.css('left'),
  112. '0px',
  113. 'There should not be an extra left offset'
  114. );
  115. });