dom.test.js 3.2 KB

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