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

Improve typing in mock.ts #5372

Merged
merged 6 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ docs/*
plugin/*
eslintrc.js
jest.config.js
mock.js
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = {
},
rules: {
curly: 'error',
'@typescript-eslint/no-explicit-any': 'warn',
tjzel marked this conversation as resolved.
Show resolved Hide resolved
'import/no-unresolved': 'error',
'import/consistent-type-specifier-style': ['error', 'prefer-top-level'],
'react/jsx-uses-vars': 'error',
Expand Down
52 changes: 3 additions & 49 deletions mock.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,10 @@
/**
* Mock implementation for test runners.
*
* Example:
*
* ```js
* jest.mock('react-native-reanimated', () => require('react-native-reanimated/mock'));
* ```
*/

const { View, Text, Image, Animated, processColor } = require('react-native');
const ReanimatedV2 = require('./src/reanimated2/mock');

function NOOP() {
// noop
}

const Reanimated = {
SpringUtils: {
makeDefaultConfig: NOOP,
makeConfigFromBouncinessAndSpeed: NOOP,
makeConfigFromOrigamiTensionAndFriction: NOOP,
},

View,
Text,
Image,
ScrollView: Animated.ScrollView,
FlatList: Animated.FlatList,

Extrapolate: {
EXTEND: 'extend',
CLAMP: 'clamp',
IDENTITY: 'identity',
},

processColor,

interpolate: NOOP,
interpolateColor: NOOP,
clamp: NOOP,
createAnimatedComponent: (Component) => Component,
addWhitelistedUIProps: NOOP,
addWhitelistedNativeProps: NOOP,
};
const Reanimated = require('./src/mock');
const Animated = Reanimated.default;
tjzel marked this conversation as resolved.
Show resolved Hide resolved

module.exports = {
__esModule: true,

...Reanimated,
...ReanimatedV2,

default: {
...Reanimated,
...Animated,
},
};
114 changes: 68 additions & 46 deletions src/reanimated2/mock.ts → src/mock.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
/* eslint-disable n/no-callback-literal */
'use strict';

import type { WithSpringConfig, WithTimingConfig } from './animation';
import type { DecayConfig } from './animation/decay/utils';
import type { AnimatableValue, AnimationCallback } from './commonTypes';
import type {
WithSpringConfig,
WithTimingConfig,
WithDecayConfig,
AnimatableValue,
AnimationCallback,
} from './reanimated2';
import {
IOSReferenceFrame,
InterfaceOrientation,
KeyboardState,
ReduceMotion,
SensorType,
} from './commonTypes';
import { ColorSpace, Extrapolate } from './interpolateColor';
import { Extrapolation } from './interpolation';
import { SharedTransitionType } from './layoutReanimation';
ColorSpace,
Extrapolation,
SharedTransitionType,
} from './reanimated2';
import {
View as ViewRN,
Text as TextRN,
Image as ImageRN,
Animated as AnimatedRN,
processColor as processColorRN,
} from 'react-native';

const NOOP = () => {};
const NOOP_FACTORY = () => NOOP;
const ID = <T>(t: T) => t;
const IMMEDIATE_CALLBACK_INVOCATION = <T>(callback: () => T) => callback();
const NOOP_PROXY_FACTORY = () =>
new Proxy(
{},
{
get: NOOP,
}
);

const hook = {
useAnimatedProps: IMMEDIATE_CALLBACK_INVOCATION,
useEvent: NOOP_PROXY_FACTORY, // should be fine?
useHandler: NOOP_PROXY_FACTORY, // should be fine?
// useEvent: ADD ME IF NEEDED
// useHandler: ADD ME IF NEEDED
useWorkletCallback: ID,
useSharedValue: <Value>(init: Value) => ({ value: init }),
useReducedMotion: NOOP, // should be fine?
// useReducedMotion: ADD ME IF NEEDED
useAnimatedStyle: IMMEDIATE_CALLBACK_INVOCATION,
useAnimatedGestureHandler: NOOP_FACTORY,
useAnimatedReaction: NOOP,
Expand Down Expand Up @@ -64,16 +68,16 @@ const hook = {
iosReferenceFrame: 0,
},
}),
useFrameCallback: IMMEDIATE_CALLBACK_INVOCATION, // should be fine?
// useFrameCallback: ADD ME IF NEEDED
useAnimatedKeyboard: () => ({ height: 0, state: 0 }),
useScrollViewOffset: () => ({ value: 0 }), // should be fine?
// useScrollViewOffset: ADD ME IF NEEDED
};

