fix: 首页滚动条改为恒速并补移动端拖动
This commit is contained in:
@@ -0,0 +1,234 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { flushPromises, mount } from '@vue/test-utils'
|
||||
import ElementPlus from 'element-plus'
|
||||
import { createMemoryHistory, createRouter } from 'vue-router'
|
||||
import Welcome from '@/components/Welcome.vue'
|
||||
import { getReviewFeed } from '@/api/review'
|
||||
import { BASE_SPEED_PX_PER_SEC, NARROW_SPEED_FACTOR } from '@/utils/autoScroll'
|
||||
|
||||
vi.mock('@/api/review', () => ({
|
||||
getReviewFeed: vi.fn(),
|
||||
getTaskReview: vi.fn(),
|
||||
}))
|
||||
|
||||
const makeFragment = (id: number) => ({
|
||||
id,
|
||||
sessionNum: `SESSION_${id}`,
|
||||
taskName: `任务${id}`,
|
||||
taskNum: `T${id}`,
|
||||
sourceType: 'FRAGMENT',
|
||||
content: `残片内容${id}`,
|
||||
})
|
||||
|
||||
const router = createRouter({
|
||||
history: createMemoryHistory(),
|
||||
routes: [
|
||||
{ path: '/', component: { template: '<div />' } },
|
||||
{ path: '/welcome', component: { template: '<div />' } },
|
||||
{ path: '/study', component: { template: '<div />' } },
|
||||
{ path: '/review', component: { template: '<div />' } },
|
||||
],
|
||||
})
|
||||
|
||||
/** jsdom 不实现 Web Animations API,用可控桩替代 */
|
||||
interface AnimStub {
|
||||
keyframes: unknown
|
||||
options: { duration: number; iterations: number; easing: string }
|
||||
currentTime: number
|
||||
paused: boolean
|
||||
cancel: ReturnType<typeof vi.fn>
|
||||
pause: ReturnType<typeof vi.fn>
|
||||
play: ReturnType<typeof vi.fn>
|
||||
}
|
||||
|
||||
let animations: AnimStub[] = []
|
||||
let animateSpy: ReturnType<typeof vi.fn>
|
||||
|
||||
/**
|
||||
* jsdom 不做布局,用宽度桩模拟渲染结果:
|
||||
* scrollWidth = 单份内容宽 × 当前已渲染份数(由 DOM 里的 chip 数量推得),
|
||||
* clientWidth = 轨道宽度。
|
||||
*/
|
||||
const stubLayout = (perCopyWidth: number, trackWidth: number) => {
|
||||
Object.defineProperty(HTMLElement.prototype, 'scrollWidth', {
|
||||
configurable: true,
|
||||
get(this: HTMLElement) {
|
||||
if (!this.classList?.contains('review-scroll-content')) return 0
|
||||
const renderedChips = this.querySelectorAll('.review-chip').length
|
||||
return perCopyWidth * (renderedChips / SOURCE_CHIP_COUNT)
|
||||
},
|
||||
})
|
||||
Object.defineProperty(HTMLElement.prototype, 'clientWidth', {
|
||||
configurable: true,
|
||||
get(this: HTMLElement) {
|
||||
return this.classList?.contains('review-scroll-track') ? trackWidth : 0
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/** mock 数据里的残留条数,用于把“份数”换算回“条数” */
|
||||
const SOURCE_CHIP_COUNT = 2
|
||||
/** 从渲染结果反推当前份数 */
|
||||
const renderedRepeatCount = (wrapper: { findAll: (s: string) => unknown[] }) =>
|
||||
wrapper.findAll('.review-chip').length / SOURCE_CHIP_COUNT
|
||||
|
||||
const mountWelcome = async () => {
|
||||
const wrapper = mount(Welcome, { global: { plugins: [router, ElementPlus] } })
|
||||
await flushPromises()
|
||||
return wrapper
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
animations = []
|
||||
animateSpy = vi.fn((keyframes: unknown, options: AnimStub['options']) => {
|
||||
const anim: AnimStub = {
|
||||
keyframes,
|
||||
options,
|
||||
currentTime: 0,
|
||||
paused: false,
|
||||
cancel: vi.fn(),
|
||||
pause: vi.fn(function (this: AnimStub) { this.paused = true }),
|
||||
play: vi.fn(function (this: AnimStub) { this.paused = false }),
|
||||
}
|
||||
animations.push(anim)
|
||||
return anim as unknown as Animation
|
||||
})
|
||||
Element.prototype.animate = animateSpy as unknown as Element['animate']
|
||||
vi.mocked(getReviewFeed).mockResolvedValue({ code: 200, data: [makeFragment(1), makeFragment(2)] })
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
// @ts-expect-error 清理原型上的自定义布局桩
|
||||
delete HTMLElement.prototype.scrollWidth
|
||||
// @ts-expect-error 清理原型上的自定义布局桩
|
||||
delete HTMLElement.prototype.clientWidth
|
||||
})
|
||||
|
||||
describe('Welcome 首页滚动条', () => {
|
||||
it('内容超过一屏时按基准速度建立动画,时长由恒速换算', async () => {
|
||||
stubLayout(4000, 1000) // 单份 4000px,60px/s → 66666ms
|
||||
await mountWelcome()
|
||||
|
||||
expect(animateSpy).toHaveBeenCalledTimes(1)
|
||||
const options = animations[0].options
|
||||
expect(options.iterations).toBe(Infinity)
|
||||
expect(options.easing).toBe('linear')
|
||||
expect(options.duration).toBeCloseTo((4000 / BASE_SPEED_PX_PER_SEC) * 1000)
|
||||
})
|
||||
|
||||
it('速度与内容量无关:内容翻倍只让时长翻倍', async () => {
|
||||
stubLayout(4000, 1000)
|
||||
await mountWelcome()
|
||||
const few = animations[0].options.duration
|
||||
|
||||
animations = []
|
||||
animateSpy.mockClear()
|
||||
stubLayout(40000, 1000)
|
||||
await mountWelcome()
|
||||
const many = animations[0].options.duration
|
||||
|
||||
expect(many / few).toBeCloseTo(10)
|
||||
})
|
||||
|
||||
it('内容不足一屏时仍然滚动,且按单份宽度换算时长', async () => {
|
||||
stubLayout(300, 1000) // 单份 300px,远小于轨道
|
||||
await mountWelcome()
|
||||
|
||||
expect(animateSpy).toHaveBeenCalled()
|
||||
expect(animations[animations.length - 1].options.duration)
|
||||
.toBeCloseTo((300 / BASE_SPEED_PX_PER_SEC) * 1000)
|
||||
})
|
||||
|
||||
it('内容比轨道窄时补足份数后重建动画,避免循环露白', async () => {
|
||||
stubLayout(300, 1000) // 单份 300px、轨道 1000px → ceil(1000/300)+1 = 5 份
|
||||
const wrapper = await mountWelcome()
|
||||
|
||||
expect(renderedRepeatCount(wrapper)).toBe(5)
|
||||
expect(animateSpy).toHaveBeenCalledTimes(1)
|
||||
// 位移按份数换算:100% / 5 = 20%
|
||||
expect(animations[0].keyframes).toEqual([
|
||||
{ transform: 'translateX(0)' },
|
||||
{ transform: 'translateX(-20%)' },
|
||||
])
|
||||
})
|
||||
|
||||
it('内容足够宽时保持最少两份,不会反复重建', async () => {
|
||||
stubLayout(4000, 1000)
|
||||
const wrapper = await mountWelcome()
|
||||
|
||||
expect(renderedRepeatCount(wrapper)).toBe(2)
|
||||
expect(animateSpy).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('窄屏(手机)按系数降速', async () => {
|
||||
stubLayout(4000, 375)
|
||||
await mountWelcome()
|
||||
|
||||
const speed = 4000 / (animations[0].options.duration / 1000)
|
||||
expect(speed).toBeCloseTo(BASE_SPEED_PX_PER_SEC * NARROW_SPEED_FACTOR)
|
||||
})
|
||||
|
||||
it('滚轮按速度换算手动位移,并夹在一轮时长内', async () => {
|
||||
stubLayout(4000, 1000)
|
||||
const wrapper = await mountWelcome()
|
||||
const track = wrapper.find('.review-scroll-track')
|
||||
|
||||
await track.trigger('wheel', { deltaY: BASE_SPEED_PX_PER_SEC, deltaMode: 0 })
|
||||
// 滚动 60px = 1 秒动画时间
|
||||
expect(animations[0].currentTime).toBeCloseTo(1000)
|
||||
|
||||
await track.trigger('wheel', { deltaY: -100000, deltaMode: 0 })
|
||||
expect(animations[0].currentTime).toBe(0)
|
||||
})
|
||||
|
||||
it('手指左右拖动共用同一套换算,拖动后恢复播放', async () => {
|
||||
stubLayout(4000, 1000)
|
||||
const wrapper = await mountWelcome()
|
||||
const track = wrapper.find('.review-scroll-track')
|
||||
|
||||
await track.trigger('touchstart', { touches: [{ clientX: 300 }] })
|
||||
expect(animations[0].paused).toBe(true)
|
||||
|
||||
await track.trigger('touchmove', { touches: [{ clientX: 360 }] })
|
||||
expect(animations[0].currentTime).toBeCloseTo(1000)
|
||||
|
||||
await track.trigger('touchend')
|
||||
expect(animations[0].paused).toBe(false)
|
||||
})
|
||||
|
||||
it('拖动后松开不会误触打开回忆卡片', async () => {
|
||||
stubLayout(4000, 1000)
|
||||
const wrapper = await mountWelcome()
|
||||
const track = wrapper.find('.review-scroll-track')
|
||||
|
||||
await track.trigger('touchstart', { touches: [{ clientX: 300 }] })
|
||||
await track.trigger('touchmove', { touches: [{ clientX: 400 }] })
|
||||
await wrapper.find('.review-chip').trigger('click')
|
||||
await flushPromises()
|
||||
|
||||
expect(wrapper.find('.recall-question').exists()).toBe(false)
|
||||
})
|
||||
|
||||
it('点击残片(未拖动)会打开回忆卡片', async () => {
|
||||
stubLayout(4000, 1000)
|
||||
const wrapper = await mountWelcome()
|
||||
|
||||
await wrapper.find('.review-chip').trigger('click')
|
||||
await flushPromises()
|
||||
|
||||
expect(wrapper.find('.recall-question').exists()).toBe(true)
|
||||
expect(wrapper.find('.recall-snippet').text()).toContain('残片内容1')
|
||||
})
|
||||
|
||||
it('卸载时取消动画并移除窗口监听', async () => {
|
||||
const removeSpy = vi.spyOn(window, 'removeEventListener')
|
||||
stubLayout(4000, 1000)
|
||||
const wrapper = await mountWelcome()
|
||||
|
||||
wrapper.unmount()
|
||||
|
||||
expect(animations[0].cancel).toHaveBeenCalled()
|
||||
expect(removeSpy).toHaveBeenCalledWith('resize', expect.any(Function))
|
||||
removeSpy.mockRestore()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user