我有以下的父级屏幕/组件 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>
的功能不感兴趣,所以我想模拟该组件,并确保模拟的组件仍然出现在父组件(Employees.tsx
)的快照测试中。
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模拟组件(特别是子组件)?
jest.fn()
即可? - Aleski