【发布时间】:2021-06-20 23:48:57
【问题描述】:
我正在尝试为此沙箱中的示例创建一个笑话测试: https://codesandbox.io/s/r3f-basic-demo-forked-297k3?file=/src/App.js
基本上我想在每个盒子上测试 onClick 功能。我已经遇到了开玩笑测试不在真正的浏览器中运行的事实。 Jest 使用 jsdom 来模拟 DOM 的必要部分。因此,我可能需要 jsdom 的 canvas 支持,但我不太确定该怎么做。
App.js
import React, { useRef, useState } from 'react'
import { Canvas, useFrame } from 'react-three-fiber'
function Box(props) {
// This reference will give us direct access to the mesh
const mesh = useRef()
// Set up state for the hovered and active state
const [hovered, setHover] = useState(false)
const [active, setActive] = useState(false)
// Rotate mesh every frame, this is outside of React without overhead
useFrame(() => {
mesh.current.rotation.x = mesh.current.rotation.y += 0.01
})
return (
<mesh
{...props}
ref={mesh}
scale={active ? [1.5, 1.5, 1.5] : [1, 1, 1]}
onClick={(e) => setActive(!active)}
onPointerOver={(e) => setHover(true)}
onPointerOut={(e) => setHover(false)}>
<boxBufferGeometry args={[1, 1, 1]} />
<meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
</mesh>
)
}
export const App = () => {
return (
<>
<Canvas>
<ambientLight intensity={0.5} />
<spotLight position={[10, 10, 10]} angle={0.15} penumbra={1} />
<pointLight position={[-10, -10, -10]} />
<Box position={[-1.2, 0, 0]} />
<Box position={[1.2, 0, 0]} />
</Canvas>
</>
)
}
【问题讨论】:
-
一个经典的 Stackoverflow 时刻,我找到了与我相同的问题,但没有答案。你能得到答案吗?
标签: reactjs testing jestjs html5-canvas react-three-fiber