admin管理员组文章数量:1417711
I am displaying a loading indicator whilst I wait for an async process to occur, however I believe my test might be missing it being displayed because the async process is too quick?
const [isLoading, setIsLoading] = useState(false);
const handleClick = async () => {
setIsLoading(true); // Show loader when waiting for url to be fetched
await getAsyncRedirectUrl().then(url => {
setIsLoading(false); // Hide loader when url fetched
window.open(url);
})
}
return (
<>
{isLoading && <LoadingIndicator data-testid="loader" />}
<button onClick={handleClick} data-testid="button">Click me</button>
</>
)
I have tried to test with the following, to no avail.
it('should show loader', async () => {
render(<MyComponent />);
await userEvent.click(screen.queryByTestId('button'));
await waitFor(() => {
expect(screen.queryByTestId('loader')).toBeInTheDocument();
});
});
The outcome of this test is a failure stating that the loader wasn't found within the DOM.
I added an artificial wait on the function with a setTimeout
of 100ms and the test passed. So that would indicate to me that the getAsyncRedirecUrl
function is simply finishing too quickly for the test to be able to register the loader as being shown.
本文标签: javascriptTest can39t find conditional render componentdisappearing too quicklyStack Overflow
版权声明:本文标题:javascript - Test can't find conditional render component, disappearing too quickly - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745274832a2651118.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论