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
Changes from 1 commit
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
73 changes: 39 additions & 34 deletions src/reanimated2/mock.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
/* eslint-disable node/no-callback-literal */
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
'use strict';

import type { WithSpringConfig, WithTimingConfig } from './animation';
import type { DecayConfig } from './animation/decay/utils';
import type { AnimatableValue, AnimationCallback } from './commonTypes';
import { ReduceMotion, SensorType } from './commonTypes';

const NOOP = () => {
// noop
};
const ID = (t) => t;
const IMMEDIATE_CB_INVOCATION = (cb: () => unknown) => cb();
const NOOP_FACTORY = () => NOOP;
const ID = <T>(t: T) => t;
const IMMEDIATE_CALLBACK_INVOCATION = <T>(callback: () => T) => callback();

class BaseAnimationMock {
duration(_: number) {
duration() {
return this;
}

delay(_: number) {
delay() {
return this;
}

springify(_: number) {
springify() {
return this;
}

damping(_: number) {
damping() {
return this;
}

stiffness(_: number) {
stiffness() {
return this;
}

withCallback(_: (finished: boolean) => void) {
withCallback() {
return this;
}

Expand All @@ -48,20 +50,20 @@ class BaseAnimationMock {
return () => ({ initialValues: {}, animations: {} });
}

reduceMotion(_: ReduceMotion) {
reduceMotion() {
return this;
}
}

const ReanimatedV2 = {
useSharedValue: (v) => ({ value: v }),
useDerivedValue: (a) => ({ value: a() }),
useAnimatedScrollHandler: () => NOOP,
useAnimatedGestureHandler: () => NOOP,
useAnimatedStyle: IMMEDIATE_CB_INVOCATION,
useSharedValue: <Value>(init: Value) => ({ value: init }),
useDerivedValue: <Value>(processor: () => Value) => ({ value: processor() }),
useAnimatedScrollHandler: NOOP_FACTORY,
useAnimatedGestureHandler: NOOP_FACTORY,
useAnimatedStyle: IMMEDIATE_CALLBACK_INVOCATION,
useAnimatedProps: IMMEDIATE_CALLBACK_INVOCATION,
tjzel marked this conversation as resolved.
Show resolved Hide resolved
useAnimatedRef: () => ({ current: null }),
useAnimatedReaction: NOOP,
useAnimatedProps: IMMEDIATE_CB_INVOCATION,
ReduceMotion: ReduceMotion,
SensorType: SensorType,
useAnimatedSensor: () => ({
Expand Down Expand Up @@ -89,27 +91,31 @@ const ReanimatedV2 = {
},
}),

withTiming: (toValue, _, cb) => {
cb && cb(true);
withTiming: (
toValue: AnimatableValue,
_userConfig?: WithTimingConfig,
callback?: AnimationCallback
) => {
callback?.(true);
return toValue;
},
withSpring: (toValue, _, cb) => {
cb && cb(true);
withSpring: (
toValue: AnimatableValue,
_userConfig?: WithSpringConfig,
callback?: AnimationCallback
) => {
callback?.(true);
return toValue;
},
withDecay: (_, cb) => {
cb && cb(true);
withDecay: (_userConfig: DecayConfig, callback?: AnimationCallback) => {
callback?.(true);
return 0;
},
withDelay: (_, animationValue) => {
return animationValue;
},
withSequence: (..._animations) => {
return 0;
},
withRepeat: (animation) => {
return animation;
withDelay: <T>(_delayMs: number, nextAnimation: T) => {
return nextAnimation;
},
withSequence: () => 0,
withRepeat: ID,
cancelAnimation: NOOP,
measure: () => ({
x: 0,
Expand Down Expand Up @@ -143,9 +149,8 @@ const ReanimatedV2 = {
CLAMP: 'clamp',
IDENTITY: 'identity',
},

runOnJS: (fn) => fn,
runOnUI: (fn) => fn,
runOnJS: ID,
runOnUI: ID,
};

[
Expand Down