Skip to content

Commit

Permalink
Animated: Fix NativeAnimatedModule Mock for Jest
Browse files Browse the repository at this point in the history
Summary:
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`

Differential Revision: D63572067
  • Loading branch information
yungsters authored and facebook-github-bot committed Sep 28, 2024
1 parent 513e966 commit ac14592
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
26 changes: 16 additions & 10 deletions packages/react-native/Libraries/Animated/nodes/AnimatedProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions packages/react-native/jest/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,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) => {
requestAnimationFrame(() => endCallback({finished: true}));
},
),
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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit ac14592

Please sign in to comment.