【问题标题】:jest + enyzme + useRef笑话 + 酶 + useRef
【发布时间】:2021-09-07 04:54:28
【问题描述】:

我有一个看起来像这样的组件:

import React, { useRef } from 'react'
import Modal from '@example-component-library/modal'
import ModalHeader from '@example-component-library/modal/header'
import ModalFooter from '@example-component-library/modal/footer'

const ExampleModal = () => { 
  const headerRef = useRef(null)
  ...
  return (
    <Modal
      headerRef={headerRef}
      isOpen={showModal}
      header={
        <ModalHeader closeModal={handleCloseModal} headerRef={headerRef} content="Modal Header"/>
      }
      footer={<ModalFooter closeModal={handleCloseModal} />}
    >
      Modal body stuff
    </Modal>
  )
}

那我有一个测试:

it('renders as expected', () => {
    const wrapper = mount(
      <TestWrapper>
        <ExampleModal />
      </TestWrapper>
    )

    expect(wrapper.exists()).toBe(true)
  })
})

然后我有一个错误

TypeError: Cannot read property 'style' of null

      28 |
      29 |   it('renders as expected', () => {
    > 30 |     const wrapper = mount(
         |                     ^
      31 |       <TestWrapper>
      32 |         <ExampleModal />
      33 |       </TestWrapper>

如果我将 ExampleModal 道具 header 更改为:

<Modal header={<>HEADER</>} ...>

测试没有问题 - 所以我相信它与 headerRef 和我尝试过 jest.spyOn 和其他一些解决方案有关 - 但是我总是遇到同样的错误。

模态组件标记

const ModalHeader = ({ headerRef, content, ...}) => (
  <div>
    <h5 tabIndex={-1} ref={headerRef}> {content}</h5>
    ... 
  </div>
)

const Modal = ({ id, isOpen, header, headerRef, children, ...}) => {
  useEffect(() => {
    const selectorId = `#${id}`
    const selectedElement: HTMLElement = document.querySelector(selectorId)
    // set focus to the header when modal is opened
    if (isOpen && headerRef.current) {
      headerRef.current.focus()

      // React Ref wasn't working for this case
      selectedElement.style.right = '0px'
      document.body.style.overflow = 'hidden'
      setPostAnimationState(true)
    }

    if (!isOpen && document.querySelector(selectorId)) {
      // React Ref wasn't working for this case
      document.body.style.overflow = 'auto'
      selectedElement.style.right = '-768px'
      setTimeout(() => {
        setPostAnimationState(false)
      }, 400)
    }
  }, [isOpen, headerRef, id])

  return (
    <div>
      ...
      {!!header && header}
      <div className="modal-content" ref={!header ? headerRef : null}>
       {children}
      </div>
      ...
    </div>
  )
}

【问题讨论】:

  • 为什么将headerRef 传递给Modal 和ModalHeader?您在哪里监视测试中的任何内容?您可以更新您的问题以包含所有相关代码吗? (ModalHeader、ModalFooter、测试代码等)
  • @DrewReese - 用组件标记更新了我的帖子。我没有构建组件,看看它是如何工作的,以及缺少style 的位置——我只是不确定如何在不使用shallow 的情况下模拟它或如何测试它。我想使用mount

标签: reactjs jestjs react-hooks mocking enzyme


【解决方案1】:

它实际上与ref 无关,它与document.body 有关。所以这是我现在可以使用的测试:

it('renders as expected', () => {
    const wrapper = mount(
      <TestWrapper>
        <ExampleModal />
      </TestWrapper>,
      { attachTo: document.body }
    )

    expect(wrapper.exists()).toBe(true)
  })
})

【讨论】:

    猜你喜欢
    • 2019-04-05
    • 2018-06-21
    • 1970-01-01
    • 2020-06-19
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 2021-03-02
    相关资源
    最近更新 更多