I have this basic code in a service that I want to test, using Jasmine, Karma and, whenever convenient, ng-mocks.
const app = initializeApp(environment.firebase);
this.auth = getAuth(app);
this.db = getDatabase();
// set up auth state listener
onAuthStateChanged(this.auth, (user) => {
// ...
});
I want to mock onAuthStateChanged in a way, that in one spec, it "returns" a mocked user instace, so it fakes being logged in, and in another spec, it returns null, meaning we are not logged in.
The problem is that these methods are not used via Dependency Injection but simply by importing:
import { Auth, getAuth, onAuthStateChanged, User } from '@angular/fire/auth';
They are declared in index.d.ts like so:
declare module "@firebase/auth" {
...
function onAuthStateChanged(auth: types.FirebaseAuth, nextOrObserver: NextOrObserver<User>, error?: ErrorFn, completed?: CompleteFn): Unsubscribe;
...
}
And because of that, I just can't figure out how to mock them. I've been wasting my past couple days to figure this out, thank you very much!