From 469acbaa9d3f0547efbbd8a0facbcf3e7603ef20 Mon Sep 17 00:00:00 2001 From: ZauberNerd Date: Thu, 13 Apr 2017 06:17:15 -0700 Subject: [PATCH] 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 --- jest/setup.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/jest/setup.js b/jest/setup.js index 68492e651..c7c464ae4 100644 --- a/jest/setup.js +++ b/jest/setup.js @@ -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',