dom.test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import {
  2. find,
  3. child,
  4. create,
  5. remove,
  6. domify,
  7. applyStyle,
  8. addClass,
  9. removeClass,
  10. hasClass,
  11. setAttribute,
  12. removeAttribute,
  13. } from '../../src/js/utils/dom';
  14. describe( 'DOM function ', () => {
  15. beforeEach( () => {
  16. document.body.innerHTML = `
  17. <ul class="root">
  18. <li class="child">First</li>
  19. <li class="child slide">Second</li>
  20. <li class="child">Third</li>
  21. </ul>
  22. `;
  23. } );
  24. test( '"find" should find the first element from the given parent with a selector.', () => {
  25. const elm = find( document, '.root' );
  26. expect( elm.tagName.toLowerCase() ).toBe( 'ul' );
  27. } );
  28. test( '"child" should find the first child element having the given class from the parent.', () => {
  29. const found = child( document.querySelector( '.root' ), 'slide' );
  30. expect( found.innerHTML ).toBe( 'Second' );
  31. } );
  32. test( '"create" should generate an element, applying some attributes if necessary.', () => {
  33. const elm = create( 'button', { class: 'btn' } );
  34. expect( elm.tagName.toLowerCase() ).toBe( 'button' );
  35. expect( elm.classList.contains( 'btn' ) ).toBe( true );
  36. } );
  37. test( '"remove" should remove the given element.', () => {
  38. const root = document.querySelector( '.root' );
  39. const slide = child( root, 'slide' );
  40. expect( root.children.length ).toBe( 3 );
  41. remove( slide );
  42. expect( root.children.length ).toBe( 2 );
  43. } );
  44. test( '"domify" should convert HTML string to elements.', () => {
  45. const root = document.querySelector( '.root' );
  46. const li = domify( '<li class="child">Forth</li>' );
  47. root.appendChild( li );
  48. const items = root.getElementsByTagName( 'li' );
  49. expect( items[ items.length - 1 ].textContent ).toBe( 'Forth' );
  50. } );
  51. test( '"applyStyle" should apply a style or styles to an element.', () => {
  52. const root = document.querySelector( '.root' );
  53. applyStyle( root, { width: '200px', height: '100px' } );
  54. expect( root.style.width ).toBe( '200px' );
  55. expect( root.style.height ).toBe( '100px' );
  56. } );
  57. test( '"addClass" should add a class or classes to an element.', () => {
  58. const root = document.querySelector( '.root' );
  59. addClass( root, [ 'class1', 'class2' ] );
  60. expect( root.classList.contains( 'class1' ) ).toBe( true );
  61. expect( root.classList.contains( 'class2' ) ).toBe( true );
  62. } );
  63. test( '"removeClass" should remove a class or classes from an element.', () => {
  64. const root = document.querySelector( '.root' );
  65. const classList = root.classList;
  66. classList.add( 'class3' );
  67. classList.add( 'class4' );
  68. removeClass( root, 'class3' );
  69. expect( classList.contains( 'class3' ) ).toBe( false );
  70. } );
  71. test( '"hasClass" should verify if an element has a class or not.', () => {
  72. expect( hasClass( document.querySelector( '.root' ), 'root' ) ).toBe( true );
  73. } );
  74. test( '"setAttribute" should add an attribute to an element.', () => {
  75. const root = document.querySelector( '.root' );
  76. setAttribute( root, 'data-root', 'a' );
  77. expect( root.dataset.root ).toBe( 'a' );
  78. } );
  79. test( '"removeAttribute" should remove an attribute from an element.', () => {
  80. const root = document.querySelector( '.root' );
  81. root.dataset.a = 'a';
  82. root.dataset.b = 'b';
  83. removeAttribute( root, [ 'data-a', 'data-b' ] );
  84. expect( root.dataset.a ).toBeUndefined();
  85. expect( root.dataset.b ).toBeUndefined();
  86. } );
  87. } );