|
1 | 1 | import type { DOMWrapper, VueWrapper } from '@vue/test-utils' |
2 | 2 | import { mount } from '@vue/test-utils' |
3 | | -import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest' |
| 3 | +import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' |
4 | 4 | import { axe } from 'vitest-axe' |
5 | 5 | import { defineComponent, h, nextTick, ref } from 'vue' |
6 | 6 | import { useKbd } from '@/shared' |
7 | 7 | import { handleSubmit } from '@/test' |
8 | | -import { ListboxItem, ListboxRoot } from '.' |
| 8 | +import { ListboxContent, ListboxItem, ListboxRoot, ListboxVirtualizer } from '.' |
9 | 9 | import Listbox from './story/_Listbox.vue' |
10 | 10 |
|
11 | 11 | describe('given default Listbox', () => { |
@@ -167,6 +167,104 @@ describe('given a Listbox on initial mount', () => { |
167 | 167 | }) |
168 | 168 | }) |
169 | 169 |
|
| 170 | +describe('given a virtualized Listbox on initial mount', () => { |
| 171 | + let scrollSpy: ReturnType<typeof vi.fn> |
| 172 | + |
| 173 | + window.HTMLElement.prototype.releasePointerCapture = vi.fn() |
| 174 | + window.HTMLElement.prototype.hasPointerCapture = vi.fn() |
| 175 | + window.HTMLElement.prototype.scrollTo = vi.fn() |
| 176 | + globalThis.ResizeObserver = class ResizeObserver { |
| 177 | + observe() {} |
| 178 | + unobserve() {} |
| 179 | + disconnect() {} |
| 180 | + } |
| 181 | + |
| 182 | + // jsdom reports zero-sized rects, so `@tanstack/virtual-core` would render no |
| 183 | + // items. Give the virtualizer a non-zero viewport so items actually mount. |
| 184 | + const originalGetBoundingClientRect = window.HTMLElement.prototype.getBoundingClientRect |
| 185 | + beforeAll(() => { |
| 186 | + window.HTMLElement.prototype.getBoundingClientRect = function () { |
| 187 | + return { width: 200, height: 200, top: 0, left: 0, right: 200, bottom: 200, x: 0, y: 0, toJSON() {} } |
| 188 | + } |
| 189 | + }) |
| 190 | + afterAll(() => { |
| 191 | + window.HTMLElement.prototype.getBoundingClientRect = originalGetBoundingClientRect |
| 192 | + }) |
| 193 | + |
| 194 | + const VirtualListbox = defineComponent({ |
| 195 | + props: { multiple: Boolean, modelValue: { type: null, default: undefined } }, |
| 196 | + setup(props) { |
| 197 | + const options = Array.from({ length: 100 }, (_, i) => ({ label: `Item ${i}`, value: i })) |
| 198 | + return () => h(ListboxRoot, { multiple: props.multiple, modelValue: props.modelValue }, () => |
| 199 | + h(ListboxContent, { style: 'height: 200px; overflow: auto' }, () => |
| 200 | + h(ListboxVirtualizer, { options, textContent: (o: any) => o.label }, { |
| 201 | + default: ({ option }: any) => h(ListboxItem, { value: option }, () => option.label), |
| 202 | + }))) |
| 203 | + }, |
| 204 | + }) |
| 205 | + |
| 206 | + async function flush() { |
| 207 | + // watcher → nextTick → highlightSelected (await nextTick) → virtualFocusHook → rAF |
| 208 | + await nextTick() |
| 209 | + await nextTick() |
| 210 | + await new Promise(resolve => requestAnimationFrame(() => resolve(null))) |
| 211 | + await nextTick() |
| 212 | + } |
| 213 | + |
| 214 | + beforeEach(() => { |
| 215 | + scrollSpy = vi.fn() |
| 216 | + window.HTMLElement.prototype.scrollIntoView = scrollSpy |
| 217 | + document.body.innerHTML = '' |
| 218 | + }) |
| 219 | + |
| 220 | + it('should highlight the first item without scrolling the page or stealing focus', async () => { |
| 221 | + const wrapper = mount(VirtualListbox, { props: { multiple: true }, attachTo: document.body }) |
| 222 | + await flush() |
| 223 | + |
| 224 | + const items = wrapper.findAll('[role=option]') |
| 225 | + expect(items.length).toBeGreaterThan(0) |
| 226 | + // the first item is highlighted for keyboard entry... |
| 227 | + expect(items[0].attributes('data-highlighted')).toBe('') |
| 228 | + // ...but the mount highlight must not focus it or scroll, otherwise a |
| 229 | + // virtualized Listbox below the fold scrolls the whole page on load. |
| 230 | + expect(scrollSpy).not.toHaveBeenCalled() |
| 231 | + expect(document.activeElement).not.toBe(items[0].element) |
| 232 | + }) |
| 233 | + |
| 234 | + it('should highlight a pre-selected item on mount without scrolling the page or stealing focus', async () => { |
| 235 | + // A selected value below the fold must not pull the page to the listbox on |
| 236 | + // mount. The checked item is brought into the internal scroll container |
| 237 | + // (`scrollToIndex`) and made the roving-tabindex target, but it is neither |
| 238 | + // focused nor scrolled into view at the document level. |
| 239 | + // `modelValue` is the option object (items hold the whole option as value); |
| 240 | + // it matches option #3 structurally via the default `isEqual` comparison. |
| 241 | + const wrapper = mount(VirtualListbox, { props: { modelValue: { label: 'Item 3', value: 3 } }, attachTo: document.body }) |
| 242 | + await flush() |
| 243 | + |
| 244 | + const checked = wrapper.find('[data-index="3"]') |
| 245 | + expect(checked.exists()).toBe(true) |
| 246 | + // the checked item (not the first item) becomes the highlight target... |
| 247 | + expect(checked.attributes('data-highlighted')).toBe('') |
| 248 | + expect(wrapper.find('[data-index="0"]').attributes('data-highlighted')).toBeUndefined() |
| 249 | + // ...without focusing it or scrolling the page. |
| 250 | + expect(scrollSpy).not.toHaveBeenCalled() |
| 251 | + expect(document.activeElement).not.toBe(checked.element) |
| 252 | + }) |
| 253 | + |
| 254 | + it('should focus and scroll the first item when the user enters the listbox', async () => { |
| 255 | + // Entry focus (`onEnter`) is user-driven, so focusing and scrolling the |
| 256 | + // first item into view is expected here — unlike the mount highlight above. |
| 257 | + const wrapper = mount(VirtualListbox, { props: { multiple: true }, attachTo: document.body }) |
| 258 | + await flush() |
| 259 | + scrollSpy.mockClear() |
| 260 | + |
| 261 | + await wrapper.find('[role=listbox]').trigger('focus') |
| 262 | + const items = wrapper.findAll('[role=option]') |
| 263 | + expect(document.activeElement).toBe(items[0].element) |
| 264 | + expect(scrollSpy).toHaveBeenCalled() |
| 265 | + }) |
| 266 | +}) |
| 267 | + |
170 | 268 | describe('given multiple `true` Listbox', () => { |
171 | 269 | const kbd = useKbd() |
172 | 270 | let wrapper: VueWrapper<InstanceType<typeof Listbox>> |
|
0 commit comments