|
| 1 | +/* eslint-disable jest/no-done-callback */ |
| 2 | +import { sleep } from '@segment/analytics-core' |
| 3 | +import { |
| 4 | + MutationObservable, |
| 5 | + MutationObservableSettings, |
| 6 | + MutationObservableSubscriber, |
| 7 | +} from './mutation-observer' |
| 8 | + |
| 9 | +describe('MutationObservable', () => { |
| 10 | + let mutationObservable: MutationObservable |
| 11 | + let testButton: HTMLButtonElement |
| 12 | + let testInput: HTMLInputElement |
| 13 | + const subscribeFn = jest.fn() as jest.Mock<MutationObservableSubscriber> |
| 14 | + beforeEach(() => { |
| 15 | + document.body.innerHTML = |
| 16 | + '<div id="test-element" role="button" aria-pressed="false"></div>' + |
| 17 | + '<input id="test-input" />' |
| 18 | + testButton = document.getElementById('test-element') as HTMLButtonElement |
| 19 | + testInput = document.getElementById('test-input') as HTMLInputElement |
| 20 | + }) |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + mutationObservable.cleanup() |
| 24 | + }) |
| 25 | + |
| 26 | + it('should capture attribute changes', async () => { |
| 27 | + mutationObservable = new MutationObservable( |
| 28 | + new MutationObservableSettings({ |
| 29 | + observedRoles: () => ['button'], |
| 30 | + observedAttributes: () => ['aria-pressed'], |
| 31 | + debounceMs: 500, |
| 32 | + }) |
| 33 | + ) |
| 34 | + |
| 35 | + mutationObservable.subscribe(subscribeFn) |
| 36 | + testButton.setAttribute('aria-pressed', 'true') |
| 37 | + await sleep(0) |
| 38 | + |
| 39 | + expect(subscribeFn).toHaveBeenCalledTimes(1) |
| 40 | + expect(subscribeFn).toHaveBeenCalledWith({ |
| 41 | + element: testButton, |
| 42 | + attributes: [{ attributeName: 'aria-pressed', newValue: 'true' }], |
| 43 | + }) |
| 44 | + }) |
| 45 | + |
| 46 | + it('should capture multiple attribute changes', async () => { |
| 47 | + mutationObservable = new MutationObservable( |
| 48 | + new MutationObservableSettings({ |
| 49 | + observedRoles: () => ['button'], |
| 50 | + observedAttributes: () => ['aria-pressed'], |
| 51 | + debounceMs: 500, |
| 52 | + }) |
| 53 | + ) |
| 54 | + |
| 55 | + mutationObservable.subscribe(subscribeFn) |
| 56 | + testButton.setAttribute('aria-pressed', 'true') |
| 57 | + await sleep(0) |
| 58 | + testButton.setAttribute('aria-pressed', 'false') |
| 59 | + await sleep(0) |
| 60 | + |
| 61 | + expect(subscribeFn).toHaveBeenCalledTimes(2) |
| 62 | + expect(subscribeFn).toHaveBeenNthCalledWith(1, { |
| 63 | + element: testButton, |
| 64 | + attributes: [{ attributeName: 'aria-pressed', newValue: 'true' }], |
| 65 | + }) |
| 66 | + expect(subscribeFn).toHaveBeenNthCalledWith(2, { |
| 67 | + element: testButton, |
| 68 | + attributes: [{ attributeName: 'aria-pressed', newValue: 'false' }], |
| 69 | + }) |
| 70 | + }) |
| 71 | + |
| 72 | + it('should debounce attribute changes if they occur in text inputs', async () => { |
| 73 | + mutationObservable = new MutationObservable( |
| 74 | + new MutationObservableSettings({ |
| 75 | + debounceMs: 100, |
| 76 | + }) |
| 77 | + ) |
| 78 | + mutationObservable.subscribe(subscribeFn) |
| 79 | + testInput.setAttribute('value', 'hello') |
| 80 | + await sleep(0) |
| 81 | + testInput.setAttribute('value', 'hello wor') |
| 82 | + await sleep(0) |
| 83 | + testInput.setAttribute('value', 'hello world') |
| 84 | + await sleep(200) |
| 85 | + |
| 86 | + expect(subscribeFn).toHaveBeenCalledTimes(1) |
| 87 | + expect(subscribeFn).toHaveBeenCalledWith({ |
| 88 | + element: testInput, |
| 89 | + attributes: [{ attributeName: 'value', newValue: 'hello world' }], |
| 90 | + }) |
| 91 | + }) |
| 92 | + |
| 93 | + it('should not emit event for aria-selected=false', (done) => { |
| 94 | + mutationObservable = new MutationObservable( |
| 95 | + new MutationObservableSettings({ |
| 96 | + observedRoles: () => ['button'], |
| 97 | + observedAttributes: () => ['aria-selected'], |
| 98 | + }) |
| 99 | + ) |
| 100 | + |
| 101 | + mutationObservable.subscribe(() => { |
| 102 | + done.fail('Should not emit event for aria-selected=false') |
| 103 | + }) |
| 104 | + |
| 105 | + testButton.setAttribute('aria-selected', 'false') |
| 106 | + setTimeout(done, 1000) // Wait to ensure no event is emitted |
| 107 | + }) |
| 108 | +}) |
0 commit comments