Skip to content

Commit

Permalink
Fix bug in useSharedValueMock (#6742)
Browse files Browse the repository at this point in the history
<!-- Thanks for submitting a pull request! We appreciate you spending
the time to work on these changes. Please follow the template so that
the reviewers can easily understand what the code changes affect. -->

## Summary

The API for sharedValue has changed from using .value to get() and set()
methods. As a result, the mock that previously returned an object with a
single value property no longer works in the tests.

## Test plan

This is a PR for the mocks, so it's not necessary for it.

---------

Co-authored-by: Tomasz Żelawski <[email protected]>
  • Loading branch information
sumo-slonik and tjzel committed Dec 13, 2024
1 parent f54fb3c commit 16cd933
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion packages/react-native-reanimated/src/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,37 @@ const hook = {
): EventHandlerProcessed<Event, Context> => NOOP,
// useHandler: ADD ME IF NEEDED
useWorkletCallback: ID,
useSharedValue: <Value>(init: Value) => ({ value: init }),
useSharedValue: <Value>(init: Value) => {
const value = { value: init };
return new Proxy(value, {
get(target, prop) {
if (prop === 'value') {
return target.value;
}
if (prop === 'get') {
return () => target.value;
}
if (prop === 'set') {
return (newValue: Value | ((currentValue: Value) => Value)) => {
if (typeof newValue === 'function') {
target.value = (newValue as (currentValue: Value) => Value)(
target.value
);
} else {
target.value = newValue;
}
};
}
},
set(target, prop: string, newValue) {
if (prop === 'value') {
target.value = newValue;
return true;
}
return false;
},
});
},
// useReducedMotion: ADD ME IF NEEDED
useAnimatedStyle: IMMEDIATE_CALLBACK_INVOCATION,
useAnimatedGestureHandler: NOOP_FACTORY,
Expand Down

0 comments on commit 16cd933

Please sign in to comment.