I have already mocked Firebase onAuthStateChanged, so tests work flawlessly but I have this use case, in which it set's the user inside:
const [isSignedIn, setIsSignedIn] = useState<boolean>(false);
const [displayName, setDisplayName] = useState<string | null>('');
const auth = getAuth();
onAuthStateChanged(auth, (user) => {
console.log('OnAuthStateChanged USER', user);
if (user) {
setDisplayName(user.email);
setIsSignedIn(true);
} else {
setIsSignedIn(false);
setDisplayName('');
}
I know that you can easily mock what it returns, but I don't know how to deal with the "insides" of a function if that it's even possible.
I need to set the state specifically since the DrawerNavigator will show different options depending if the user is signedIn or not:
{isSignedIn && (
<>
<Drawer.Screen name='Dashboard' component={Dashboard} />
</>
)}
{!isSignedIn && (
<>
<Drawer.Screen name='Welcome' component={Welcome} />
<Drawer.Screen name='CreateAccount' component={CreateAccount} />
<Drawer.Screen name='Login' component={Login} />
</>
)}
I can test the functionality but only on the default state as it is initialized, how can I make this work? Or is there any other change of how I set the state in which this could be testable?
Here it is also how I'm mocking firebase/auth module right now:
/**
* Firebase
*/
jest.mock('firebase/auth', () => {
return {
getAuth: () => jest.fn(),
onAuthStateChanged: () => jest.fn(),
};
});