import { expect, fixture, defineCE } from '@open-wc/testing';
import { LitElement, html } from '@lion/core';
import { getFocusableElements } from '../../src/utils/get-focusable-elements.js';
class ElementB extends LitElement {
render() {
const marker = this.getAttribute('marker') || '';
return html`
`;
}
}
customElements.define('element-b', ElementB);
describe('getFocusableElements()', () => {
it('collects focusable nodes', async () => {
const element = await fixture(`
`);
const nodes = getFocusableElements(element);
const ids = nodes.map(n => n.id);
expect(ids).eql(['el1', 'el2', 'el3', 'el4', 'el5', 'el6', 'el7']);
});
it('handles nested nodes', async () => {
const element = await fixture(`
`);
const nodes = getFocusableElements(element);
const ids = nodes.map(n => n.id);
expect(ids).eql(['el1', 'el2', 'el3', 'el4']);
});
it('skips elements that should not receive focus', async () => {
const element = await fixture(`
`);
const nodes = getFocusableElements(element);
const ids = nodes.map(n => n.id);
expect(ids).eql(['el1', 'el3', 'el8', 'el11']);
});
it('respects tabindex order', async () => {
const element = await fixture(`
`);
const nodes = getFocusableElements(element);
const ids = nodes.map(n => n.id);
expect(ids).eql(['el8', 'el10', 'el5', 'el6', 'el7', 'el9', 'el1', 'el2', 'el3']);
});
it('handles shadow dom', async () => {
const element = await fixture(`
`);
const nodes = getFocusableElements(element);
const ids = nodes.map(n => n.id);
expect(ids).eql(['el-b-marker-1', 'el-b-marker-2', 'el-b-marker-3']);
});
it('handles slotted elements', async () => {
const elTag = defineCE(
class extends LitElement {
render() {
return html`
`;
}
},
);
const element = await fixture(`
`);
const nodes = getFocusableElements(element);
const ids = nodes.map(n => n.id);
expect(ids).eql([
'el-a-1',
'el-b-marker-1-1',
'el-b-marker-1-2',
'el-b-marker-1-3',
'el-3',
'el-4',
'el-6',
'el-a-2',
'el-2',
'el-1',
'el-5',
'el-a-3',
'el-b-marker-2-1',
'el-b-marker-2-2',
'el-b-marker-2-3',
]);
});
});