I am new at Jest and Enzyme, and am struggling to write a unit test to check if my component renders correctly when a certain state value exists.
Below is my code:
//Auth.js
export const Auth = ({ children }) => {
const [authStatus, setAuthStatus] = useState('waiting')
useEffect(()=>{
const status = await getAuth()
if (status) {
setAuthStatus('Authed')
}
else{
setAuthStatus('Unauthed')
}
},[])
return (
<>
{authStatus === 'waiting' && <p>Loading...</p>}
{authStatus === 'Authed' && <>{Children}</>
</>
)
}
In the above code, I wanted to test the Loading state when authStatus is default and I wanted to write a second test to test if the Children being passed in props are rendered when authStatus is Authed. I found a way to mock implement the state change, but that implementation is restricted to only one useEffect, as there may be multiple useEffects in the future, I do not want to go with this approach. Is there any better way to test this behavior?