-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Add react-native-test-renderer package #42644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'}}]], | ||
| plugins: [ | ||
| '@babel/plugin-transform-flow-strip-types', | ||
| '@babel/plugin-transform-react-jsx', | ||
| ], | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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', | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "name": "@react-native/test-renderer", | ||
| "private": true, | ||
| "version": "0.0.0", | ||
| "description": "A Test rendering library for React Native", | ||
| "license": "MIT", | ||
| "devDependencies": { | ||
| "@babel/core": "^7.23.7", | ||
| "@babel/plugin-transform-flow-strip-types": "^7.23.3", | ||
| "@babel/plugin-transform-react-jsx": "^7.23.4", | ||
| "@babel/preset-env": "^7.23.8", | ||
| "@types/jest": "^29.5.11", | ||
| "eslint": "^8.56.0", | ||
| "jest": "^29.7.0" | ||
| }, | ||
| "dependencies": {}, | ||
| "exports": { | ||
| ".": "./index.js", | ||
| "./jest-environment": "./dist/jest-environment/index.js", | ||
| "./jest-setup": "./dist/jest-setup/index.js" | ||
| }, | ||
| "peerDependencies": { "jest": "^29.7.0" } | ||
| } |
74 changes: 74 additions & 0 deletions
74
packages/react-native-test-renderer/src/jest/environment.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: {}, | ||
| NativePerformanceObserverCxx: {}, | ||
| LogBox: {}, | ||
| SettingsManager: { | ||
| getConstants: () => ({settings: {}}), | ||
| }, | ||
| LinkingManager: {}, | ||
| })[name]; | ||
| } | ||
| }; |
222 changes: 222 additions & 0 deletions
222
packages/react-native-test-renderer/src/jest/setup-files-after-env.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| }), | ||
| }, | ||
| })); |
12 changes: 12 additions & 0 deletions
12
packages/react-native-test-renderer/src/renderer/__tests__/__snapshots__/render-test.js.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`render toJSON returns expected JSON output based on renderer component 1`] = ` | ||
| "<> | ||
| <RCTView> | ||
| <RCTText accessible=true allowFontScaling=true ellipsizeMode="tail" isHighlighted=false selectionColor=null> | ||
| <RCTRawText text="Hello" /> | ||
| </RCTText> | ||
| <RCTView /> | ||
| </RCTView> | ||
| </>" | ||
| `; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needed to add this because the package has its own jest config and running from root fails. Is there a way to make sure these tests are run against the custom config in CI moving forward?