dom.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import '../_setup.js';
  2. import {
  3. parseHTML,
  4. isVisible,
  5. hideElement,
  6. showElement,
  7. emptyChildNodes,
  8. replaceChildNodes,
  9. } from '../../../js/lib/dom.js';
  10. describe('lib/dom', function () {
  11. let el;
  12. before(function () {
  13. testContainer.innerHTML = '<div class="test"></div>';
  14. el = testContainer.firstChild;
  15. });
  16. after(function () {
  17. testContainer.removeChild(el);
  18. });
  19. describe('parseHTML()', function () {
  20. it('parses an html fragment with Range.prototype.createContextualFragment()', function () {
  21. const spyFragment = sinon.spy(Range.prototype, 'createContextualFragment');
  22. const html = '<div>test</div>';
  23. const result = parseHTML(html);
  24. expect(spyFragment.calledWith(html), 'to be true');
  25. expect(spyFragment.returned(result), 'to be true');
  26. spyFragment.restore();
  27. });
  28. });
  29. describe('isVisible()', function () {
  30. it('returns true if either offsetHeight > 0, offsetWidth > 0 or getClientRects() has an item', function () {
  31. expect(isVisible(el), 'to be false');
  32. let stub = sinon.stub(el, "offsetHeight").get(() => 10);
  33. expect(isVisible(el), 'to be true');
  34. stub.restore();
  35. stub = sinon.stub(el, "offsetWidth").get(() => 10);
  36. expect(isVisible(el), 'to be true');
  37. stub.restore();
  38. stub = sinon.stub(el, 'getClientRects').callsFake(() => [{x: 10}, {x: 20}]);
  39. expect(isVisible(el), 'to be true');
  40. stub.restore();
  41. });
  42. });
  43. describe('hideElement()', function () {
  44. it('sets none to style.display', function () {
  45. hideElement(el);
  46. expect(el.style.display, 'to be', 'none');
  47. el.removeAttribute('style');
  48. });
  49. it('copies style.display to data-style-display attribute if other than none is set', function () {
  50. el.style.display = 'flex';
  51. hideElement(el);
  52. expect(el.style.display, 'to be', 'none');
  53. expect(el.getAttribute('data-style-display'), 'to be', 'flex');
  54. el.removeAttribute('data-style-display');
  55. el.style.display = 'none';
  56. hideElement(el);
  57. expect(el.hasAttribute('data-style-display'), 'to be false');
  58. el.removeAttribute('style');
  59. });
  60. });
  61. describe('showElement()', function () {
  62. it('clears style.display if none is set', function () {
  63. el.style.display = 'none';
  64. showElement(el);
  65. expect(el.style.display, 'to be', '');
  66. el.style.display = 'inline';
  67. showElement(el);
  68. expect(el.style.display, 'to be', 'inline');
  69. el.removeAttribute('style');
  70. });
  71. it('restores the value of data-style-display attribute to style.display if it exists', function () {
  72. el.style.display = 'none';
  73. el.setAttribute('data-style-display', 'flex');
  74. showElement(el);
  75. expect(el.style.display, 'to be', 'flex');
  76. expect(el.hasAttribute('data-style-display'), 'to be false');
  77. el.removeAttribute('style');
  78. });
  79. });
  80. describe('emptyChildNodes()', function () {
  81. it('removes all child nodes', function () {
  82. el.innerHTML = '<div>test</div><!-- comment--><div></div>';
  83. emptyChildNodes(el);
  84. expect(el.innerHTML, 'to be empty');
  85. });
  86. });
  87. describe('replaceChildNodes()', function () {
  88. it('replace all child nodes with given nodes', function () {
  89. const htmlBefore = '<div>test</div><!-- comment--><div></div>';
  90. const htmlAfter = '<div id="foo">foo</div><div id="bar">bar</div>';
  91. // with html
  92. el.innerHTML = htmlBefore;
  93. replaceChildNodes(el, htmlAfter);
  94. expect(el.innerHTML, 'to be', htmlAfter);
  95. // with DocumentFragment
  96. const fragment = document.createRange().createContextualFragment(htmlAfter);
  97. el.innerHTML = htmlBefore;
  98. replaceChildNodes(el, fragment);
  99. expect(el.innerHTML, 'to be', htmlAfter);
  100. // with NodeList
  101. const nodeList = el.querySelectorAll('div');
  102. el.innerHTML = htmlBefore;
  103. replaceChildNodes(el, nodeList);
  104. expect(el.innerHTML, 'to be', htmlAfter);
  105. // with array
  106. el.innerHTML = htmlBefore;
  107. replaceChildNodes(el, Array.from(nodeList));
  108. expect(el.innerHTML, 'to be', htmlAfter);
  109. el.innerHTML = '';
  110. });
  111. });
  112. });