diff --git a/website/docs/advanced/Jest-integration.md b/website/docs/advanced/Jest-integration.md index 09452307..f1d9aff2 100644 --- a/website/docs/advanced/Jest-integration.md +++ b/website/docs/advanced/Jest-integration.md @@ -4,21 +4,24 @@ title: Jest integration sidebar_label: Jest integration --- - -Async Storage module is tightly coupled with its `NativeModule` part - it needs a running React Native application to work properly. In order to use it in tests, you have to provide its separate implementation. Follow these steps to add a mocked `Async Storage` module. +Async Storage module is tightly coupled with its `NativeModule` part - it needs +a running React Native application to work properly. In order to use it in +tests, you have to provide its separate implementation. Follow these steps to +add a mocked `Async Storage` module. ## Using Async Storage mock You can use one of two ways to provide mocked version of `AsyncStorage`: -### With __mocks__ directory +### With **mocks** directory -1. In your project root directory, create `__mocks__/@react-native-async-storage` directory. +1. In your project root directory, create + `__mocks__/@react-native-async-storage` directory. 2. Inside that folder, create `async-storage.js` file. 3. Inside that file, export `Async Storage` mock. ```javascript -export default from '@react-native-async-storage/async-storage/jest/async-storage-mock' +export default from '@react-native-async-storage/async-storage/jest/async-storage-mock'; ``` ### With Jest setup file @@ -34,20 +37,24 @@ export default from '@react-native-async-storage/async-storage/jest/async-storag 2. Inside your setup file, set up Async Storage mocking: ```javascript -import mockAsyncStorage from '@react-native-async-storage/async-storage/jest/async-storage-mock'; - -jest.mock('@react-native-async-storage/async-storage', () => mockAsyncStorage); +jest.mock('@react-native-async-storage/async-storage', () => + require('@react-native-async-storage/async-storage/jest/async-storage-mock') +); ``` + ## Testing with mock -Each public method available from `Async Storage` is [a mock function](https://jestjs.io/docs/en/mock-functions), that you can test for certain condition, for example, if `.getItem` has been called with a specific arguments: +Each public method available from `Async Storage` is +[a mock function](https://jestjs.io/docs/en/mock-functions), that you can test +for certain condition, for example, if `.getItem` has been called with a +specific arguments: ```javascript it('checks if Async Storage is used', async () => { await asyncOperationOnAsyncStorage(); expect(AsyncStorage.getItem).toBeCalledWith('myKey'); -}) +}); ``` ## Overriding Mock logic @@ -61,9 +68,11 @@ import AsyncStorageMock from '@react-native-async-storage/async-storage/jest/asy AsyncStorageMock.multiGet = jest.fn(([keys], callback) => { // do something here to retrieve data callback([]); -}) +}); export default AsyncStorageMock; ``` -You can [check its implementation](https://github.com/react-native-async-storage/async-storage/blob/master/jest/async-storage-mock.js) to get more insight into methods signatures. +You can +[check its implementation](https://github.com/react-native-async-storage/async-storage/blob/master/jest/async-storage-mock.js) +to get more insight into methods signatures.