我正在为一个呈现FlatList的组件编写一些单元测试,我需要模拟一些动作,比如scrollTo和scrollToEnd。
谁知道我怎么才能做到这一点?
我正在使用Jest和Enzyme进行测试。
import React form "react";
import { FlatList } from 'react-native';
import { mount } from 'enzyme';
describe("<FlatList/>", () => {
const callback = jest.fn();
it("how to simulate scroll?", () => {
const list = mount(<FlatList onEndReach={callback}/>);
//how to simulate scrool and reach the end of the list?
expect(callback).toHaveBeenCalled();
})
})从挂载对象的实例调用scrollToEnd()不起作用。
const flatList = wrapper.find(FlatList);
flatList.first().instance().scrollToEnd();Expected mock function to have been called, but it was not called.在我的模拟中,我希望在调用Flatlist.scrollToEnd()时调用回调函数。
发布于 2019-08-12 01:18:44
既然你测试的是你的组件,而不是FlatList本身,我相信你最好以某种抽象的方式思考:不要去做一些低级的事情,比如模拟滚动,只要调用道具就行了:
list.find(FlatList).prop('onEndReach')();或者使用simulate()语法糖:
list.find(FlatList).simulate('endReach');对我来说,它不仅比模拟滚动更容易,而且可维护性更好。
https://stackoverflow.com/questions/57451632
复制相似问题