2017-10-21 140 views
0

我试图用酶来测试一个阵营本地FlatList。我要检查如果有合适的函数被调用时,列表到达终点:测试阵营本地FlatList与酶

import { listIsAtTheEnd } from "./actions"; 
import { mount } from "enzyme"; 
jest.mock("./actions"); 


describe("Main page",() => { 
    if("Calls listIsAtTheEnd when FlatList reaches the end",() => { 
     var app = mount(<App />); 
     var container = app.find("FlatListContainer"); 
     var fList = container.childAt(0); 
     fList.instance().scrollToEnd(); 

     expect(listIsAtTheEnd.mock.calls).toHaveLength(1) 
    }) 
}) 

,但是这是我得到:

TypeError: this._scrollRef.scrollTo is not a function 

    at VirtualizedList.scrollToEnd (node_modules/react-native/Libraries/Lists/VirtualizedList.js:219:17) 
    at FlatList.scrollToEnd (node_modules/react-native/Libraries/Lists/FlatList.js:544:141) 
    at Object.<anonymous> (src/components/forumPage/__tests__/forumPageTests.js:81:21) 
    at tryCallTwo (node_modules/promise/lib/core.js:45:5) 
    at doResolve (node_modules/promise/lib/core.js:200:13) 
    at new Promise (node_modules/promise/lib/core.js:66:3) 

我在做什么错?什么是测试这个的正确方法?

回答

3

因为它的立场似乎没有可能/非常好的测试阵营与原住民酶一旦你超越了一个非常简单的浅渲染和快照组合。

我使用Test Renderer更为可靠呈现出有点像FlatList,穿越它,并调用行动

另外的测试发现,上面将是棘手的,到目前为止,我一直在检查正确使用间谍来调用API,而不是像上面那样实际测试功能。

这个错误发生,虽然因为scrollTo还没有被正确地嘲笑在ScrollView模拟,你可以用jest.mock入侵例如。请参阅:this issue

+0

同意。我试着用'react-native-mock-render'来尝试使用'酶'来帮助模拟原生组件,但是我没有发现模拟的组件已经足够完成断言或快照,并最终恢复为'jest'和'react-测试renderer'。这是一个移动的目标。 React Native获得像FlatList这样的新控件,如果你要对它做任何体面的测试,它们都需要被正确地嘲笑。不是一件容易的事。 –