first, you can't just use await
keyword in useEffect
callback, it should be an async
function which would do that like this:
//Auth.js
export const Auth = ({ children }) => {
const [authStatus, setAuthStatus] = useState('waiting')
async function checkAuth(){
const status = await getAuth()
if (status) {
setAuthStatus('Authed')
}
else{
setAuthStatus('Unauthed')
}
}
useEffect(()=>{
checkAuth();
},[])
return (
<>
{authStatus === 'waiting' && <p>Loading...</p>}
{authStatus === 'Authed' && <>{Children}</>
</>
)
}
and then it's a best practice not to test implementation details like state
.
usually, you want to mock api calls
and imported modules
like getAuth
function.
so I think you should mock the getAuth
function and then return your desired value in your mock so you can test if differnet state like loading
will happen or not
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…