Javascript 如何测试回调参数本机响应

Javascript 如何测试回调参数本机响应,javascript,reactjs,react-native,jestjs,enzyme,Javascript,Reactjs,React Native,Jestjs,Enzyme,我有一个react本机组件,看起来像: export class Estimate extends PureComponent { setRef = (ref) => { this.estimateForm = ref } render () { const { onPress } = this.props return ( <Form name="estimateForm" ref={

我有一个react本机组件,看起来像:

export class Estimate extends PureComponent {
  setRef =
    (ref) => {
      this.estimateForm = ref
    }

  render () {
    const { onPress } = this.props
    return (
      <Form
        name="estimateForm"
        ref={this.setRef}
      >
        <Actions style={{ alignItems: 'flex-end' }}>
          <Button
            type="text"
            style={{ marginBottom: -em(0.5) }}
            onPress={() => onPress(this.estimateForm.values)}
            title={t('estimateShippingAndTax')}
          />
          <TextInput
            placeholder={t('postalCode')}
            name="estimate"
            style={{ width: 100 }}
            validate="postalCode"
          />
        </Actions>
      </Form>
    )
  }
}
let obj
let onPressFunction
describe('Estimate', () => {
  beforeAll(() => {
    onPressFunction = jest.fn()
    obj = shallow(<Estimate onPress={onPressFunction} />)
  })

  test('Text gets returned with onPress', () => {
    console.log(obj.html())
    const value = { values: { estimate: '61606' } }
    obj.instance().setRef(value)
    obj.find('Button').first().simulate('onPress')
    expect(onPressFunction).toHaveBeenCalledWith('61606')
  })
})
导出类组件{
setRef=
(参考)=>{
this.estimateForm=ref
}
渲染(){
const{onPress}=this.props
返回(
onPress(this.estimateForm.values)}
title={t('estimateShippingAndTax')}
/>
)
}
}
我的玩笑/酶测试如下所示:

export class Estimate extends PureComponent {
  setRef =
    (ref) => {
      this.estimateForm = ref
    }

  render () {
    const { onPress } = this.props
    return (
      <Form
        name="estimateForm"
        ref={this.setRef}
      >
        <Actions style={{ alignItems: 'flex-end' }}>
          <Button
            type="text"
            style={{ marginBottom: -em(0.5) }}
            onPress={() => onPress(this.estimateForm.values)}
            title={t('estimateShippingAndTax')}
          />
          <TextInput
            placeholder={t('postalCode')}
            name="estimate"
            style={{ width: 100 }}
            validate="postalCode"
          />
        </Actions>
      </Form>
    )
  }
}
let obj
let onPressFunction
describe('Estimate', () => {
  beforeAll(() => {
    onPressFunction = jest.fn()
    obj = shallow(<Estimate onPress={onPressFunction} />)
  })

  test('Text gets returned with onPress', () => {
    console.log(obj.html())
    const value = { values: { estimate: '61606' } }
    obj.instance().setRef(value)
    obj.find('Button').first().simulate('onPress')
    expect(onPressFunction).toHaveBeenCalledWith('61606')
  })
})
let obj
let on Press函数
描述('估计',()=>{
以前(()=>{
onPressFunction=jest.fn()
obj=浅()
})
test('文本通过onPress'返回,()=>{
console.log(obj.html())
常量值={values:{estimate:'61606'}}
obj.instance().setRef(值)
obj.find('Button').first().simulate('onPress'))
expect(onpress函数)。已通过('61606')调用
})
})
即使我的组件按预期工作,我似乎也无法通过测试。我的测试失败,原因是:

预期已使用以下函数调用模拟函数: ["61606"]
但是它没有被调用。

你的酶测试应该模拟
press
事件,而不是
onPress
事件。事件名称为“按”

查看您的标记,这是一个React本机项目,建议尝试直接调用该方法,而不是依赖
simulate

obj.find('Button').first().props().onPress()

你的酶测试应该模拟
press
事件,而不是
onPress
事件。事件名称为“按”

查看您的标记,这是一个React本机项目,建议尝试直接调用该方法,而不是依赖
simulate

obj.find('Button').first().props().onPress()

谢谢,就是这样:)谢谢,就是这样:)我正在传递整个有效负载,因为我还没有向表单添加更多元素。我正在传递整个有效负载,因为我还没有向表单添加更多元素