0

我想为使用React-Ace Editor Component的组件编写测试。我必须向这个编辑器提供参考。但它总是等于 null(null 是 ref 的默认值)。我不知道如何解决这个问题,因为我不知道带有 refs 的组件如何在测试环境中呈现。我发现解决这个问题的唯一方法是模拟 useRef 并从中返回一个假对象。但这不是我想要的。对于测试,我使用react-testing-library

// Component
const TestComponent = () => {
  const ref = useRef(null)
  
  // always null
  console.log(ref)

  return <AceEditor ref={ref}/>
}

// Test
test(() => {
  const rendered = render(<TestComponent />)
})
4

0 回答 0