测试使用笑话和测试库调用内部自定义钩子的反应本机组件
我第一次尝试为我的 React Native 应用程序编写测试。我有一个组件 LoginForm
,它在内部调用一个钩子 useLoginForm
,其编写方式如下:
//LoginForm.js
import { useLoginForm } from './hooks';
//more imports ...
export default function LoginForm() {
//other variables ...
const {
//more functions and state variables...
isLoading,
login
} = useLoginForm()
return (
//more code...
<TouchableOpacity
onPress={login}
>
{isLoading ?
<ActivityIndicator accessibilityHint={"login-loading"}/>
:
<Text>
confirm
</Text>
}
</TouchableOpacity>
)
}
这个想法是,当我按下确认按钮时,它会将文本替换为ActivityIndicator 并且它停止在登录功能内加载 因为它成功了或者发生了错误。
钩子内部:
//useLoginForm.js
import { signInRequest } from "../api"
//...
export const useLoginForm = () => {
const [isLoading, updateIsLoading] = React.useState(false)
// ...
const login = async () => {
updateIsLoading(true)
await signInRequest(email, password)
.then(data => {
// do something ...
updateIsLoading(false)
}
})
.catch(error => {
updateIsLoading(false)
// ...
})
}
}
此外,我的文件当前的结构如下
─src
└───components
└───LoginForm
├───hooks
│ ├───useLoginForm.js
│ └───index.js
├───LoginForm.js
├───LoginForm.test.js
└───index.js
,其中 index.js
包含以下行:
// hooks/index.js
export { useLoginForm } from "./useLoginForm";
我正在使用 jest
和 @testing-library/react -本机
。
如果需要,我可以包括测试,但目前它只包括一些非常基本的断言和事件,例如更改输入字段和按确认按钮
有一些事情我无法理解:
- 如何访问
登录
函数来检查它是否被调用?
fireEvent.press(getByText("confirm"))
await waitForElementToBeRemoved(() => getByAccessibilityHint("login-loading"))
// the following line is what I am trying to accomplish
expect(login).toHaveBeenCalled()
如果我要使用此文件结构ONLY模拟此
login
函数,我应该怎么做?我尝试了在 StackOverflow 上找到的一些方法,但都没有成功。我是否可以使用此文件结构正确导出和导入? (我最近重构了我的应用程序,所以我对这个文件结构仍然有点不确定)
I am trying to write tests for my react native application for the first time. I have a component LoginForm
that calls a hook useLoginForm
inside of it, which is written like so:
//LoginForm.js
import { useLoginForm } from './hooks';
//more imports ...
export default function LoginForm() {
//other variables ...
const {
//more functions and state variables...
isLoading,
login
} = useLoginForm()
return (
//more code...
<TouchableOpacity
onPress={login}
>
{isLoading ?
<ActivityIndicator accessibilityHint={"login-loading"}/>
:
<Text>
confirm
</Text>
}
</TouchableOpacity>
)
}
The idea is that when I press the confirm button it replaces the text by an ActivityIndicator and it stops loading inside the login function, either
because it was successful or an error occurred.
Inside the hook:
//useLoginForm.js
import { signInRequest } from "../api"
//...
export const useLoginForm = () => {
const [isLoading, updateIsLoading] = React.useState(false)
// ...
const login = async () => {
updateIsLoading(true)
await signInRequest(email, password)
.then(data => {
// do something ...
updateIsLoading(false)
}
})
.catch(error => {
updateIsLoading(false)
// ...
})
}
}
Also, my files are currently structured like this
─src
└───components
└───LoginForm
├───hooks
│ ├───useLoginForm.js
│ └───index.js
├───LoginForm.js
├───LoginForm.test.js
└───index.js
Where index.js
contains the following line:
// hooks/index.js
export { useLoginForm } from "./useLoginForm";
I am using jest
and @testing-library/react-native
.
I can include the test if neccessary, but at the moment it only includes some very basic assertions and events like changing input fields and pressing the confirm button
There are some things I am having trouble in understanding:
- How can I access the
login
function to check if it was called?
fireEvent.press(getByText("confirm"))
await waitForElementToBeRemoved(() => getByAccessibilityHint("login-loading"))
// the following line is what I am trying to accomplish
expect(login).toHaveBeenCalled()
If I were to mock ONLY this
login
function with this file structure, how should I do it? I've tried several things I found across StackOverflow and had no success with neither of them.Am I exporting and importing correctly with this file structure? (I recently refactored my application, so I am still a bit unsure about this file structure)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论