41 lines
1.7 KiB
TypeScript
41 lines
1.7 KiB
TypeScript
import '@testing-library/jest-dom'
|
|
// RTL v13+ 默认会注册 afterEach 清理,但在 vitest + jsdom 下偶尔 cleanup 没跑,
|
|
// 导致用例之间的 DOM 残留(出现多个「粘贴代码 → 运行 → 看热点与可优化点」之类)。
|
|
// 显式 register 一次保险。
|
|
import { afterEach, vi } from 'vitest'
|
|
import { cleanup } from '@testing-library/react'
|
|
|
|
// jsdom 不带 ResizeObserver,App 用 useEffect 注册它来跟踪主区尺寸变化。
|
|
// 给一个最小的 noop stub —— 我们的测试只关心渲染产物,不真关心尺寸。
|
|
if (typeof globalThis.ResizeObserver === 'undefined') {
|
|
globalThis.ResizeObserver = class {
|
|
observe(): void {}
|
|
unobserve(): void {}
|
|
disconnect(): void {}
|
|
} as unknown as typeof ResizeObserver
|
|
}
|
|
|
|
// jsdom 24 仍未实现 PointerEvent; fireEvent.pointerDown 因此退化成裸 Event,button
|
|
// / clientX / pointerId 全丢失 —— Splitter 这类用 Pointer Events 的组件测不出来。
|
|
// 给一个最小 polyfill:继承 MouseEvent(jsdom 已支持)补 pointerId / pointerType,
|
|
// 让 RTL 派发的 PointerEventInit(button / clientX / pointerId / pointerType)真正生效。
|
|
if (typeof globalThis.PointerEvent === 'undefined') {
|
|
class PointerEventPolyfill extends MouseEvent {
|
|
public readonly pointerId: number
|
|
public readonly pointerType: string
|
|
public readonly isPrimary: boolean
|
|
constructor(type: string, params: PointerEventInit = {}) {
|
|
super(type, params)
|
|
this.pointerId = params.pointerId ?? 0
|
|
this.pointerType = params.pointerType ?? ''
|
|
this.isPrimary = params.isPrimary ?? true
|
|
}
|
|
}
|
|
globalThis.PointerEvent = PointerEventPolyfill as unknown as typeof PointerEvent
|
|
}
|
|
|
|
afterEach(() => {
|
|
cleanup()
|
|
vi.restoreAllMocks()
|
|
})
|