Vue项目移动端集成测试时,怎么模拟手机横屏触发的事件?
我在写一个移动端Vue组件的集成测试,里面用到了横屏检测功能:
<template>
<div @orientationchange="handleOrientation">
<p>当前方向:{{ orientation }}</p>
</div>
</template>
<script setup>
import { ref } from 'vue'
const orientation = ref('portrait')
const handleOrientation = () => {
orientation.value = window.innerHeight > window.innerWidth ? 'portrait' : 'landscape'
}
</script>
测试时用Jest+Vue Test Utils写断言,发现直接修改window.orientation属性没效果,用window.matchMedia(‘(orientation: landscape)’)也没触发事件。有什么办法能在测试环境里模拟真实的横屏操作吗?
window.orientation,这个属性在大部分浏览器里都是只读的,Jest环境更不会触发真实行为。你得手动 dispatch 事件,并且 mockinnerWidth和innerHeight的值。核心思路是用
Object.defineProperty劫持window.innerHeight和innerWidth,然后手动触发orientationchange事件。Vue Test Utils 渲染完组件后,先改屏幕尺寸模拟横屏,再 dispatch 事件就行了。比如你在测试用例里这么写:
注意
writable: true要设上,不然没法重定义。另外别忘了某些逻辑可能依赖screen.orientation.angle或其他属性,如果组件里用了可以继续 defineProperty 补全。CSS的话,这种响应式布局一般靠 media query 控制样式,但你的逻辑是 JS 判断宽高比,所以重点还是 mock 尺寸 + 手动触发事件。这套方法我测过 Vue 2 和 3 都行,放心用。