319 lines
11 KiB
TypeScript
319 lines
11 KiB
TypeScript
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, getReviewTaskStats } from '@/api/review'
|
||
import { updateFragmentOwnership } from '@/api/reportFragments'
|
||
import appRouter from '@/router'
|
||
import { BASE_SPEED_PX_PER_SEC, NARROW_SPEED_FACTOR } from '@/utils/autoScroll'
|
||
|
||
vi.mock('@/api/review', () => ({
|
||
getReviewFeed: vi.fn(),
|
||
getReviewTaskStats: vi.fn(),
|
||
getTaskReview: vi.fn(),
|
||
}))
|
||
|
||
vi.mock('@/api/reportFragments', () => ({
|
||
updateFragmentOwnership: vi.fn(),
|
||
}))
|
||
|
||
const makeFragment = (id: number) => ({
|
||
id,
|
||
contextNum: `SESSION_${id}`,
|
||
contextType: 'SESSION',
|
||
taskName: `任务${id}`,
|
||
taskNum: `T${id}`,
|
||
sourceType: 'FRAGMENT',
|
||
content: `残片内容${id}`,
|
||
createdTime: '2026-01-01 10:00:00',
|
||
pendingOwnership: false,
|
||
})
|
||
|
||
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()
|
||
})
|
||
})
|
||
|
||
/**
|
||
* 待归类内容(归属-01):Feed 卡片打「待归类」标记,并在卡片上就地完成归类,不必进详情。
|
||
*/
|
||
describe('Welcome Feed 待归类内容', () => {
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
stubLayout(4000, 1000)
|
||
vi.mocked(getReviewTaskStats).mockResolvedValue({
|
||
code: 200,
|
||
data: [
|
||
{ taskNum: 'T002', taskName: 'TypeScript 类型体操' },
|
||
],
|
||
} as never)
|
||
vi.mocked(updateFragmentOwnership).mockResolvedValue({ code: 200, data: null } as never)
|
||
})
|
||
|
||
it('pendingOwnership 的残片在 Feed 上打「待归类」标记', async () => {
|
||
vi.mocked(getReviewFeed).mockResolvedValue({
|
||
code: 200,
|
||
data: [{ ...makeFragment(1), taskNum: null, taskName: null, pendingOwnership: true }],
|
||
} as never)
|
||
const wrapper = await mountWelcome()
|
||
|
||
expect(wrapper.find('.chip-pending').exists()).toBe(true)
|
||
expect(wrapper.find('.review-chip').text()).toContain('待归类')
|
||
})
|
||
|
||
it('点击待归类残片弹归类弹窗,选定任务后就地归类并刷新 Feed', async () => {
|
||
vi.mocked(getReviewFeed).mockResolvedValue({
|
||
code: 200,
|
||
data: [{ ...makeFragment(1), taskNum: null, taskName: null, pendingOwnership: true }],
|
||
} as never)
|
||
const wrapper = await mountWelcome()
|
||
|
||
await wrapper.find('.review-chip').trigger('click')
|
||
await flushPromises()
|
||
|
||
expect(wrapper.text()).toContain('归类这条学习残片')
|
||
expect(getReviewTaskStats).toHaveBeenCalled()
|
||
|
||
wrapper.findComponent({ name: 'ElSelect' }).vm.$emit('update:modelValue', 'T002')
|
||
await flushPromises()
|
||
|
||
const confirm = wrapper.findAll('button').find((b) => b.text().trim() === '确定归类')
|
||
await confirm!.trigger('click')
|
||
await flushPromises()
|
||
|
||
expect(updateFragmentOwnership).toHaveBeenCalledWith(1, 'T002')
|
||
// 归类成功后重新拉取 Feed
|
||
expect(getReviewFeed).toHaveBeenCalledTimes(2)
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('「升格为新任务」跳转到已有的新建任务流程并带上残片编号', async () => {
|
||
vi.mocked(getReviewFeed).mockResolvedValue({
|
||
code: 200,
|
||
data: [{ ...makeFragment(7), taskNum: null, taskName: null, pendingOwnership: true }],
|
||
} as never)
|
||
const pushSpy = vi.spyOn(appRouter, 'push').mockResolvedValue()
|
||
const wrapper = await mountWelcome()
|
||
|
||
await wrapper.find('.review-chip').trigger('click')
|
||
await flushPromises()
|
||
|
||
const promote = wrapper.findAll('button').find((b) => b.text().trim() === '升格为新任务')
|
||
await promote!.trigger('click')
|
||
await flushPromises()
|
||
|
||
expect(pushSpy).toHaveBeenCalledWith({ name: 'add-task', query: { promoteFragment: '7' } })
|
||
pushSpy.mockRestore()
|
||
wrapper.unmount()
|
||
})
|
||
})
|