Mock MongoDB's @realm/react for testing authentication states with jest

I have written an app in React-Native and implemented a cloud-sync mechanism using MongoDB’s Realm and @realm/react packages. All works great, but now I want to implement tests with jest to ensure my app’s ui respond correctly to changes in the state of the user, especially: Does my UI hierarchy (and the components in it) correctly update when a user signs on/off.

To do so, I’m looking for a way to mock the user object in the @realm/react package. I’m trying to stick as closely to the implementation that users will actually use, so I’m not looking to simply mock some user object to be returned from the useUser hook. I want to actually mock a user into the AppProvider context, so that the UserProvider receives it, and displays either the user-aware stack or the fallback view.

I’ve been poking around both the @realm-react and Realm-js packages in my node-modules for the past couple of days, and despite seeing al the functions to authenticate there, I haven’t been able to localize the actual implementations to see what’s inside of them and correctly mock them.

Question How does one mock either @realm/react or realm-js to manipulate the user object (either a fake one or none at all) being present in realms AppProvider context, allowing one to test the way their app responds to different authentication states?

Any guidance would be highly appreciated.

Any chance you can work with an anonymous user or create a user account dedicated to testing? That’s how we handle it for the docs and example code you see in the docs.

You can check it out in the docs-realm repo. Our approach to testing React Native apps with @realm/react has evolved a bit and is still a bit of a work in progress, but maybe you’ll find something helpful!

I’m afraid I’m no help with mocking the user object.

Hy Kyle!

Thanks for your reaction. Do I understand correctly that you suggest testing against the actual @realm/react implementation, and therefore make tests dependent on a network connection?

I’m asking this because normally, this would not be a recommended approach. Would you recommend me to mock the AppProvider, or UserProvider context instead, to be able to mimic behavior as realistically as possible, while not begin dependent on external, factors such as an internet connection?

@Kyle_Rollins I forgot to mention, my app only offers social sign-on for now. not anonymous or with email/password credentials

I understand it’s not common to write tests that depend on a network connection, but I believe it gets you closer to the real user experience than mocking does. But that’s an overall testing philosophy discussion that we probably don’t want to get too into here.

I suggested it as a workaround to mocking the user object.

@Andrew_Meyer might have better testing suggestions for you, but he’s sporadically available through the end of the year.

You could take a look at the @realm/react tests themselves. It might lead you to something that more closely aligns with your testing philosophy. I believe they use anonymous auth most of the time though.