Skip to content
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

[RNMobile] Refactor UIManager native module mock #29390

Merged
merged 6 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions packages/react-native-aztec/src/AztecView.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* External dependencies
*/
import ReactNative, {
import {
findNodeHandle,
requireNativeComponent,
UIManager,
TouchableWithoutFeedback,
Expand Down Expand Up @@ -39,7 +40,7 @@ class AztecView extends Component {
dispatch( command, params ) {
params = params || [];
UIManager.dispatchViewManagerCommand(
ReactNative.findNodeHandle( this ),
findNodeHandle( this ),
command,
params
);
Expand Down Expand Up @@ -125,7 +126,7 @@ class AztecView extends Component {

_onBlur( event ) {
this.selectionEndCaretY = null;
TextInputState.blurTextInput( ReactNative.findNodeHandle( this ) );
TextInputState.blurTextInput( findNodeHandle( this ) );

if ( ! this.props.onBlur ) {
return;
Expand Down Expand Up @@ -177,18 +178,16 @@ class AztecView extends Component {
}

blur() {
TextInputState.blurTextInput( ReactNative.findNodeHandle( this ) );
TextInputState.blurTextInput( findNodeHandle( this ) );
}

focus() {
TextInputState.focusTextInput( ReactNative.findNodeHandle( this ) );
TextInputState.focusTextInput( findNodeHandle( this ) );
}

isFocused() {
const focusedField = TextInputState.currentlyFocusedField();
return (
focusedField && focusedField === ReactNative.findNodeHandle( this )
);
return focusedField && focusedField === findNodeHandle( this );
}

_onPress( event ) {
Expand Down
20 changes: 0 additions & 20 deletions test/native/__mocks__/react-native-aztec/index.js

This file was deleted.

1 change: 0 additions & 1 deletion test/native/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ module.exports = {
haste: {
defaultPlatform: rnPlatform,
platforms: [ 'android', 'ios', 'native' ],
providesModuleNodeModules: [ 'react-native', 'react-native-svg' ],
},
transformIgnorePatterns: [
// This is required for now to have jest transform some of our modules
Expand Down
33 changes: 10 additions & 23 deletions test/native/setup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { NativeModules } from 'react-native';
import 'react-native-gesture-handler/jestSetup';

jest.mock( '@wordpress/element', () => {
Expand Down Expand Up @@ -106,27 +105,6 @@ jest.mock( '@react-native-community/blur', () => () => 'BlurView', {
virtual: true,
} );

// Overwrite some native module mocks from `react-native` jest preset:
// https://github.com/facebook/react-native/blob/HEAD/jest/setup.js
// to fix issue "TypeError: Cannot read property 'Commands' of undefined"
// raised when calling focus or blur on a native component
const mockNativeModules = {
UIManager: {
...NativeModules.UIManager,
getViewManagerConfig: jest.fn( () => ( { Commands: {} } ) ),
},
};

Object.keys( mockNativeModules ).forEach( ( module ) => {
try {
jest.doMock( module, () => mockNativeModules[ module ] ); // needed by FacebookSDK-test
} catch ( error ) {
jest.doMock( module, () => mockNativeModules[ module ], {
virtual: true,
} );
}
} );

jest.mock( 'react-native-reanimated', () => {
const Reanimated = require( 'react-native-reanimated/mock' );

Expand All @@ -137,5 +115,14 @@ jest.mock( 'react-native-reanimated', () => {
return Reanimated;
} );

// Silence the warning: Animated: `useNativeDriver` is not supported because the native animated module is missing
// Silence the warning: Animated: `useNativeDriver` is not supported because the
// native animated module is missing. This was added per React Navigation docs.
// https://reactnavigation.org/docs/testing/#mocking-native-modules
jest.mock( 'react-native/Libraries/Animated/src/NativeAnimatedHelper' );

// We currently reference TextStateInput (a private module) within
// react-native-aztec/src/AztecView. Doing so requires that we mock it via its
// internal path to avoid "TypeError: Cannot read property 'Commands' of
// undefined." The private module referenced could possibly be replaced with
// a React ref instead. We could then remove this internal mock.
jest.mock( 'react-native/Libraries/Components/TextInput/TextInputState' );