mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-05-20 16:04:32 +08:00
fix mock for AsyncLocalStorage
Summary: The current mock of `AsyncLocalStorage` seems to mock parts of the `AsyncStorage` API that can't be found on the `AsyncLocalStorage` object itself and therefore it doesn't work as expected. What it should do is mock the API of `AsyncLocalStorage` which is a `NativeModule` so that the `AsyncStorage` module can be used in tests and it will require the mocked methods. In order to enable behaviour in unit tests it is possible to replace or overwrite the mock implementation, see the [jest guide on mocks](https://facebook.github.io/jest/docs/mock-functions.html). By doing something similar to: ```javascript import { NativeModules } from 'react-native'; const { AsyncLocalStorage } = NativeModules; // mock 'multiGet', in order to allow 'AsyncStorage.getItem('myKey')' AsyncLocalStorage.multiGet.mockImplementationOnce((keys, callback) => { callback(null, [['myKey', 'myValue']]); }); // execute unit tests for code that makes use of 'AsyncStorage' ``` Closes https://github.com/facebook/react-native/pull/13433 Differential Revision: D4883389 Pulled By: javache fbshipit-source-id: 92a0ee94480b3022acc748e306ee2d4ee7a9869d
This commit is contained in:
committed by
Facebook Github Bot
parent
0d58669c81
commit
469acbaa9d
@@ -72,10 +72,12 @@ const mockNativeModules = {
|
||||
addEventListener: jest.fn(),
|
||||
},
|
||||
AsyncLocalStorage: {
|
||||
clear: jest.fn(),
|
||||
getItem: jest.fn(),
|
||||
removeItem: jest.fn(),
|
||||
setItem: jest.fn(),
|
||||
multiGet: jest.fn((keys, callback) => process.nextTick(() => callback(null, []))),
|
||||
multiSet: jest.fn((entries, callback) => process.nextTick(() => callback(null))),
|
||||
multiRemove: jest.fn((keys, callback) => process.nextTick(() => callback(null))),
|
||||
multiMerge: jest.fn((entries, callback) => process.nextTick(() => callback(null))),
|
||||
clear: jest.fn(callback => process.nextTick(() => callback(null))),
|
||||
getAllKeys: jest.fn(callback => process.nextTick(() => callback(null, []))),
|
||||
},
|
||||
BuildInfo: {
|
||||
appVersion: '0',
|
||||
|
||||
Reference in New Issue
Block a user