import { defineComponent, h } from 'vue' import { flushPromises, mount } from '@vue/test-utils' import { expect, it, vi } from 'vitest' import BeatShotDirectory from '@/features/storyboard/components/BeatShotDirectory.vue' import { designedShot } from '@/features/storyboard/testing/fixtures' it('定位嵌套镜头时避让所属段落标题,首行镜号不被吸顶标题遮挡', async () => { const wrapper = mount( defineComponent({ props: { activeId: { type: String, required: true } }, setup: props => () => h('div', { class: 'n-scrollbar-container' }, [ h(BeatShotDirectory, { shots: [designedShot('first'), { ...designedShot('next'), beatNo: 2 }], activeId: props.activeId, itemClass: 'shot-link' }) ]) }), { props: { activeId: 'first' } } ) try { await flushPromises() const viewport = wrapper.get('.n-scrollbar-container').element const selected = wrapper.findAll('.directory-item')[1]!.element const heading = selected.closest('[role="group"]')!.querySelector('h4')! Object.defineProperty(heading, 'offsetHeight', { configurable: true, value: 38 }) vi.spyOn(viewport, 'getBoundingClientRect').mockReturnValue(new DOMRect(0, 100, 240, 400)) vi.spyOn(selected, 'getBoundingClientRect').mockReturnValue(new DOMRect(0, 180, 240, 88)) viewport.scrollTop = 80 /** 记录目录自身滚动目标,断言标题避让距离。 */ const scrollTo = vi.fn<(options: ScrollToOptions) => void>() Object.defineProperty(viewport, 'scrollTo', { configurable: true, value: scrollTo }) await wrapper.setProps({ activeId: 'next' }) await flushPromises() expect(scrollTo).toHaveBeenLastCalledWith({ top: 122, behavior: 'smooth' }) } finally { wrapper.unmount() } })