import { expect, fixture, defineCE } from '@open-wc/testing'; import { LitElement, html } from '@lion/core'; import { getDeepActiveElement } from '../../src/utils/get-deep-active-element.js'; describe('getDeepActiveElement()', () => { it('handles document level active elements', async () => { const element = await fixture(`
Href
`); const el1 = element.querySelector('#el-1'); const el2 = element.querySelector('#el-2'); const el3 = element.querySelector('#el-3'); el1.focus(); expect(getDeepActiveElement()).to.eql(el1); el2.focus(); expect(getDeepActiveElement()).to.eql(el2); el3.focus(); expect(getDeepActiveElement()).to.eql(el3); }); it('handles active element inside shadowroots', async () => { const elNestedTag = defineCE( class extends LitElement { render() { return html`
Button
Href `; } }, ); const elTag = defineCE( class extends LitElement { render() { const elNested = document.createElement(elNestedTag); return html` ${elNested} `; } }, ); const element = await fixture(`
<${elTag}>
`); const elA = element.querySelector(elTag).shadowRoot; const elB = elA.querySelector(elNestedTag).shadowRoot; const elA1 = elA.querySelector('#el-a-1'); const elA2 = elA.querySelector('#el-a-2'); const elB1 = elB.querySelector('#el-b-1'); const elB2 = elB.querySelector('#el-b-1'); const el1 = element.querySelector('#el-1'); elA1.focus(); expect(getDeepActiveElement()).to.eql(elA1); elA2.focus(); expect(getDeepActiveElement()).to.eql(elA2); elB1.focus(); expect(getDeepActiveElement()).to.eql(elB1); elB2.focus(); expect(getDeepActiveElement()).to.eql(elB2); el1.focus(); expect(getDeepActiveElement()).to.eql(el1); }); });