使用酶测试重组 - 访问道具问题

Testing recompose with enzyme - accessing props issue

所以我搜索了一个答案,但尚未找到适合我的用例的答案。我对使用 Jest/Enzyme 进行测试有点陌生,对使用重组进行测试也很陌生。

我不知道如何测试我的功能组件,它是使用重构创建的。我似乎无法弄清楚如何在我的测试中访问道具(状态和处理函数)。

这是我的功能组件的示例,我将调用 UserDetails

const UserDetails = ({
 userInfo,
 onUserInfoUpdate,
 className,
 error,
 title,
 primaryBtnTitle,
 submit,
 secondaryBtnTitle,
 secondaryBtnFunc,
 ...props
}) => (
 <div className={className}>
   <div className="user-details-body">
     <Section title="User details">
       <TextInput
         caption="First Name"
         value={userInfo.first}
         onChange={onUserInfoUpdate('first')}
         name="first-name"
         min="1"
         max="30"
         autoComplete="first-name"
       />
       <TextInput
         caption="Last Name"
         value={userInfo.last}
         onChange={onUserInfoUpdate('last')}
         name="last-name"
         min="1"
         max="30"
         autoComplete="last-name"
       />
     </Section>
   </div>

   <div className="errorBar">
     {error && <Alert type="danger">{error}</Alert>}
   </div>

   <ActionBar>
     <ButtonGroup>
       <Button type="secondary" onClick={secondaryBtnFunc}>
         {secondaryBtnTitle}
       </Button>
       <Button type="primary" onClick={submit}>
         {primaryBtnTitle}
       </Button>
     </ButtonGroup>
   </ActionBar>
 </div>

这是我的 index.js 文件的示例代码,它将我的 withState 和 withHandlers 组合到我的组件中:

import UserDetails from './UserDetails'
import { withState, withHandlers, compose } from 'recompose'

export default compose(
  withState('error', 'updateError', ''),
  withState('userInfo', 'updateUserInfo', {
    first: '',
    last: '',
  }),
  withHandlers({
    onUserInfoUpdate: ({ userInfo, updateUserInfo }) => key => e => {
      e.preventDefault()
      updateCardInfo({
        ...cardInfo,
        [key]: e.target.value,
      })
    },
    submit: ({ userInfo, submitUserInfo }) => key => e => {
      e.preventDefault()
      submitUserInfo(userInfo) 
      //submitUserInfo is a graphQL mutation
      })
    }  
  }) 
)

现在我正在尝试为此组件编写测试。我导入包含组件的索引文件以创建 HOC。

import React from 'react'
import renderer from 'react-test-renderer'
import { mount, shallow } from 'enzyme' 
import UserDetails from './'

describe('UserDetails Element', () => {
  it('test', () => {
    const tree = mount( <UserDetails /> )
  console.log(tree.props());
  })
})

控制台日志在终端中给了我这个

{ children: 
  { '$$typeof': Symbol(react.element),
    type: 
      { [Function: WithState]
        displayName: 'withState(withState(withHandlers((UserDetails)))))' },
      key: null,
      ref: null,
      props: {},
      _owner: null,
      _store: {} } }

如果我 console.log tree.props('userInfo') 我得到相同的输出。如果我 console.log tree.prop('userInfo')tree.props().userInfo

我得到 undefined

此外,当我尝试在测试文件中使用我的处理程序时,我收到一条错误消息,指出该方法未定义。

我哪里错了? 谢谢!

您的测试加载 enhanced 组件,而不是底层 bare UserDetails 组件。所以你看到的 console.log 是最外层组件 (compose) 的 props。您可以使用 tree.find(UserDetails) 获取内部组件。

为此,您必须在测试中导入这两个文件:

import UserDetails from './'
import BareUserDetails from './UserDetails'

find这样

const tree = mount( <UserDetails /> )
const innerDetails = tree.find(BareUserDetails);

enzymefind 并不总能按您预期的方式找到您想要的内容。尝试 find 提供的各种选项。