react-native-macos/jest/setup.js

329 строки
9.0 KiB
JavaScript
Исходник Обычный вид История

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
'use strict';
const MockNativeMethods = jest.requireActual('./MockNativeMethods');
const mockComponent = jest.requireActual('./mockComponent');
jest.requireActual('../Libraries/polyfills/Object.es7.js');
jest.requireActual('../Libraries/polyfills/error-guard');
global.__DEV__ = true;
global.Promise = jest.requireActual('promise');
global.regeneratorRuntime = jest.requireActual('regenerator-runtime/runtime');
global.requestAnimationFrame = function(callback) {
return setTimeout(callback, 0);
};
global.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
jest.mock('setupDevtools');
// there's a __mock__ for it.
jest.setMock('ErrorUtils', require('ErrorUtils'));
jest
.mock('InitializeCore', () => {})
.mock('Image', () => mockComponent('Image'))
.mock('Text', () => mockComponent('Text', MockNativeMethods))
.mock('TextInput', () => mockComponent('TextInput'))
.mock('Modal', () => mockComponent('Modal'))
.mock('View', () => mockComponent('View', MockNativeMethods))
.mock('RefreshControl', () => jest.requireMock('RefreshControlMock'))
.mock('ScrollView', () => jest.requireMock('ScrollViewMock'))
.mock('ActivityIndicator', () => mockComponent('ActivityIndicator'))
.mock('AnimatedImplementation', () => {
const AnimatedImplementation = jest.requireActual('AnimatedImplementation');
const oldCreate = AnimatedImplementation.createAnimatedComponent;
AnimatedImplementation.createAnimatedComponent = function(
Component,
defaultProps,
) {
const Wrapped = oldCreate(Component, defaultProps);
Wrapped.__skipSetNativeProps_FOR_TESTS_ONLY = true;
return Wrapped;
};
return AnimatedImplementation;
})
.mock('ReactNative', () => {
const ReactNative = jest.requireActual('ReactNative');
const NativeMethodsMixin =
ReactNative.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
.NativeMethodsMixin;
Object.assign(NativeMethodsMixin, MockNativeMethods);
Object.assign(ReactNative.NativeComponent.prototype, MockNativeMethods);
return ReactNative;
})
.mock('ensureComponentIsNative', () => () => true);
const mockNativeModules = {
AccessibilityInfo: {
addEventListener: jest.fn(),
announceForAccessibility: jest.fn(),
fetch: jest.fn(),
isBoldTextEnabled: jest.fn(),
isGrayscaleEnabled: jest.fn(),
isInvertColorsEnabled: jest.fn(),
isReduceMotionEnabled: jest.fn(),
isReduceTransparencyEnabled: jest.fn(),
isScreenReaderEnabled: jest.fn(),
removeEventListener: jest.fn(),
setAccessibilityFocus: jest.fn(),
},
AlertManager: {
alertWithArgs: jest.fn(),
},
AppState: {
addEventListener: jest.fn(),
removeEventListener: 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',
getConstants() {
return {
appVersion: '0',
buildVersion: '0',
};
},
},
Clipboard: {
setString: jest.fn(),
},
DataManager: {
queryData: jest.fn(),
},
DeviceInfo: {
Dimensions: {
window: {
fontScale: 2,
height: 1334,
scale: 2,
width: 750,
},
screen: {
fontScale: 2,
height: 1334,
scale: 2,
width: 750,
},
},
},
FacebookSDK: {
login: jest.fn(),
logout: jest.fn(),
queryGraphPath: jest.fn((path, method, params, callback) => callback()),
},
GraphPhotoUpload: {
upload: jest.fn(),
},
I18n: {
translationsDictionary: JSON.stringify({
'Good bye, {name}!|Bye message': '\u{00A1}Adi\u{00F3}s {name}!',
}),
},
ImageLoader: {
getSize: jest.fn(url => Promise.resolve({width: 320, height: 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(),
},
Linking: {
openURL: jest.fn(),
canOpenURL: jest.fn(() => Promise.resolve(true)),
- Add openSettings method to Linking module (#23965) Summary: This will create a cross-platform and safe way to programmatically open the app's settings into the iOS /Android Settings app. Right now it's possible to open the app's settings, but _**only for iOS**_ via `Linking.openURL("app-settings:")` To do the same for Android, you need to either create NodeModule or install a dependency such as [react-native-open-settings](https://github.com/lunarmayor/react-native-open-settings). Why this new method is useful: since Android 6, app permissions work similar to iOS. It's granular and it's requested in the app runtime. https://developer.android.com/guide/topics/permissions/overview#runtime_requests_android_60_and_higher > If the device is running Android 6.0 (API level 23) or higher, and the app's targetSdkVersion is 23 or higher, the user isn't notified of any app permissions at install time. Your app must ask the user to grant the dangerous permissions at runtime. When your app requests permission, the user sees a system dialog telling the user which permission group your app is trying to access. The dialog includes a Deny and Allow button. Thus, if the user checks the **"Never ask again box"** and taps **"Deny"**, for some specific permission, the only way to change the permission is going to the Android Setting app. And that's where this new method becomes useful. It'll allow our apps to programmatically send the the user to settings app. Also, `openSettings()` doesn't receive a parameter to redirect to specific subsections of the Settings app because there's no public API to do it on iOS ([there's a way to have, via private API, but it causes the app to get rejected.](https://github.com/mauron85/cordova-plugin-background-geolocation/issues/394)) Create `Linking.openSettings()` for iOS and Android; [General] [add ] - Add openSetting method to Linking module Pull Request resolved: https://github.com/facebook/react-native/pull/23965 Differential Revision: D14502910 Pulled By: cpojer fbshipit-source-id: d27d62282b9df499845c78d983d3b6936c36ea39
2019-03-18 18:03:05 +03:00
openSettings: jest.fn(),
addEventListener: jest.fn(),
getInitialURL: jest.fn(() => Promise.resolve()),
removeEventListener: jest.fn(),
sendIntent: jest.fn(),
},
LocationObserver: {
addListener: jest.fn(),
getCurrentPosition: jest.fn(),
removeListeners: jest.fn(),
requestAuthorization: jest.fn(),
setConfiguration: jest.fn(),
startObserving: jest.fn(),
stopObserving: jest.fn(),
},
ModalFullscreenViewManager: {},
NetInfo: {
fetch: jest.fn(() => Promise.resolve()),
getConnectionInfo: jest.fn(() => Promise.resolve()),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
isConnected: {
fetch: jest.fn(() => Promise.resolve()),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
},
isConnectionExpensive: jest.fn(() => Promise.resolve()),
},
Networking: {
sendRequest: jest.fn(),
abortRequest: jest.fn(),
addListener: jest.fn(),
removeListeners: jest.fn(),
},
PushNotificationManager: {
presentLocalNotification: jest.fn(),
scheduleLocalNotification: jest.fn(),
cancelAllLocalNotifications: jest.fn(),
removeAllDeliveredNotifications: jest.fn(),
getDeliveredNotifications: jest.fn(callback => process.nextTick(() => [])),
removeDeliveredNotifications: jest.fn(),
setApplicationIconBadgeNumber: jest.fn(),
getApplicationIconBadgeNumber: jest.fn(callback =>
process.nextTick(() => callback(0)),
),
cancelLocalNotifications: jest.fn(),
getScheduledLocalNotifications: jest.fn(callback =>
process.nextTick(() => callback()),
),
requestPermissions: jest.fn(() =>
Promise.resolve({alert: true, badge: true, sound: true}),
),
abandonPermissions: jest.fn(),
checkPermissions: jest.fn(callback =>
process.nextTick(() => callback({alert: true, badge: true, sound: true})),
),
getInitialNotification: jest.fn(() => Promise.resolve(null)),
addListener: jest.fn(),
removeListeners: jest.fn(),
},
SourceCode: {
scriptURL: null,
},
StatusBarManager: {
add missing jest mock in StatusBarManager Summary: fix issue https://github.com/facebook/react-native/issues/11701. Pass current ci <!-- Does this PR require a documentation change? Create a PR at https://github.com/facebook/react-native-website and add a link to it here. --> <!-- Required. Help reviewers and the release process by writing your own release notes. See below for an example. --> [GENERAL] [BUGFIX] [JEST] - add missing jest mock in StatusBarManager <!-- **INTERNAL and MINOR tagged notes will not be included in the next version's final release notes.** CATEGORY [----------] TYPE [ CLI ] [-------------] LOCATION [ DOCS ] [ BREAKING ] [-------------] [ GENERAL ] [ BUGFIX ] [ {Component} ] [ INTERNAL ] [ ENHANCEMENT ] [ {Filename} ] [ IOS ] [ FEATURE ] [ {Directory} ] |-----------| [ ANDROID ] [ MINOR ] [ {Framework} ] - | {Message} | [----------] [-------------] [-------------] |-----------| EXAMPLES: [IOS] [BREAKING] [FlatList] - Change a thing that breaks other things [ANDROID] [BUGFIX] [TextInput] - Did a thing to TextInput [CLI] [FEATURE] [local-cli/info/info.js] - CLI easier to do things with [DOCS] [BUGFIX] [GettingStarted.md] - Accidentally a thing/word [GENERAL] [ENHANCEMENT] [Yoga] - Added new yoga thing/position [INTERNAL] [FEATURE] [./scripts] - Added thing to script that nobody will see --> Closes https://github.com/facebook/react-native/pull/19017 Differential Revision: D8014888 Pulled By: hramos fbshipit-source-id: 1be29f9643cf0faa53ad64f79e11d48b654a97b3
2018-05-15 23:38:30 +03:00
HEIGHT: 42,
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: {
AndroidViewPager: {
Commands: {
setPage: jest.fn(),
setPageWithoutAnimation: jest.fn(),
},
},
blur: jest.fn(),
createView: jest.fn(),
dispatchViewManagerCommand: jest.fn(),
focus: jest.fn(),
getViewManagerConfig: jest.fn(name => {
if (name === 'AndroidDrawerLayout') {
return {
Constants: {
DrawerPosition: {
Left: 10,
},
},
};
}
}),
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: {},
},
},
BlobModule: {
BLOB_URI_SCHEME: 'content',
BLOB_URI_HOST: null,
addNetworkingHandler: jest.fn(),
enableBlobSupport: jest.fn(),
disableBlobSupport: jest.fn(),
createFromParts: jest.fn(),
sendBlob: jest.fn(),
release: jest.fn(),
},
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);
jest.doMock('requireNativeComponent', () => {
const React = require('react');
return viewName =>
class extends React.Component {
render() {
return React.createElement(viewName, this.props, this.props.children);
}
};
});