gpt4 book ai didi

reactjs - 使用 enzyme 测试重组 - 访问 Prop 问题

转载 作者:行者123 更新时间:2023-11-28 19:54:18 25 4
gpt4 key购买 nike

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

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

这是我的功能组件的示例,我将调用 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

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

我哪里错了? 谢谢!

最佳答案

您的测试加载增强 组件,而不是底层 UserDetails 组件。因此,您在 console.log 中看到的是最外层组件 (compose) 的 props。您可以使用 tree.find(UserDetails) 获取内部组件。

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

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

并且以这种方式找到

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

enzymefind 并不总能按照您预期的方式找到您想要的东西。试验 find 提供的各种选项。

关于reactjs - 使用 enzyme 测试重组 - 访问 Prop 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49160884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com