【发布时间】:2018-09-24 08:24:17
【问题描述】:
我有一个看起来像这样的原生组件:
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')
})
})
即使我的组件按预期工作,我似乎也无法通过测试。我的测试失败了:
预期的模拟函数已被调用: [“61606”] 但它没有被调用。
【问题讨论】:
-
我正在传递整个有效负载,因为我还没有向表单添加更多元素
标签: javascript reactjs react-native jestjs enzyme