我正在尝试findByTestId一个IconButton (来自React本地文件),但我得到了以下错误:
Unable to find an element with testID: home-settings-button
84 | fireEvent.press(loginButton);
85 |
> 86 | const settingsButton = await findByTestId("home-settings-button");
| ^
87 |
88 | fireEvent.press(settingsButton);
89 |
at findByTestId (node_modules/@testing-library/react-native/build/helpers/makeQueries.js:95:35)
at _callee5$ (__tests__/navigator.test.js:86:32)
at tryCatch (node_modules/regenerator-runtime/runtime.js:63:40)
at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:294:22)
at Generator.next (node_modules/regenerator-runtime/runtime.js:119:21)
at tryCatch (node_modules/regenerator-runtime/runtime.js:63:40)
at invoke (node_modules/regenerator-runtime/runtime.js:155:20)
at node_modules/regenerator-runtime/runtime.js:165:13当我试图呈现视图并以getByTestId表示IconButton时,我没有收到这个错误,但是当我试图呈现AppNavigator并尝试对同一个IconButton进行findByTestId时,它就不起作用了。
例如,这起作用是:
it("renders the home screen", () => {
const { getByTestId } = render(<HomeScreen />);
getByTestId("home-settings-button");
});但这并不是:
it("test settings page's logic", async () => {
const { findByTestId } = render(<AppNavigator />);
//Login and go to settings page
const userInput = await findByTestId("login-username-input");
const passwordInput = await findByTestId("login-password-input");
const loginButton = await findByTestId("login-login-button");
fireEvent.changeText(userInput, "admin");
fireEvent.changeText(passwordInput, "admin");
fireEvent.press(loginButton);
const settingsButton = await findByTestId("home-settings-button");
fireEvent.press(settingsButton);
});这里是IconButton:
<IconButton
icon="cog"
size={30}
style={homeStyle.settings}
onPress={() => {
settings();
}}
testID={"home-settings-button"}
/>我不太明白为什么会发生这种情况,为什么我不能findByTestId这个IconButton呢?我只是试着用按钮来测试导航。
发布于 2022-12-03 15:23:22
如果使用NativeBase解决上述问题,只需在测试中将initialWindowMetrics传递给NativeBaseProvider即可。
const inset = {
frame: { x: 0, y: 0, width: 0, height: 0 },
insets: { top: 0, left: 0, right: 0, bottom: 0 },
};
<NativeBaseProvider initialWindowMetrics={inset}>
{children}
</NativeBaseProvider>;https://stackoverflow.com/questions/73177741
复制相似问题