跳到主要内容

使用 Jest 进行测试

由于 FlashList 不会立即渲染,而是等待底层 ScrollView 的大小(除非你指定 estimatedListSize),我们需要模拟触发 onLayout 事件。

设置

将以下行添加到你的 jest-setup.js 文件中

require("@shopify/flash-list/jestSetup");

为了确保,请检查你的 jest.config.js 文件是否包含

...
preset: 'react-native',
setupFiles: ['./jest-setup.js'],
...

示例

以下是使用 @testing-library/react-native 的示例

import React from "react";
import { render } from "@testing-library/react-native";

describe("MyFlashListComponent", () => {
it("renders items", () => {
const { getByText } = render(<MyFlashListComponent />);
const element = getByText("Title of one of the items");
// Do something with element ...
});
});