使用酶测试React Native FlatList

Enu*_*uff 4 javascript jest react-native enzyme

我正在尝试使用酶来测试React Native 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)
    })
})
Run Code Online (Sandbox Code Playgroud)

但这就是我得到的:

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)
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?什么是正确的测试方法?

jsf*_*low 6

就目前而言,一旦您超越了非常简单的浅层渲染和快照组合,似乎就不可能/非常好用酶来测试React Native。

我发现使用Test Renderer更加可靠,可以渲染出FlatList,遍历并调用动作

另外,测试上述内容将非常棘手,到目前为止,我一直在检查是否使用间谍程序调用了正确的API,而不是像上面那样实际测试功能。

发生此错误的原因scrollTo是,例如,尚未正确ScrollView模拟可jest.mock用于破解的模拟。请参阅:本期