【发布时间】:2023-04-02 23:32:01
【问题描述】:
我有以下父屏幕/组件Employees.tsx
import PasswordForm from '../../components/Forms/PasswordForm'
...
<View style={ stylesheet.modalWrapper }>
<PasswordForm
errorMessage={ auth.error }
isWorking={ auth.isWorking }
onCancel={ toggleModal }
onSubmit={ customSubmitHandler }
/>
</View>
<PasswordForm /> 是一个子组件,它是一个使用reduxForm 到connect 的装饰表单,以标准方式导入到父组件中。
PasswordForm.tsx
const PasswordForm = reduxForm({
form: 'password-form'
})(PasswordFormStatic)
在我的测试中,我对这个子组件<PasswordForm> 的功能不感兴趣,所以我想模拟那个组件,并确保被模拟的组件仍然存在于父组件的快照测试中(@ 987654329@)。
jest.mock() 我认为会处理这个问题。这是Employees.spec.tsx
describe('Employees Scene', () => {
let wrapper
const requestAuthToken = jest.fn()
jest.mock('../../components/Forms/PasswordForm', () => {
const mockedPasswordForm = () => null
return mockedPasswordForm
})
但是,我仍然收到Invariant Violation: Could not find "store" in either the context or props... 的错误,这确实是对孩子的抱怨。
所以看来jest.mock() 不是在嘲笑我的组件?因为它仍在尝试渲染并抱怨缺少商店。
如何在 Jest 中使用 React-Native 正确模拟组件(特别是子组件)?
【问题讨论】:
标签: reactjs unit-testing react-native jestjs