From ea75462c3de0a17e67ffe4decf93dce1c448b053 Mon Sep 17 00:00:00 2001 From: konnorrogers Date: Tue, 22 Aug 2023 17:40:24 -0400 Subject: [PATCH] fix tests --- src/internal/tabbable.test.ts | 70 ++++++++++++++--------------------- 1 file changed, 27 insertions(+), 43 deletions(-) diff --git a/src/internal/tabbable.test.ts b/src/internal/tabbable.test.ts index 4367d0f6..0f984443 100644 --- a/src/internal/tabbable.test.ts +++ b/src/internal/tabbable.test.ts @@ -4,32 +4,7 @@ import '../../dist/shoelace.js'; import { html } from 'lit'; import { sendKeys } from '@web/test-runner-commands'; import type { SlDrawer } from '../../dist/shoelace.js'; - -function getActiveElements(el: null | Element = document.activeElement) { - const elements: Element[] = []; - - function walk(element: null | Element) { - if (element === null || element === undefined) { - return; - } - - elements.push(element); - - if ('shadowRoot' in element && element.shadowRoot && element.shadowRoot.mode !== 'closed') { - walk(element.shadowRoot.activeElement); - } - } - - walk(el); - - return elements; -} - -function getDeepestActiveElement(el: null | Element = document.activeElement) { - const activeElements = getActiveElements(el); - - return activeElements[activeElements.length - 1]; -} +import { activeElements } from './active-elements.js'; async function holdShiftKey(callback: () => Promise) { await sendKeys({ down: 'Shift' }); @@ -37,6 +12,15 @@ async function holdShiftKey(callback: () => Promise) { await sendKeys({ up: 'Shift' }); } +// Simple helper to turn the activeElements generator into an array +function activeElementsArray () { + return [...activeElements()] +} + +function getDeepestActiveElement () { + return activeElementsArray().pop() +} + window.customElements.define( 'tab-test-1', class extends HTMLElement { @@ -97,57 +81,57 @@ it('Should allow tabbing to slotted elements', async () => { expect(getDeepestActiveElement()).to.equal(focusZero); await sendKeys({ press: 'Tab' }); - expect(getActiveElements()).to.include(focusOne); + expect(activeElementsArray()).to.include(focusOne); // When we hit the key we should go to the "close button" on the drawer await sendKeys({ press: 'Tab' }); - expect(getActiveElements()).to.include(focusTwo); + expect(activeElementsArray()).to.include(focusTwo); await sendKeys({ press: 'Tab' }); - expect(getActiveElements()).to.include(focusThree); + expect(activeElementsArray()).to.include(focusThree); await sendKeys({ press: 'Tab' }); - expect(getActiveElements()).to.include(focusFour); + expect(activeElementsArray()).to.include(focusFour); await sendKeys({ press: 'Tab' }); - expect(getActiveElements()).to.include(focusFive); + expect(activeElementsArray()).to.include(focusFive); await sendKeys({ press: 'Tab' }); - expect(getActiveElements()).to.include(focusSix); + expect(activeElementsArray()).to.include(focusSix); // Now we should loop back to #panel await sendKeys({ press: 'Tab' }); - expect(getActiveElements()).to.include(focusZero); + expect(activeElementsArray()).to.include(focusZero); // Now we should loop back to #panel await sendKeys({ press: 'Tab' }); - expect(getActiveElements()).to.include(focusOne); + expect(activeElementsArray()).to.include(focusOne); // Let's reset and try from starting point 0 and go backwards. await holdShiftKey(async () => await sendKeys({ press: 'Tab' })); - expect(getActiveElements()).to.include(focusZero); + expect(activeElementsArray()).to.include(focusZero); await holdShiftKey(async () => await sendKeys({ press: 'Tab' })); - expect(getActiveElements()).to.include(focusSix); + expect(activeElementsArray()).to.include(focusSix); await holdShiftKey(async () => await sendKeys({ press: 'Tab' })); - expect(getActiveElements()).to.include(focusFive); + expect(activeElementsArray()).to.include(focusFive); await holdShiftKey(async () => await sendKeys({ press: 'Tab' })); - expect(getActiveElements()).to.include(focusFour); + expect(activeElementsArray()).to.include(focusFour); await holdShiftKey(async () => await sendKeys({ press: 'Tab' })); - expect(getActiveElements()).to.include(focusThree); + expect(activeElementsArray()).to.include(focusThree); await holdShiftKey(async () => await sendKeys({ press: 'Tab' })); - expect(getActiveElements()).to.include(focusTwo); + expect(activeElementsArray()).to.include(focusTwo); await holdShiftKey(async () => await sendKeys({ press: 'Tab' })); - expect(getActiveElements()).to.include(focusOne); + expect(activeElementsArray()).to.include(focusOne); await holdShiftKey(async () => await sendKeys({ press: 'Tab' })); - expect(getActiveElements()).to.include(focusZero); + expect(activeElementsArray()).to.include(focusZero); await holdShiftKey(async () => await sendKeys({ press: 'Tab' })); - expect(getActiveElements()).to.include(focusSix); + expect(activeElementsArray()).to.include(focusSix); });