|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +import { BasePageObject } from '@cloudscape-design/browser-test-tools/page-objects'; |
| 4 | +import useBrowser from '@cloudscape-design/browser-test-tools/use-browser'; |
| 5 | + |
| 6 | +import createWrapper from '../../../lib/components/test-utils/selectors'; |
| 7 | + |
| 8 | +const list = createWrapper().findOptionList('[data-testid="option-list"]'); |
| 9 | +const listbox = `${list.toSelector()} [role="listbox"]`; |
| 10 | + |
| 11 | +class ScrollPage extends BasePageObject { |
| 12 | + /** Whether the highlighted option is inside the viewport and inside every scrolling ancestor. */ |
| 13 | + isHighlightVisible() { |
| 14 | + return this.browser.execute(selector => { |
| 15 | + const option = document.querySelector(selector); |
| 16 | + if (!option) { |
| 17 | + return false; |
| 18 | + } |
| 19 | + const rect = option.getBoundingClientRect(); |
| 20 | + const within = (top: number, bottom: number) => rect.top >= top - 1 && rect.bottom <= bottom + 1; |
| 21 | + for (let parent = option.parentElement; parent; parent = parent.parentElement) { |
| 22 | + const { overflowY } = getComputedStyle(parent); |
| 23 | + if ((overflowY === 'auto' || overflowY === 'scroll') && parent.scrollHeight > parent.clientHeight) { |
| 24 | + const parentRect = parent.getBoundingClientRect(); |
| 25 | + if (!within(parentRect.top + parent.clientTop, parentRect.top + parent.clientTop + parent.clientHeight)) { |
| 26 | + return false; |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + return within(0, window.innerHeight); |
| 31 | + }, list.findHighlightedOption().toSelector()); |
| 32 | + } |
| 33 | + |
| 34 | + getHighlightedText() { |
| 35 | + return this.getText(list.findHighlightedOption().findLabel().toSelector()); |
| 36 | + } |
| 37 | + |
| 38 | + /** Scrolls the list's nearest scroll container (or the page) back to the top, away from the highlight. */ |
| 39 | + scrollToTop() { |
| 40 | + return this.browser.execute(selector => { |
| 41 | + for (let parent = document.querySelector(selector)!.parentElement; parent; parent = parent.parentElement) { |
| 42 | + const { overflowY } = getComputedStyle(parent); |
| 43 | + if ((overflowY === 'auto' || overflowY === 'scroll') && parent.scrollHeight > parent.clientHeight) { |
| 44 | + parent.scrollTop = 0; |
| 45 | + return; |
| 46 | + } |
| 47 | + } |
| 48 | + window.scrollTo(0, 0); |
| 49 | + }, listbox); |
| 50 | + } |
| 51 | + |
| 52 | + /** Retries the visibility check for a short time, so it doesn't run before a scroll has settled. */ |
| 53 | + async waitForHighlightVisible() { |
| 54 | + await this.waitForAssertion(() => expect(this.isHighlightVisible()).resolves.toBe(true)); |
| 55 | + } |
| 56 | + |
| 57 | + async expectHighlightVisibleAfter(keys: string[], label: string) { |
| 58 | + await this.keys(keys); |
| 59 | + await expect(this.getHighlightedText()).resolves.toBe(label); |
| 60 | + await this.waitForHighlightVisible(); |
| 61 | + } |
| 62 | + |
| 63 | + async assertKeyboardScrolling() { |
| 64 | + // Arrow keys call preventDefault, so the browser doesn't scroll on its own: every scroll here comes from the list. |
| 65 | + await this.expectHighlightVisibleAfter(new Array(30).fill('ArrowDown'), 'Option 31'); |
| 66 | + await this.expectHighlightVisibleAfter(new Array(30).fill('ArrowUp'), 'Option 1'); |
| 67 | + |
| 68 | + // Wrapping around jumps between the two ends of the list. |
| 69 | + await this.expectHighlightVisibleAfter(['ArrowUp'], 'Option 100'); |
| 70 | + await this.expectHighlightVisibleAfter(['ArrowDown'], 'Option 1'); |
| 71 | + await this.expectHighlightVisibleAfter(['ArrowUp'], 'Option 100'); |
| 72 | + |
| 73 | + // Scroll away from the highlight, so the next move has to scroll back to it. |
| 74 | + await this.scrollToTop(); |
| 75 | + await this.expectHighlightVisibleAfter(['ArrowUp'], 'Option 99'); |
| 76 | + |
| 77 | + // Home and End don't call preventDefault, so the browser also starts its native scroll. That scroll is animated |
| 78 | + // and keeps running after the key press, so it would move the page under any later check. Check them last, and |
| 79 | + // only check where they move the highlight. |
| 80 | + await this.keys(['End']); |
| 81 | + await expect(this.getHighlightedText()).resolves.toBe('Option 100'); |
| 82 | + await this.keys(['Home']); |
| 83 | + await expect(this.getHighlightedText()).resolves.toBe('Option 1'); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +const setupTest = (url: string, testFn: (page: ScrollPage) => Promise<void>) => |
| 88 | + useBrowser({ width: 1200, height: 800 }, async browser => { |
| 89 | + await browser.url(url); |
| 90 | + const page = new ScrollPage(browser); |
| 91 | + await page.waitForVisible('h1'); |
| 92 | + await testFn(page); |
| 93 | + }); |
| 94 | + |
| 95 | +describe('OptionList keyboard scrolling', () => { |
| 96 | + describe.each(['wrapper', 'page'])('container=%s', container => { |
| 97 | + const focusList = async (page: ScrollPage) => { |
| 98 | + await page.click('#option-list-heading'); |
| 99 | + await page.keys(['Tab']); |
| 100 | + await expect(page.isFocused(listbox)).resolves.toBe(true); |
| 101 | + }; |
| 102 | + |
| 103 | + test( |
| 104 | + 'keeps the highlighted option visible', |
| 105 | + setupTest(`#/light/option-list/scroll?container=${container}`, async page => { |
| 106 | + await focusList(page); |
| 107 | + await page.assertKeyboardScrolling(); |
| 108 | + }) |
| 109 | + ); |
| 110 | + |
| 111 | + test( |
| 112 | + 'focus entry highlights the selected option near the end and scrolls it into view', |
| 113 | + setupTest(`#/light/option-list/scroll?container=${container}&preselected=near-end`, async page => { |
| 114 | + await focusList(page); |
| 115 | + await expect(page.getHighlightedText()).resolves.toBe('Option 81'); |
| 116 | + await page.waitForHighlightVisible(); |
| 117 | + }) |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + describe.each([false, true])('in a Dropdown, expandToViewport=%s', expandToViewport => { |
| 122 | + test( |
| 123 | + 'keeps the highlighted option visible without scrolling the page on open', |
| 124 | + setupTest(`#/light/option-list/scroll?container=dropdown&expandToViewport=${expandToViewport}`, async page => { |
| 125 | + const scrollBefore = await page.getWindowScroll(); |
| 126 | + await page.click('#dropdown-trigger'); |
| 127 | + await page.waitForVisible(listbox); |
| 128 | + await expect(page.isFocused(listbox)).resolves.toBe(true); |
| 129 | + await expect(page.getWindowScroll()).resolves.toEqual(scrollBefore); |
| 130 | + await page.assertKeyboardScrolling(); |
| 131 | + }) |
| 132 | + ); |
| 133 | + }); |
| 134 | +}); |
0 commit comments