Skip to content

Commit

Permalink
docs: update for Jest 29 (react-native-async-storage#865)
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 authored Nov 9, 2022
1 parent 409b150 commit 1736e37
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions website/docs/advanced/Jest-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.

0 comments on commit 1736e37

Please sign in to comment.