【发布时间】:2021-06-23 06:40:40
【问题描述】:
我有一个 React 组件,直到 fetchUser 状态不是 loading 时才会呈现。我的测试失败,因为该组件没有第一次呈现,因此找不到page loaded 文本。如何在我的测试中模拟我的 const [fetchStatusLoading, setFetchStatusLoading] = useState(false),以便呈现页面而不是 loading 文本?
const fetchUser = useSelector((state) => (state.fetchUser));
const [fetchStatusLoading, setFetchStatusLoading] = useState(true);
useEffect(() => {
setFetchStatusLoading(fetchUser .status === 'loading');
}, [fetchStatusLoading, fetch.status]);
useEffect(() => {
// this is a redux thunk dispatch that sets fetch.status to loading/succeeded
dispatch(fetchAPI({ username }));
}, []);
if(fetchStatusLoading) return 'loading..';
return (<>page loaded</>)
// test case fails
expect(getByText('page loaded')).toBeInTheDocument();
【问题讨论】:
-
您是否尝试将您的
expect包装在waitFor块中?
标签: javascript reactjs unit-testing jestjs react-testing-library