|
1 | | -import { describe, expect, it } from 'vitest'; |
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { afterEach, describe, expect, it } from 'vitest'; |
2 | 5 | import { instrumentDOM } from '../../src/instrumentation/dom'; |
3 | 6 | import { WINDOW } from '../../src/types'; |
4 | 7 |
|
5 | 8 | // @ts-expect-error - idk |
6 | 9 | WINDOW.XMLHttpRequest = undefined; |
7 | 10 |
|
8 | | -describe('instrumentXHR', () => { |
9 | | - it('it does not throw if XMLHttpRequest is a key on window but not defined', () => { |
| 11 | +type InstrumentedDocument = Document & { __sentry_instrumentation_handlers__?: Record<string, any> }; |
| 12 | + |
| 13 | +describe('instrumentDOM', () => { |
| 14 | + const { addEventListener: nativeAdd, removeEventListener: nativeRemove } = EventTarget.prototype; |
| 15 | + |
| 16 | + // `instrumentDOM` patches `EventTarget.prototype` and isn't idempotent, so restore the native methods after every test. |
| 17 | + afterEach(() => { |
| 18 | + EventTarget.prototype.addEventListener = nativeAdd; |
| 19 | + EventTarget.prototype.removeEventListener = nativeRemove; |
| 20 | + delete (document as InstrumentedDocument).__sentry_instrumentation_handlers__; |
| 21 | + }); |
| 22 | + |
| 23 | + /** Runs `instrumentDOM` and returns a function counting the click listeners actually attached to `document`. */ |
| 24 | + function instrumentAndTrackDocumentClickListeners(): () => number { |
| 25 | + const documentClickListeners = { capture: new Set<unknown>(), bubble: new Set<unknown>() }; |
| 26 | + |
| 27 | + const phase = (options?: boolean | EventListenerOptions): Set<unknown> => |
| 28 | + (typeof options === 'boolean' ? options : !!options?.capture) |
| 29 | + ? documentClickListeners.capture |
| 30 | + : documentClickListeners.bubble; |
| 31 | + |
| 32 | + // Installed before `instrumentDOM` so these sit underneath the SDK and also see the listeners it attaches itself. |
| 33 | + EventTarget.prototype.addEventListener = function (type, listener, options) { |
| 34 | + if (this === document && type === 'click') { |
| 35 | + phase(options).add(listener); |
| 36 | + } |
| 37 | + return nativeAdd.call(this, type, listener, options); |
| 38 | + }; |
| 39 | + |
| 40 | + EventTarget.prototype.removeEventListener = function (type, listener, options) { |
| 41 | + if (this === document && type === 'click') { |
| 42 | + phase(options).delete(listener); |
| 43 | + } |
| 44 | + return nativeRemove.call(this, type, listener, options); |
| 45 | + }; |
| 46 | + |
| 47 | + instrumentDOM(); |
| 48 | + |
| 49 | + return () => documentClickListeners.capture.size + documentClickListeners.bubble.size; |
| 50 | + } |
| 51 | + |
| 52 | + it('does not throw if XMLHttpRequest is a key on window but not defined', () => { |
10 | 53 | expect(instrumentDOM).not.toThrow(); |
11 | 54 | }); |
| 55 | + |
| 56 | + it('does not leak document click listeners when removeEventListener uses mismatched capture options', () => { |
| 57 | + const countDocumentClickListeners = instrumentAndTrackDocumentClickListeners(); |
| 58 | + |
| 59 | + // baseline listenercount is 1 which comes from the SDK's global click handler registered |
| 60 | + // in instrumentDOM(). |
| 61 | + const baseline = countDocumentClickListeners(); |
| 62 | + |
| 63 | + const never = (): void => {}; |
| 64 | + const onCapture = (): void => {}; |
| 65 | + const onBubble = (): void => {}; |
| 66 | + |
| 67 | + for (let i = 0; i < 20; i++) { |
| 68 | + document.addEventListener('click', onCapture, true); |
| 69 | + document.addEventListener('click', onBubble); |
| 70 | + document.removeEventListener('click', never); |
| 71 | + document.removeEventListener('click', never); |
| 72 | + document.removeEventListener('click', onCapture, true); |
| 73 | + document.removeEventListener('click', onBubble); |
| 74 | + } |
| 75 | + |
| 76 | + expect(countDocumentClickListeners() - baseline).toBe(0); |
| 77 | + }); |
| 78 | + |
| 79 | + it('keeps its handler attached while listeners remain, even after removing listeners that were never added', () => { |
| 80 | + const countDocumentClickListeners = instrumentAndTrackDocumentClickListeners(); |
| 81 | + const baseline = countDocumentClickListeners(); |
| 82 | + |
| 83 | + const never = (): void => {}; |
| 84 | + const onClick = (): void => {}; |
| 85 | + |
| 86 | + document.addEventListener('click', onClick); |
| 87 | + document.removeEventListener('click', never); |
| 88 | + document.removeEventListener('click', onClick, true); |
| 89 | + |
| 90 | + // `onClick` plus the SDK's handler for it |
| 91 | + expect(countDocumentClickListeners() - baseline).toBe(2); |
| 92 | + |
| 93 | + document.removeEventListener('click', onClick); |
| 94 | + |
| 95 | + expect(countDocumentClickListeners() - baseline).toBe(0); |
| 96 | + }); |
| 97 | + |
| 98 | + it('removes its handler when listeners are added and removed with `null` options', () => { |
| 99 | + const countDocumentClickListeners = instrumentAndTrackDocumentClickListeners(); |
| 100 | + const baseline = countDocumentClickListeners(); |
| 101 | + |
| 102 | + const onClick = (): void => {}; |
| 103 | + |
| 104 | + // @ts-expect-error - `null` is valid at runtime and treated like default options |
| 105 | + document.addEventListener('click', onClick, null); |
| 106 | + // @ts-expect-error - see above |
| 107 | + document.removeEventListener('click', onClick, null); |
| 108 | + |
| 109 | + expect(countDocumentClickListeners() - baseline).toBe(0); |
| 110 | + }); |
| 111 | + |
| 112 | + it('does not retain listeners added with `once` or `signal`, which the browser removes on its own', () => { |
| 113 | + instrumentDOM(); |
| 114 | + |
| 115 | + for (let i = 0; i < 20; i++) { |
| 116 | + const controller = new AbortController(); |
| 117 | + document.addEventListener('click', () => {}, { signal: controller.signal }); |
| 118 | + document.addEventListener('click', () => {}, { once: true, capture: true }); |
| 119 | + controller.abort(); |
| 120 | + } |
| 121 | + document.dispatchEvent(new MouseEvent('click')); |
| 122 | + |
| 123 | + const clickHandlers = (document as InstrumentedDocument).__sentry_instrumentation_handlers__?.click; |
| 124 | + expect(clickHandlers.listenerCount).toBe(0); |
| 125 | + expect(clickHandlers.handler).toBeDefined(); |
| 126 | + }); |
| 127 | + |
| 128 | + it('keeps its handler attached after removing tracked listeners if a `once` or `signal` listener was added', () => { |
| 129 | + const countDocumentClickListeners = instrumentAndTrackDocumentClickListeners(); |
| 130 | + const baseline = countDocumentClickListeners(); |
| 131 | + |
| 132 | + const onClick = (): void => {}; |
| 133 | + const onceClick = (): void => {}; |
| 134 | + |
| 135 | + document.addEventListener('click', onClick); |
| 136 | + document.addEventListener('click', onceClick, { once: true }); |
| 137 | + document.removeEventListener('click', onClick); |
| 138 | + |
| 139 | + // `onceClick` plus the SDK's handler, which must still see the pending `once` listener's event |
| 140 | + expect(countDocumentClickListeners() - baseline).toBe(2); |
| 141 | + }); |
| 142 | + |
| 143 | + it('counts a listener added twice in the same phase only once', () => { |
| 144 | + const countDocumentClickListeners = instrumentAndTrackDocumentClickListeners(); |
| 145 | + const baseline = countDocumentClickListeners(); |
| 146 | + |
| 147 | + const onClick = (): void => {}; |
| 148 | + |
| 149 | + document.addEventListener('click', onClick); |
| 150 | + document.addEventListener('click', onClick); |
| 151 | + document.removeEventListener('click', onClick); |
| 152 | + |
| 153 | + expect(countDocumentClickListeners() - baseline).toBe(0); |
| 154 | + }); |
| 155 | + |
| 156 | + it('removes its handler if a listener is re-added with `once` while already registered', () => { |
| 157 | + const countDocumentClickListeners = instrumentAndTrackDocumentClickListeners(); |
| 158 | + const baseline = countDocumentClickListeners(); |
| 159 | + |
| 160 | + const onClick = (): void => {}; |
| 161 | + |
| 162 | + document.addEventListener('click', onClick); |
| 163 | + // no-op in the browser, since `onClick` is already registered for the bubble phase |
| 164 | + document.addEventListener('click', onClick, { once: true }); |
| 165 | + document.removeEventListener('click', onClick); |
| 166 | + |
| 167 | + expect(countDocumentClickListeners() - baseline).toBe(0); |
| 168 | + }); |
12 | 169 | }); |
0 commit comments