Skip to content

Commit

Permalink
Revert D66599197: Delete tests migrated to Fantom and unnecessary moc…
Browse files Browse the repository at this point in the history
…ks for FabricUIManager, DOM, etc.

Differential Revision:
D66599197

Original commit changeset: 33822588c217

Original Phabricator Diff: D66599197

fbshipit-source-id: 00891602920f84a04ea9eac32758d2af08f3d4c7
  • Loading branch information
Nikita Rubilov authored and facebook-github-bot committed Dec 5, 2024
1 parent ca908c0 commit ea84bc6
Show file tree
Hide file tree
Showing 16 changed files with 2,150 additions and 1 deletion.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module.exports = {
'/node_modules/',
'<rootDir>/packages/react-native/sdks',
'<rootDir>/packages/react-native/Libraries/Renderer',
'<rootDir>/packages/react-native-test-renderer/src',
'<rootDir>/packages/react-native/sdks/hermes/',
...PODS_LOCATIONS,
],
Expand Down
17 changes: 17 additions & 0 deletions packages/react-native-test-renderer/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/

module.exports = {
presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
'@babel/preset-flow',
],
plugins: ['@babel/plugin-transform-react-jsx'],
};
25 changes: 25 additions & 0 deletions packages/react-native-test-renderer/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) Meta Platforms, Inc. and 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';

module.exports = {
haste: {
defaultPlatform: 'ios',
platforms: ['android', 'ios', 'native'],
},
transform: {
'^.+\\.(js|ts|tsx)$': 'babel-jest',
},
transformIgnorePatterns: [
'node_modules/(?!((jest-)?react-native|@react-native(-community)?)/)',
],
setupFilesAfterEnv: ['./src/jest/setup-files-after-env'],
testEnvironment: './src/jest/environment',
};
18 changes: 18 additions & 0 deletions packages/react-native-test-renderer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@react-native/test-renderer",
"private": true,
"version": "0.77.0-main",
"description": "A Test rendering library for React Native",
"license": "MIT",
"devDependencies": {
"@babel/core": "^7.25.2",
"@babel/plugin-transform-react-jsx": "^7.25.2",
"@babel/preset-env": "^7.25.3",
"@babel/preset-flow": "^7.20.0"
},
"dependencies": {},
"main": "src/index.js",
"peerDependencies": {
"jest": "^29.7.0"
}
}
14 changes: 14 additions & 0 deletions packages/react-native-test-renderer/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
* @oncall react_native
*/

export {render} from './renderer/index.js';

export {ReactNativeEnvironment} from './jest/environment.js';
74 changes: 74 additions & 0 deletions packages/react-native-test-renderer/src/jest/environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* Copyright (c) Meta Platforms, Inc. and 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 NodeEnv = require('jest-environment-node').TestEnvironment;

