|
| 1 | +import type { DomHandlerData } from '../../../src/coreHandlers/handleDom'; |
| 2 | +import { handleDom } from '../../../src/coreHandlers/handleDom'; |
| 3 | + |
| 4 | +describe('Unit | coreHandlers | handleDom', () => { |
| 5 | + test('it works with a basic click event on a div', () => { |
| 6 | + const parent = document.createElement('body'); |
| 7 | + const target = document.createElement('div'); |
| 8 | + target.classList.add('my-class', 'other-class'); |
| 9 | + parent.appendChild(target); |
| 10 | + |
| 11 | + const handlerData: DomHandlerData = { |
| 12 | + name: 'click', |
| 13 | + event: { |
| 14 | + target, |
| 15 | + }, |
| 16 | + }; |
| 17 | + const actual = handleDom(handlerData); |
| 18 | + expect(actual).toEqual({ |
| 19 | + category: 'ui.click', |
| 20 | + data: {}, |
| 21 | + message: 'body > div.my-class.other-class', |
| 22 | + timestamp: expect.any(Number), |
| 23 | + type: 'default', |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + test('it works with a basic click event on a button', () => { |
| 28 | + const parent = document.createElement('body'); |
| 29 | + const target = document.createElement('button'); |
| 30 | + target.classList.add('my-class', 'other-class'); |
| 31 | + parent.appendChild(target); |
| 32 | + |
| 33 | + const handlerData: DomHandlerData = { |
| 34 | + name: 'click', |
| 35 | + event: { |
| 36 | + target, |
| 37 | + }, |
| 38 | + }; |
| 39 | + const actual = handleDom(handlerData); |
| 40 | + expect(actual).toEqual({ |
| 41 | + category: 'ui.click', |
| 42 | + data: {}, |
| 43 | + message: 'body > button.my-class.other-class', |
| 44 | + timestamp: expect.any(Number), |
| 45 | + type: 'default', |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + test('it works with a basic click event on a span inside of <button>', () => { |
| 50 | + const parent = document.createElement('body'); |
| 51 | + const interactive = document.createElement('button'); |
| 52 | + interactive.classList.add('my-class', 'other-class'); |
| 53 | + parent.appendChild(interactive); |
| 54 | + |
| 55 | + const target = document.createElement('span'); |
| 56 | + interactive.appendChild(target); |
| 57 | + |
| 58 | + const handlerData: DomHandlerData = { |
| 59 | + name: 'click', |
| 60 | + event: { |
| 61 | + target, |
| 62 | + }, |
| 63 | + }; |
| 64 | + const actual = handleDom(handlerData); |
| 65 | + expect(actual).toEqual({ |
| 66 | + category: 'ui.click', |
| 67 | + data: {}, |
| 68 | + message: 'body > button.my-class.other-class', |
| 69 | + timestamp: expect.any(Number), |
| 70 | + type: 'default', |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + test('it works with a basic click event on a span inside of <a>', () => { |
| 75 | + const parent = document.createElement('body'); |
| 76 | + const interactive = document.createElement('a'); |
| 77 | + interactive.classList.add('my-class', 'other-class'); |
| 78 | + parent.appendChild(interactive); |
| 79 | + |
| 80 | + const target = document.createElement('span'); |
| 81 | + interactive.appendChild(target); |
| 82 | + |
| 83 | + const handlerData: DomHandlerData = { |
| 84 | + name: 'click', |
| 85 | + event: { |
| 86 | + target, |
| 87 | + }, |
| 88 | + }; |
| 89 | + const actual = handleDom(handlerData); |
| 90 | + expect(actual).toEqual({ |
| 91 | + category: 'ui.click', |
| 92 | + data: {}, |
| 93 | + message: 'body > a.my-class.other-class', |
| 94 | + timestamp: expect.any(Number), |
| 95 | + type: 'default', |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + test('it works with very deep nesting', () => { |
| 100 | + const parent = document.createElement('body'); |
| 101 | + |
| 102 | + let current: HTMLElement = parent; |
| 103 | + for (let i = 0; i < 20; i++) { |
| 104 | + const next = document.createElement('div'); |
| 105 | + next.classList.add(`level-${i}`, `level-other-${i}`); |
| 106 | + current.appendChild(next); |
| 107 | + current = next; |
| 108 | + } |
| 109 | + |
| 110 | + const target = document.createElement('div'); |
| 111 | + target.classList.add('my-class', 'other-class'); |
| 112 | + current.appendChild(target); |
| 113 | + |
| 114 | + const handlerData: DomHandlerData = { |
| 115 | + name: 'click', |
| 116 | + event: { |
| 117 | + target, |
| 118 | + }, |
| 119 | + }; |
| 120 | + const actual = handleDom(handlerData); |
| 121 | + expect(actual).toEqual({ |
| 122 | + category: 'ui.click', |
| 123 | + data: {}, |
| 124 | + message: |
| 125 | + 'div.level-16.level-other-16 > div.level-17.level-other-17 > div.level-18.level-other-18 > div.level-19.level-other-19 > div.my-class.other-class', |
| 126 | + timestamp: expect.any(Number), |
| 127 | + type: 'default', |
| 128 | + }); |
| 129 | + }); |
| 130 | +}); |
0 commit comments