mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Animated: Fix NativeAnimatedModule Mock for Jest (#46715)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/46715 Properly mocks `NativeAnimatedModule` in Jest so that we can cleanup the `process.env.NODE_ENV` checks in Animated. Changelog: [General][Added] - Added Jest mocks for `NativeAnimatedModule` Reviewed By: javache Differential Revision: D63572067 fbshipit-source-id: a4fcedeebbaaa47a34bb356305652a3ed6358855
This commit is contained in:
committed by
Facebook GitHub Bot
parent
177697f539
commit
fd355308fa
@@ -16,16 +16,7 @@ let Animated = require('../Animated').default;
|
||||
const AnimatedProps = require('../nodes/AnimatedProps').default;
|
||||
const TestRenderer = require('react-test-renderer');
|
||||
|
||||
jest.mock('../../BatchedBridge/NativeModules', () => ({
|
||||
NativeAnimatedModule: {},
|
||||
PlatformConstants: {
|
||||
getConstants() {
|
||||
return {};
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
describe('Animated tests', () => {
|
||||
describe('Animated', () => {
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
});
|
||||
|
||||
+16
-10
@@ -212,11 +212,14 @@ export default class AnimatedProps extends AnimatedNode {
|
||||
|
||||
__connectAnimatedView(): void {
|
||||
invariant(this.__isNative, 'Expected node to be marked as "native"');
|
||||
const nativeViewTag: ?number = findNodeHandle(this.#animatedView);
|
||||
invariant(
|
||||
nativeViewTag != null,
|
||||
'Unable to locate attached view in the native tree',
|
||||
);
|
||||
let nativeViewTag: ?number = findNodeHandle(this.#animatedView);
|
||||
if (nativeViewTag == null) {
|
||||
if (process.env.NODE_ENV === 'test') {
|
||||
nativeViewTag = -1;
|
||||
} else {
|
||||
throw new Error('Unable to locate attached view in the native tree');
|
||||
}
|
||||
}
|
||||
NativeAnimatedHelper.API.connectAnimatedNodeToView(
|
||||
this.__getNativeTag(),
|
||||
nativeViewTag,
|
||||
@@ -225,11 +228,14 @@ export default class AnimatedProps extends AnimatedNode {
|
||||
|
||||
__disconnectAnimatedView(): void {
|
||||
invariant(this.__isNative, 'Expected node to be marked as "native"');
|
||||
const nativeViewTag: ?number = findNodeHandle(this.#animatedView);
|
||||
invariant(
|
||||
nativeViewTag != null,
|
||||
'Unable to locate attached view in the native tree',
|
||||
);
|
||||
let nativeViewTag: ?number = findNodeHandle(this.#animatedView);
|
||||
if (nativeViewTag == null) {
|
||||
if (process.env.NODE_ENV === 'test') {
|
||||
nativeViewTag = -1;
|
||||
} else {
|
||||
throw new Error('Unable to locate attached view in the native tree');
|
||||
}
|
||||
}
|
||||
NativeAnimatedHelper.API.disconnectAnimatedNodeFromView(
|
||||
this.__getNativeTag(),
|
||||
nativeViewTag,
|
||||
|
||||
Vendored
+28
@@ -280,6 +280,34 @@ jest
|
||||
addListener: jest.fn(),
|
||||
removeListeners: jest.fn(),
|
||||
},
|
||||
NativeAnimatedModule: {
|
||||
createAnimatedNode: jest.fn(),
|
||||
updateAnimatedNodeConfig: jest.fn(),
|
||||
getValue: jest.fn(),
|
||||
startListeningToAnimatedNodeValue: jest.fn(),
|
||||
stopListeningToAnimatedNodeValue: jest.fn(),
|
||||
connectAnimatedNodes: jest.fn(),
|
||||
disconnectAnimatedNodes: jest.fn(),
|
||||
startAnimatingNode: jest.fn(
|
||||
(animationId, nodeTag, config, endCallback) => {
|
||||
setTimeout(() => endCallback({finished: true}), 16);
|
||||
},
|
||||
),
|
||||
stopAnimation: jest.fn(),
|
||||
setAnimatedNodeValue: jest.fn(),
|
||||
setAnimatedNodeOffset: jest.fn(),
|
||||
flattenAnimatedNodeOffset: jest.fn(),
|
||||
extractAnimatedNodeOffset: jest.fn(),
|
||||
connectAnimatedNodeToView: jest.fn(),
|
||||
disconnectAnimatedNodeFromView: jest.fn(),
|
||||
restoreDefaultValues: jest.fn(),
|
||||
dropAnimatedNode: jest.fn(),
|
||||
addAnimatedEventToView: jest.fn(),
|
||||
removeAnimatedEventFromView: jest.fn(),
|
||||
addListener: jest.fn(),
|
||||
removeListener: jest.fn(),
|
||||
removeListeners: jest.fn(),
|
||||
},
|
||||
Networking: {
|
||||
sendRequest: jest.fn(),
|
||||
abortRequest: jest.fn(),
|
||||
|
||||
@@ -175,9 +175,8 @@ const API = {
|
||||
|
||||
flushQueue: (isSingleOpBatching
|
||||
? (): void => {
|
||||
// TODO: (T136971132)
|
||||
invariant(
|
||||
NativeAnimatedModule || process.env.NODE_ENV === 'test',
|
||||
NativeAnimatedModule,
|
||||
'Native animated module is not available',
|
||||
);
|
||||
flushQueueImmediate = null;
|
||||
@@ -197,9 +196,8 @@ const API = {
|
||||
singleOpQueue.length = 0;
|
||||
}
|
||||
: (): void => {
|
||||
// TODO: (T136971132)
|
||||
invariant(
|
||||
NativeAnimatedModule || process.env.NODE_ENV === 'test',
|
||||
NativeAnimatedModule,
|
||||
'Native animated module is not available',
|
||||
);
|
||||
flushQueueImmediate = null;
|
||||
|
||||
Reference in New Issue
Block a user