diff --git a/src/components/checkbox/checkbox.test.ts b/src/components/checkbox/checkbox.test.ts index 55816b1c..49a486f4 100644 --- a/src/components/checkbox/checkbox.test.ts +++ b/src/components/checkbox/checkbox.test.ts @@ -1,5 +1,5 @@ +import { aTimeout, expect, fixture, html, oneEvent, waitUntil } from '@open-wc/testing'; import { clickOnElement } from '../../internal/test'; -import { expect, fixture, html, oneEvent, waitUntil } from '@open-wc/testing'; import { runFormControlBaseTests } from '../../internal/test/form-control-base-tests'; import { sendKeys } from '@web/test-runner-commands'; import sinon from 'sinon'; @@ -274,6 +274,58 @@ describe('', () => { expect(focusSpy.called).to.equal(true); expect(el.shadowRoot!.activeElement).to.equal(checkbox); }); + + it('should not jump the page to the bottom when focusing a checkbox at the bottom of an element with overflow: auto;', async () => { + // https://github.com/shoelace-style/shoelace/issues/1169 + const el = await fixture(html` +
+ Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox + Checkbox +
+ ; + `); + + const checkboxes = el.querySelectorAll('sl-checkbox'); + const lastSwitch = checkboxes[checkboxes.length - 1]; + + expect(window.scrollY).to.equal(0); + // Without these 2 timeouts, tests will pass unexpectedly in Safari. + await aTimeout(10); + lastSwitch.focus(); + await aTimeout(10); + expect(window.scrollY).to.equal(0); + }); }); describe('blur', () => { diff --git a/src/components/switch/switch.test.ts b/src/components/switch/switch.test.ts index 5cdcc2ae..b0cd8a30 100644 --- a/src/components/switch/switch.test.ts +++ b/src/components/switch/switch.test.ts @@ -262,5 +262,65 @@ describe('', () => { }); }); + it('should not jump the page to the bottom when focusing a switch at the bottom of an element with overflow: auto;', async () => { + // https://github.com/shoelace-style/shoelace/issues/1169 + const el = await fixture(html` +
+ Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch + Switch +
+ ; + `); + + const switches = el.querySelectorAll('sl-switch'); + const lastSwitch = switches[switches.length - 1]; + + expect(window.scrollY).to.equal(0); + // Without these 2 timeouts, tests will pass unexpectedly in Safari. + await aTimeout(10); + lastSwitch.focus(); + await aTimeout(10); + expect(window.scrollY).to.equal(0); + }); + runFormControlBaseTests('sl-switch'); });