`);
const invokers = el.querySelectorAll('[slot=invoker]');
invokers[1].firstElementChild.dispatchEvent(new KeyboardEvent('keyup', { key: 'Home' }));
expect(el.focusedIndex).to.equal(0);
});
it('selects last invoker on [end]', async () => {
const el = await fixture(basicAccordion);
const invokers = el.querySelectorAll('[slot=invoker]');
invokers[0].firstElementChild.dispatchEvent(new KeyboardEvent('keyup', { key: 'End' }));
expect(el.focusedIndex).to.equal(2);
});
it('selects first invoker on [arrow-right] if on last invoker', async () => {
const el = await fixture(html`
content 1
content 2
content 3
`);
const invokers = el.querySelectorAll('[slot=invoker]');
invokers[2].firstElementChild.dispatchEvent(
new KeyboardEvent('keyup', { key: 'ArrowRight' }),
);
expect(el.focusedIndex).to.equal(0);
});
it('selects last invoker on [arrow-left] if on first invoker', async () => {
const el = await fixture(html`
content 1
content 2
content 3
`);
const invokers = el.querySelectorAll('[slot=invoker]');
invokers[0].firstElementChild.dispatchEvent(new KeyboardEvent('keyup', { key: 'ArrowLeft' }));
expect(el.focusedIndex).to.equal(2);
});
});
describe('Content distribution', () => {
it('should work with append children', async () => {
const el = await fixture(basicAccordion);
const c = 2;
const n = el.children.length / 2;
for (let i = n + 1; i < n + c + 1; i += 1) {
const invoker = document.createElement('h2');
const button = document.createElement('button');
invoker.setAttribute('slot', 'invoker');
button.innerText = `invoker ${i}`;
invoker.appendChild(button);
const content = document.createElement('div');
content.setAttribute('slot', 'content');
content.innerText = `content ${i}`;
el.append(invoker);
el.append(content);
}
el.expanded = [el.children.length / 2 - 1];
await el.updateComplete;
expect(
Array.from(el.children).find(
child => child.slot === 'invoker' && child.hasAttribute('expanded'),
).textContent,
).to.equal('invoker 5');
expect(
Array.from(el.children).find(
child => child.slot === 'content' && child.hasAttribute('expanded'),
).textContent,
).to.equal('content 5');
});
it('should add order style property to each invoker and content', async () => {
const el = await fixture(basicAccordion);
const c = 2;
const n = el.children.length / 2;
for (let i = n + 1; i < n + c + 1; i += 1) {
const invoker = document.createElement('h2');
const button = document.createElement('button');
invoker.setAttribute('slot', 'invoker');
button.innerText = `invoker ${i}`;
invoker.appendChild(button);
const content = document.createElement('div');
content.setAttribute('slot', 'content');
content.innerText = `content ${i}`;
el.append(invoker);
el.append(content);
}
el.expanded = [el.children.length / 2 - 1];
await el.updateComplete;
const invokers = el.querySelectorAll('[slot=invoker]');
const contents = el.querySelectorAll('[slot=content]');
invokers.forEach((invoker, index) => {
const content = contents[index];
expect(invoker.style.getPropertyValue('order')).to.equal(`${index + 1}`);
expect(content.style.getPropertyValue('order')).to.equal(`${index + 1}`);
});
});
});
describe('Accessibility', () => {
it('does not make contents focusable', async () => {
const el = await fixture(html`
content 1
content 2
`);
expect(Array.from(el.children).find(child => child.slot === 'content')).to.not.have.attribute(
'tabindex',
);
expect(Array.from(el.children).find(child => child.slot === 'content')).to.not.have.attribute(
'tabindex',
);
});
describe('Invokers', () => {
it('links ids of content items to invoker first child via [aria-controls]', async () => {
const el = await fixture(html`
content 1
content 2
`);
const invokers = el.querySelectorAll('[slot=invoker]');
const contents = el.querySelectorAll('[slot=content]');
expect(invokers[0].firstElementChild.getAttribute('aria-controls')).to.equal(
contents[0].id,
);
expect(invokers[1].firstElementChild.getAttribute('aria-controls')).to.equal(
contents[1].id,
);
});
it('adds aria-expanded="false" to invoker when its content is not expanded', async () => {
const el = await fixture(html`
content
`);
expect(
Array.from(el.children).find(child => child.slot === 'invoker').firstElementChild,
).to.have.attribute('aria-expanded', 'false');
});
it('adds aria-expanded="true" to invoker when its content is expanded', async () => {
const el = await fixture(html`