vanGalilea / react-native-testing

This is how you should test your react-native components with Jest and React Native Testing Library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[feat. req] Provide an example of how to mock React Native Async Storage with react-native-testing

uyriq opened this issue · comments

I am using your library to store data in my React Native app. I want to test my app with Jest, but I am having trouble mocking the Async Storage module. I have read the official documentation on testing with mock, but I still don’t understand how to set up the mock and use it in my tests.

The only example I found online is mock-async-storage, but it is 5 years old and not react-native-testing.
I would really appreciate it if you could provide a simple and up-to-date example of how to mock React Native Async Storage with Jest in your repository. This would help me and other users who want to test their apps with your library.

Thank you for your time and effort.

Hey @uyriq 👋🏻 thanks for submitting this issue!

Check out this test LoginSubmission.test.tsx where we mock async storage.

You can achieve pretty much anything you'd wish with the following code:

jest.mock('@react-native-community/async-storage', () => ({
  setItem: jest.fn(),
}));

Good to have the following in mind:

  • The library's path is correct with the library you'd like to mock (i.e. '@react-native-community/async-storage' in our case)
  • The mock returns an object that is similar to what they library what've returned. If the component your testing uses other functions, such as getItem make sure you implement them with a mock implementation that suits your test case.

More info about how to mock node modules can be found in Jest's documentation

ok, I want to apologize, but I found only this version of the file in the public repo
https://github.com/vanGalilea/react-native-testing/blob/5266e2905eab84f68646199e137bf79a06388ca0/__tests__/LoginSubmission.test.tsx
it seems to be the right one, thanks, I'll make my own tests like this.! 🍒