Skip to content

Commit

Permalink
fix: focus tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeripeierSBB committed Mar 26, 2024
1 parent 3f6078c commit 8e7591b
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions src/components/core/a11y/focus.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ describe('focus', () => {
await customElements.whenDefined('my-custom-element');
lightDOM = await fixture(html`
<div>
<span tabindex="0" id="sbb-button">Button</span>
<a href="#" id="a">Link</a>
<custom-button tabindex="0" id="custom-button">Button</custom-button>
<custom-link tabindex="0" href="#" id="custom-link">Link</custom-link>
<div>
<input id="input" />
<button style="visibility: hidden" id="hidden-button">Button</button>
Expand All @@ -41,8 +41,8 @@ describe('focus', () => {
const elements = getFocusableElements([lightDOM, shadowDOM]);

expect(elements.map((el) => el.id)).to.deep.equal([
'sbb-button',
'a',
'custom-button',
'custom-link',
'input',
'shadow-button',
]);
Expand All @@ -54,8 +54,8 @@ describe('focus', () => {
});

expect(elements.map((el) => el.id)).to.deep.equal([
'sbb-button',
'a',
'custom-button',
'custom-link',
'input',
'hidden-button',
'shadow-button',
Expand All @@ -64,10 +64,10 @@ describe('focus', () => {

it('should retrieve filtered focusable elements', () => {
const elements = getFocusableElements([lightDOM, shadowDOM], {
filter: (el) => ['div', 'a'].includes(el.localName),
filter: (el) => ['div', 'custom-link'].includes(el.localName),
});

expect(elements.map((el) => el.id)).to.deep.equal(['a']);
expect(elements.map((el) => el.id)).to.deep.equal(['custom-link']);
});

it('should prefer slotted over shadow DOM', () => {
Expand All @@ -81,7 +81,7 @@ describe('focus', () => {
it('should retrieve first focusable element', () => {
const element = getFirstFocusableElement([lightDOM, shadowDOM]);

expect(element!.id).to.equal('sbb-button');
expect(element!.id).to.equal('custom-button');
});
});

Expand All @@ -95,8 +95,8 @@ describe('focus', () => {
super();
const shadowRoot = this.attachShadow({ mode: 'open' });
shadowRoot.innerHTML = `
<span tabindex="0" id="sbb-button">Button</span>
<a href="#" id="a">Link</a>
<custom-button tabindex="0" id="custom-button">Button</custom-button>
<custom-link tabindex="0" href="#" id="custom-link">Link</custom-link>
<div>
<input id="input" />
<button style="visibility: hidden" id="hidden-button">Button</button>
Expand All @@ -118,11 +118,11 @@ describe('focus', () => {
const focusHandler = new FocusHandler();
focusHandler.trap(element);

element.shadowRoot!.querySelector<HTMLSpanElement>('#sbb-button')!.focus();
expect(document.activeElement!.shadowRoot!.activeElement!.id).to.equal('sbb-button');
element.shadowRoot!.querySelector<HTMLElement>('#custom-button')!.focus();
expect(document.activeElement!.shadowRoot!.activeElement!.id).to.equal('custom-button');

await sendKeys({ press: 'Tab' });
expect(document.activeElement!.shadowRoot!.activeElement!.id).to.equal('a');
expect(document.activeElement!.shadowRoot!.activeElement!.id).to.equal('custom-link');

await sendKeys({ press: 'Tab' });

Expand All @@ -134,7 +134,7 @@ describe('focus', () => {

// Wrap around
await sendKeys({ press: 'Tab' });
expect(document.activeElement!.shadowRoot!.activeElement!.id).to.equal('sbb-button');
expect(document.activeElement!.shadowRoot!.activeElement!.id).to.equal('custom-button');
});

it('should focus next element with filter', async () => {
Expand All @@ -153,17 +153,17 @@ describe('focus', () => {
it('should focus next element with post filter', async () => {
const focusHandler = new FocusHandler();
focusHandler.trap(element, {
postFilter: (el) => ['sbb-button', 'a'].includes(el.id),
postFilter: (el) => ['custom-button', 'custom-link'].includes(el.id),
});

element.shadowRoot!.querySelector<HTMLSpanElement>('#sbb-button')!.focus();
expect(document.activeElement!.shadowRoot!.activeElement!.id).to.equal('sbb-button');
element.shadowRoot!.querySelector<HTMLElement>('#custom-button')!.focus();
expect(document.activeElement!.shadowRoot!.activeElement!.id).to.equal('custom-button');

await sendKeys({ press: 'Tab' });
expect(document.activeElement!.shadowRoot!.activeElement!.id).to.equal('a');
expect(document.activeElement!.shadowRoot!.activeElement!.id).to.equal('custom-link');

await sendKeys({ press: 'Tab' });
expect(document.activeElement!.shadowRoot!.activeElement!.id).to.equal('sbb-button');
expect(document.activeElement!.shadowRoot!.activeElement!.id).to.equal('custom-button');
});
});
});

0 comments on commit 8e7591b

Please sign in to comment.