|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { stub } from 'sinon'; |
| 3 | +import getActiveElement from './getActiveElement'; |
| 4 | + |
| 5 | +describe('getActiveElement', () => { |
| 6 | + it('should return the active element from document', () => { |
| 7 | + const button = document.createElement('button'); |
| 8 | + document.body.appendChild(button); |
| 9 | + button.focus(); |
| 10 | + |
| 11 | + const activeElement = getActiveElement(document); |
| 12 | + expect(activeElement).to.equal(button); |
| 13 | + |
| 14 | + document.body.removeChild(button); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should return null when no element has focus', () => { |
| 18 | + const activeElementStub = stub(document, 'activeElement').get(() => null); |
| 19 | + |
| 20 | + const activeElement = getActiveElement(document); |
| 21 | + expect(activeElement).to.equal(null); |
| 22 | + |
| 23 | + activeElementStub.restore(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should traverse shadow roots to find the actual focused element', () => { |
| 27 | + // Create a shadow host |
| 28 | + const host = document.createElement('div'); |
| 29 | + document.body.appendChild(host); |
| 30 | + const shadowRoot = host.attachShadow({ mode: 'open' }); |
| 31 | + |
| 32 | + // Create an element inside the shadow root |
| 33 | + const button = document.createElement('button'); |
| 34 | + shadowRoot.appendChild(button); |
| 35 | + button.focus(); |
| 36 | + |
| 37 | + // document.activeElement should point to the host |
| 38 | + expect(document.activeElement).to.equal(host); |
| 39 | + |
| 40 | + // getActiveElement should traverse into the shadow root |
| 41 | + const activeElement = getActiveElement(document); |
| 42 | + expect(activeElement).to.equal(button); |
| 43 | + |
| 44 | + document.body.removeChild(host); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should handle nested shadow roots', () => { |
| 48 | + // Create outer shadow host |
| 49 | + const outerHost = document.createElement('div'); |
| 50 | + document.body.appendChild(outerHost); |
| 51 | + const outerShadowRoot = outerHost.attachShadow({ mode: 'open' }); |
| 52 | + |
| 53 | + // Create inner shadow host inside outer shadow root |
| 54 | + const innerHost = document.createElement('div'); |
| 55 | + outerShadowRoot.appendChild(innerHost); |
| 56 | + const innerShadowRoot = innerHost.attachShadow({ mode: 'open' }); |
| 57 | + |
| 58 | + // Create a button inside the inner shadow root |
| 59 | + const button = document.createElement('button'); |
| 60 | + innerShadowRoot.appendChild(button); |
| 61 | + button.focus(); |
| 62 | + |
| 63 | + // document.activeElement should point to the outer host |
| 64 | + expect(document.activeElement).to.equal(outerHost); |
| 65 | + |
| 66 | + // getActiveElement should traverse through both shadow roots |
| 67 | + const activeElement = getActiveElement(document); |
| 68 | + expect(activeElement).to.equal(button); |
| 69 | + |
| 70 | + document.body.removeChild(outerHost); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should return the element inside shadow root when it has focus', () => { |
| 74 | + const host = document.createElement('div'); |
| 75 | + document.body.appendChild(host); |
| 76 | + const shadowRoot = host.attachShadow({ mode: 'open' }); |
| 77 | + |
| 78 | + const button = document.createElement('button'); |
| 79 | + shadowRoot.appendChild(button); |
| 80 | + button.focus(); |
| 81 | + |
| 82 | + const activeElement = getActiveElement(document); |
| 83 | + expect(activeElement).to.equal(button); |
| 84 | + |
| 85 | + document.body.removeChild(host); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should work when starting from a shadow root', () => { |
| 89 | + const host = document.createElement('div'); |
| 90 | + document.body.appendChild(host); |
| 91 | + const shadowRoot = host.attachShadow({ mode: 'open' }); |
| 92 | + |
| 93 | + const button = document.createElement('button'); |
| 94 | + shadowRoot.appendChild(button); |
| 95 | + button.focus(); |
| 96 | + |
| 97 | + // When called with the shadow root directly |
| 98 | + const activeElement = getActiveElement(shadowRoot); |
| 99 | + expect(activeElement).to.equal(button); |
| 100 | + |
| 101 | + document.body.removeChild(host); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should handle shadow root with null activeElement', () => { |
| 105 | + const host = document.createElement('div'); |
| 106 | + document.body.appendChild(host); |
| 107 | + const shadowRoot = host.attachShadow({ mode: 'open' }); |
| 108 | + |
| 109 | + const activeElementStub = stub(shadowRoot, 'activeElement').get(() => null); |
| 110 | + |
| 111 | + const activeElement = getActiveElement(shadowRoot); |
| 112 | + expect(activeElement).to.equal(null); |
| 113 | + |
| 114 | + activeElementStub.restore(); |
| 115 | + document.body.removeChild(host); |
| 116 | + }); |
| 117 | +}); |
0 commit comments