【发布时间】:2019-01-15 22:48:44
【问题描述】:
我在使用 jest 和 enzyme 测试 react-native 中的 TextInput 变化时遇到了一些问题。
我处理用户输入的组件基本上看起来像这样(简化):
class Search extends React.PureComponent {
onSearchTextChange = input => {
// do something awesome
};
render() {
return (
<View>
<TextInput
onChangeText={debounce(this.onSearchTextChange, 800)}
/>
</View>
);
}
}
我想测试文本输入行为。这是测试现在的样子:
it('should render a text input and react to user input', done => {
const mockOnSearchTextChange = jest.fn();
Search.prototype.onSearchTextChange = mockOnSearchTextChange;
const tree = shallow(<Search />);
const textInput = tree.find('TextInput');
expect(textInput).toHaveLength(1);
textInput.simulate('changeText', { target: { value: 'test' } });
expect(mockOnSearchTextChange).not.toHaveBeenCalled();
setTimeout(() => {
expect(mockOnSearchTextChange).toHaveBeenCalledWith('test');
done();
}, 1500);
});
运行此测试时,我收到此错误消息
预期的模拟函数已被调用: [“测试”]
但它没有被调用。
所以模拟函数永远不会按预期调用。我错过了什么?
【问题讨论】:
标签: javascript react-native jestjs enzyme