const animation = {
cancelAnimation: NOOP,
defineAnimation: NOOP_FACTORY, // ?
withClamp: NOOP_FACTORY, // ?
withDecay: (_userConfig: DecayConfig, callback?: AnimationCallback) => {
// defineAnimation: ADD ME IF NEEDED
// withClamp: ADD ME IF NEEDED
withDecay: (_userConfig: WithDecayConfig, callback?: AnimationCallback) => {
callback?.(true);
return 0;
},
Expand Down Expand Up @@ -102,15 +106,16 @@ const animation = {

const interpolation = {
Extrapolation,
interpolate: ID, // ?
clamp: ID, // ?
interpolate: NOOP,
clamp: NOOP,
};

const interpolateColor = {
Extrapolate,
Extrapolate: Extrapolation,
Extrapolation,
ColorSpace,
interpolateColor: ID, // ?
useInterpolateConfig: NOOP_FACTORY, // ?
interpolateColor: NOOP,
// useInterpolateConfig: ADD ME IF NEEDED
};

const Easing = {
Expand Down Expand Up @@ -144,21 +149,21 @@ const platformFunctions = {
pageX: 0,
pageY: 0,
}),
dispatchCommand: NOOP, // ?
scrollTo: NOOP, // ?
setGestureState: NOOP, // ?
setNativeProps: NOOP, // ?
getRelativeCoords: ID, // ?
// dispatchCommand: ADD ME IF NEEDED
// scrollTo: ADD ME IF NEEDED
// setGestureState: ADD ME IF NEEDED
// setNativeProps: ADD ME IF NEEDED
// getRelativeCoords: ADD ME IF NEEDED
};

const Colors = {
isColor: () => true, // ?
processColor: ID, // ?
convertToRGBA: ID, // ?
// isColor: ADD ME IF NEEDED
processColor: processColorRN,
// convertToRGBA: ADD ME IF NEEDED
};

const PropAdapters = {
createAnimatedPropAdapter: NOOP_FACTORY, // ?
// createAnimatedPropAdapter: ADD ME IF NEEDED
};

class BaseAnimationMock {
Expand Down Expand Up @@ -259,9 +264,9 @@ const core = {
makeMutable: ID,
makeShareableCloneRecursive: ID,
isReanimated3: () => true,
isConfigured: () => true, // ?
// isConfigured: ADD ME IF NEEDED
enableLayoutAnimations: NOOP,
getViewProp: ID, // ?
// getViewProp: ADD ME IF NEEDED
};

const layoutReanimation = {
Expand Down Expand Up @@ -364,14 +369,14 @@ const layoutReanimation = {
JumpingTransition: new BaseAnimationMock(),
CurvedTransition: new BaseAnimationMock(),
EntryExitTransition: new BaseAnimationMock(),
combineTransitions: ID, // ?
// combineTransitions: ADD ME IF NEEDED
// SET
SharedTransition: new BaseAnimationMock(), // ?
// SharedTransition: ADD ME IF NEEDED
SharedTransitionType,
};

const isSharedValue = {
isSharedValue: () => true, // ?
// isSharedValue: ADD ME IF NEEDED
};

const commonTypes = {
Expand All @@ -383,18 +388,33 @@ const commonTypes = {
};

const pluginUtils = {
getUseOfValueInStyleWarning: NOOP, // ?
// getUseOfValueInStyleWarning: ADD ME IF NEEDED
};

// const jestUtils ???
// const jestUtils = ADD ME IF NEEDED

const LayoutAnimationConfig = {
LayoutAnimationConfig: NOOP_FACTORY, // ?
// LayoutAnimationConfig: ADD ME IF NEEDED
};

const mappers = {
startMapper: NOOP, // ?
stopMapper: NOOP, // ?
// startMapper: ADD ME IF NEEDED
// stopMapper: ADD ME IF NEEDED
};

const Animated = {
View: ViewRN,
Text: TextRN,
Image: ImageRN,
ScrollView: AnimatedRN.ScrollView,
FlatList: AnimatedRN.FlatList,
Extrapolate: Extrapolation,
interpolate: NOOP,
interpolateColor: NOOP,
clamp: NOOP,
createAnimatedComponent: ID,
addWhitelistedUIProps: NOOP,
addWhitelistedNativeProps: NOOP,
};

const Reanimated = {
Expand All @@ -416,5 +436,7 @@ const Reanimated = {
};

module.exports = {
__esModule: true,
...Reanimated,
default: Animated,
};