reactjs - 从 React Hook 测试中获取状态
问题描述
我在尝试使用 react-hooks-testing-library 访问 state 和 setState 时遇到问题。
我已经使用 Provider 跟踪了来自各种来源的示例,但它不起作用
支付.tsx
export const Pay = (): JSX.Element => {
const location = useLocation()
const [paymentState, setPaymentState] = useState<
'IDLE' | 'LOADING' | 'SUCCESS' | 'FAILURE'
>('IDLE')
return (
<View style={{ minHeight: '100vh' }}>
{paymentState === 'LOADING' && <PaymentLoading />}
{paymentState === 'SUCCESS' && <PaymentResult status="SUCCESS" />}
{paymentState === 'FAILURE' && <PaymentResult status="FAIL" />}
支付.test.tsx
import { renderHook, act } from '@testing-library/react-hooks'
it(`Payment Input`, async () => {
const wrapper: FC = ({ children }) => (
<SafeAreaProvider>
<Router history={history}>
<Route path="/pay">{children}</Route>
</Router>
</SafeAreaProvider>
)
const { result: payHook, waitForNextUpdate } = renderHook(() => Pay(), {
wrapper,
})
console.log(payHook.current)
})
预期 console.log(payHook.current)
{paymentState: 'IDLE', setPaymentState: [Function]}
我得到的结果 console.log(payHook.current)
{ '$$typeof': Symbol(react.element),
type: {
'$$typeof': Symbol(react.forward_ref),
render: [Function (anonymous)] { displayName: 'View' }
},
key: null,
ref: null,
props: {
style: { minHeight: '100vh' },
children: [ [Object], false, false, false ]
},
_owner: <ref *1> FiberNode {
tag: 0,
key: null,
elementType: [Function: TestComponent],
type: [Function: TestComponent],
解决方案
推荐阅读
- eslint - 如何从多个 eslint 标准扩展?
- javascript - 安装 React js 时出现此错误
- c# - 如何根据 CollectionView Items 更改元素的可见性
- python - 导入请求在 python 中显示错误,但它显示在 pip3 列表中
- wordpress - 脚本在前端工作,但不在 wp-admin 中
- jquery - Object.keys(items[0]) - 无法读取未定义的属性“0”
- google-apps-script - 如何使用 Google Apps 脚本将 .git 从 GitHub 克隆到 Google Drive?
- objective-c - 在关闭模式视图时设置 UITabBarController 索引
- c++ - Octave(IO包错误)无法运行C++编译的程序
- node.js - 从 Postgresql 到 node/express 的数据访问:resp.rows[0] 未定义