Files
FileCodeBoxFronted2026/tests/detailMotion.test.mjs
orion af009cf3c5 ✨ feat(core): 实现详情面板动画逻辑并完善相关测试
* 新增 detailMotion 工具类以支持更精细的动画控制
* 优化 FileManageView 面板渲染逻辑,改用 v-show 提升交互性能
* 调整 Transition 组件为手动控制模式以适配自定义动画
* 在 package.json 中配置自动化测试脚本
* 补充 detailMotion 相关的单元测试与测试说明文档
2026-09-15 11:25:43 +08:00

77 lines
2.7 KiB
JavaScript

import assert from 'node:assert/strict'
import { test } from 'node:test'
import { createDetailMotion } from '../src/utils/detailMotion.ts'
class FakeAnimation extends EventTarget {
cancelled = false
cancel() { this.cancelled = true; this.dispatchEvent(new Event('cancel')) }
finish() { this.dispatchEvent(new Event('finish')) }
}
class FakeElement {
animations = []
animate(frames, options) {
const animation = new FakeAnimation()
Object.assign(animation, { frames, options })
this.animations.push(animation)
return animation
}
getAnimations() { return this.animations.filter(a => !a.cancelled) }
}
const frames = [{ height: '186px' }, { height: '429px' }]
test('completed height effects are removed so content can shrink and nested records can grow', () => {
const motion = createDetailMotion(() => false)
const element = new FakeElement()
let done = 0
motion.play(element, frames, { duration: 420 }, () => done++)
element.animations[0].finish()
assert.equal(done, 1)
assert.equal(element.getAnimations().length, 0)
})
test('rapid reversal finishes the previous transition once and ignores its stale finish event', () => {
const motion = createDetailMotion(() => false)
const element = new FakeElement()
let first = 0, second = 0
motion.play(element, frames, {}, () => first++)
const old = element.animations[0]
motion.play(element, frames.toReversed(), {}, () => second++)
old.finish()
assert.equal(first, 1)
assert.equal(second, 0)
assert.equal(element.getAnimations().length, 1)
element.animations[1].finish()
assert.equal(second, 1)
assert.equal(element.getAnimations().length, 0)
})
test('explicit cancellation removes effects and calls completion only once', () => {
const motion = createDetailMotion(() => false)
const element = new FakeElement()
let done = 0
motion.play(element, frames, {}, () => done++)
motion.cancel(element)
motion.cancel(element)
element.animations[0].finish()
assert.equal(done, 1)
assert.equal(element.getAnimations().length, 0)
})
test('reduced motion skips animations and still completes', () => {
const motion = createDetailMotion(() => true)
const element = new FakeElement()
let done = 0
motion.play(element, frames, {}, () => done++)
assert.equal(done, 1)
assert.equal(element.animations.length, 0)
})
test('external cancellation and disposal release all owned animations', () => {
const motion = createDetailMotion(() => false)
const a = new FakeElement(), b = new FakeElement()
let done = 0
motion.play(a, frames, {}, () => done++)
a.animations[0].cancel()
motion.play(a, frames, {}, () => done++)
motion.play(b, frames, {}, () => done++)
motion.dispose()
assert.equal(done, 3)
assert.equal(a.getAnimations().length + b.getAnimations().length, 0)
})