module.exports = class ReactNativeEnvironment extends NodeEnv {
customExportConditions = ['require', 'react-native'];

constructor(config, context) {
super(config, context);
}

async setup() {
await super.setup();
this.assignGlobals();
this.initializeTurboModuleRegistry();
}

assignGlobals() {
Object.defineProperties(this.global, {
__DEV__: {
configurable: true,
enumerable: true,
value: true,
writable: true,
},
});
this.global.IS_REACT_ACT_ENVIRONMENT = true;
}

initializeTurboModuleRegistry() {
const dims = {width: 100, height: 100, scale: 1, fontScale: 1};
const DIMS = {
screen: {
...dims,
},
window: {
...dims,
},
};
this.global.nativeModuleProxy = name => ({})[name];
this.global.__turboModuleProxy = name =>
({
SourceCode: {getConstants: () => ({scriptURL: ''})},
WebSocketModule: {connect: () => {}},
FileReaderModule: {},
AppState: {getConstants: () => ({}), getCurrentAppState: () => ({})},
DeviceInfo: {getConstants: () => ({Dimensions: DIMS})},
UIManager: {getConstants: () => ({})},
Timing: {},
DevSettings: {},
PlatformConstants: {
getConstants: () => ({reactNativeVersion: '1000.0.0'}),
},
Networking: {},
ImageLoader: {},
NativePerformanceCxx: {},
LogBox: {},
SettingsManager: {
getConstants: () => ({settings: {}}),
},
LinkingManager: {},
I18n: {getConstants: () => ({})},
})[name];
}
};
222 changes: 222 additions & 0 deletions packages/react-native-test-renderer/src/jest/setup-files-after-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
/**
* Copyright (c) Meta Platforms, Inc. and 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';

jest.requireActual('@react-native/js-polyfills/error-guard');

jest
.mock('react-native/Libraries/ReactNative/UIManager', () => ({
AndroidViewPager: {
Commands: {
setPage: jest.fn(),
setPageWithoutAnimation: jest.fn(),
},
},
blur: jest.fn(),
createView: jest.fn(),
customBubblingEventTypes: {},
customDirectEventTypes: {},
getConstants: () => ({
ViewManagerNames: [],
}),
getDefaultEventTypes: jest.fn(),
dispatchViewManagerCommand: jest.fn(),
focus: jest.fn(),
getViewManagerConfig: jest.fn(name => {
if (name === 'AndroidDrawerLayout') {
return {
Constants: {
DrawerPosition: {
Left: 10,
},
},
};
}

return {NativeProps: {}};
}),
hasViewManagerConfig: jest.fn(name => {
return name === 'AndroidDrawerLayout';
}),
measure: jest.fn(),
manageChildren: jest.fn(),
removeSubviewsFromContainerWithID: jest.fn(),
replaceExistingNonRootView: jest.fn(),
setChildren: jest.fn(),
updateView: jest.fn(),
AndroidDrawerLayout: {
Constants: {
DrawerPosition: {
Left: 10,
},
},
},
AndroidTextInput: {
Commands: {},
},
ScrollView: {
Constants: {},
},
View: {
Constants: {},
},
}))
// Mock modules defined by the native layer (ex: Objective-C, Java)
.mock('react-native/Libraries/BatchedBridge/NativeModules', () => ({
AlertManager: {
alertWithArgs: 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, [])),
),
},
DeviceInfo: {
getConstants() {
return {
Dimensions: {
window: {
fontScale: 2,
height: 1334,
scale: 2,
width: 750,
},
screen: {
fontScale: 2,
height: 1334,
scale: 2,
width: 750,
},
},
};
},
},
DevSettings: {
addMenuItem: jest.fn(),
reload: jest.fn(),
},
ImageLoader: {
getSize: jest.fn(url => Promise.resolve([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(),
},
Networking: {
sendRequest: jest.fn(),
abortRequest: jest.fn(),
addListener: jest.fn(),
removeListeners: jest.fn(),
},
PlatformConstants: {
getConstants() {
return {
reactNativeVersion: {
major: 1000,
minor: 0,
patch: 0,
},
};
},
},
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(),
},
StatusBarManager: {
setColor: jest.fn(),
setStyle: jest.fn(),
setHidden: jest.fn(),
setNetworkActivityIndicatorVisible: jest.fn(),
setBackgroundColor: jest.fn(),
setTranslucent: jest.fn(),
getConstants: () => ({
HEIGHT: 42,
}),
},
Timing: {
createTimer: jest.fn(),
deleteTimer: jest.fn(),
},
UIManager: {},
BlobModule: {
getConstants: () => ({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(),
},
I18nManager: {
allowRTL: jest.fn(),
forceRTL: jest.fn(),
swapLeftAndRightInRTL: jest.fn(),
getConstants: () => ({
isRTL: false,
doLeftAndRightSwapInRTL: true,
}),
},
}));
Loading

0 comments on commit ea84bc6

Please sign in to comment.