mirror of
https://github.com/status-im/react-native.git
synced 2025-01-10 17:45:59 +00:00
469acbaa9d
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
216 lines
5.7 KiB
JavaScript
216 lines
5.7 KiB
JavaScript
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*/
|
|
'use strict';
|
|
|
|
const mockComponent = require.requireActual('./mockComponent');
|
|
|
|
require.requireActual('../packager/src/Resolver/polyfills/babelHelpers.js');
|
|
require.requireActual('../packager/src/Resolver/polyfills/Object.es7.js');
|
|
require.requireActual('../packager/src/Resolver/polyfills/error-guard');
|
|
|
|
global.__DEV__ = true;
|
|
|
|
global.Promise = require.requireActual('promise');
|
|
global.regeneratorRuntime = require.requireActual('regenerator-runtime/runtime');
|
|
|
|
jest
|
|
.mock('setupDevtools')
|
|
.mock('npmlog');
|
|
|
|
// there's a __mock__ for it.
|
|
jest.setMock('ErrorUtils', require('ErrorUtils'));
|
|
|
|
jest
|
|
.mock('InitializeCore')
|
|
.mock('Image', () => mockComponent('Image'))
|
|
.mock('Text', () => mockComponent('Text'))
|
|
.mock('TextInput', () => mockComponent('TextInput'))
|
|
.mock('Modal', () => mockComponent('Modal'))
|
|
.mock('View', () => mockComponent('View'))
|
|
.mock('RefreshControl', () => require.requireMock('RefreshControlMock'))
|
|
.mock('ScrollView', () => require.requireMock('ScrollViewMock'))
|
|
.mock(
|
|
'ActivityIndicator',
|
|
() => mockComponent('ActivityIndicator'),
|
|
)
|
|
.mock('ListView', () => require.requireMock('ListViewMock'))
|
|
.mock('ListViewDataSource', () => {
|
|
const DataSource = require.requireActual('ListViewDataSource');
|
|
DataSource.prototype.toJSON = function() {
|
|
function ListViewDataSource(dataBlob) {
|
|
this.items = 0;
|
|
// Ensure this doesn't throw.
|
|
try {
|
|
Object.keys(dataBlob).forEach(key => {
|
|
this.items += dataBlob[key] && (
|
|
dataBlob[key].length || dataBlob[key].size || 0
|
|
);
|
|
});
|
|
} catch (e) {
|
|
this.items = 'unknown';
|
|
}
|
|
}
|
|
|
|
return new ListViewDataSource(this._dataBlob);
|
|
};
|
|
return DataSource;
|
|
})
|
|
.mock('ensureComponentIsNative', () => () => true);
|
|
|
|
const mockEmptyObject = {};
|
|
const mockNativeModules = {
|
|
AlertManager: {
|
|
alertWithArgs: jest.fn(),
|
|
},
|
|
AppState: {
|
|
addEventListener: jest.fn(),
|
|
},
|
|
AsyncLocalStorage: {
|
|
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',
|
|
buildVersion: '0',
|
|
},
|
|
Clipboard: {
|
|
setString: jest.fn(),
|
|
},
|
|
DataManager: {
|
|
queryData: jest.fn(),
|
|
},
|
|
DeviceInfo: {
|
|
Dimensions: {
|
|
window: {
|
|
fontScale: 2,
|
|
height: 1334,
|
|
scale: 2,
|
|
width: 750,
|
|
},
|
|
},
|
|
},
|
|
FacebookSDK: {
|
|
login: jest.fn(),
|
|
logout: jest.fn(),
|
|
queryGraphPath: jest.fn((path, method, params, callback) => callback()),
|
|
},
|
|
FbRelayNativeAdapter: {
|
|
updateCLC: jest.fn(),
|
|
},
|
|
GraphPhotoUpload: {
|
|
upload: jest.fn(),
|
|
},
|
|
I18n: {
|
|
translationsDictionary: JSON.stringify({
|
|
'Good bye, {name}!|Bye message': '\u{00A1}Adi\u{00F3}s {name}!',
|
|
}),
|
|
},
|
|
ImageLoader: {
|
|
getSize: jest.fn(
|
|
(uri, success) => process.nextTick(() => success(320, 240))
|
|
),
|
|
prefetchImage: jest.fn(),
|
|
},
|
|
ImageViewManager: {
|
|
getSize: jest.fn(
|
|
(uri, success) => process.nextTick(() => success(320, 240))
|
|
),
|
|
prefetchImage: jest.fn(),
|
|
},
|
|
KeyboardObserver: {
|
|
addListener: jest.fn(),
|
|
removeListeners: jest.fn(),
|
|
},
|
|
ModalFullscreenViewManager: {},
|
|
Networking: {
|
|
sendRequest: jest.fn(),
|
|
abortRequest: jest.fn(),
|
|
addListener: jest.fn(),
|
|
removeListeners: jest.fn(),
|
|
},
|
|
SourceCode: {
|
|
scriptURL: null,
|
|
},
|
|
StatusBarManager: {
|
|
setColor: jest.fn(),
|
|
setStyle: jest.fn(),
|
|
setHidden: jest.fn(),
|
|
setNetworkActivityIndicatorVisible: jest.fn(),
|
|
setBackgroundColor: jest.fn(),
|
|
setTranslucent: jest.fn(),
|
|
},
|
|
Timing: {
|
|
createTimer: jest.fn(),
|
|
deleteTimer: jest.fn(),
|
|
},
|
|
UIManager: {
|
|
blur: jest.fn(),
|
|
createView: jest.fn(),
|
|
dispatchViewManagerCommand: jest.fn(),
|
|
focus: jest.fn(),
|
|
setChildren: jest.fn(),
|
|
manageChildren: jest.fn(),
|
|
updateView: jest.fn(),
|
|
removeSubviewsFromContainerWithID: jest.fn(),
|
|
replaceExistingNonRootView: jest.fn(),
|
|
customBubblingEventTypes: {},
|
|
customDirectEventTypes: {},
|
|
AndroidTextInput: {
|
|
Commands: {},
|
|
},
|
|
ModalFullscreenView: {
|
|
Constants: {},
|
|
},
|
|
ScrollView: {
|
|
Constants: {},
|
|
},
|
|
View: {
|
|
Constants: {},
|
|
},
|
|
},
|
|
WebSocketModule: {
|
|
connect: jest.fn(),
|
|
send: jest.fn(),
|
|
sendBinary: jest.fn(),
|
|
ping: jest.fn(),
|
|
close: jest.fn(),
|
|
addListener: jest.fn(),
|
|
removeListeners: jest.fn(),
|
|
},
|
|
};
|
|
|
|
Object.keys(mockNativeModules).forEach(module => {
|
|
try {
|
|
jest.doMock(module, () => mockNativeModules[module]); // needed by FacebookSDK-test
|
|
} catch (e) {
|
|
jest.doMock(module, () => mockNativeModules[module], {virtual: true});
|
|
}
|
|
});
|
|
|
|
jest
|
|
.doMock('NativeModules', () => mockNativeModules)
|
|
.doMock('ReactNativePropRegistry', () => ({
|
|
register: id => id,
|
|
getByID: () => mockEmptyObject,
|
|
}));
|
|
|
|
jest.doMock('requireNativeComponent', () => {
|
|
const React = require('react');
|
|
|
|
return viewName => props => React.createElement(
|
|
viewName,
|
|
props,
|
|
props.children,
|
|
);
|
|
});
|