Skip to content

Commit 78d6420

Browse files
sumo-sloniktjzel
andcommitted
Fix bug in useSharedValueMock (#6742)
<!-- 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]>
1 parent 30608d0 commit 78d6420

File tree

1 file changed

+31
-1
lines changed
  • packages/react-native-reanimated/src

1 file changed

+31
-1
lines changed

packages/react-native-reanimated/src/mock.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,37 @@ const hook = {
5050
): EventHandlerProcessed<Event, Context> => NOOP,
5151
// useHandler: ADD ME IF NEEDED
5252
useWorkletCallback: ID,
53-
useSharedValue: <Value>(init: Value) => ({ value: init }),
53+
useSharedValue: <Value>(init: Value) => {
54+
const value = { value: init };
55+
return new Proxy(value, {
56+
get(target, prop) {
57+
if (prop === 'value') {
58+
return target.value;
59+
}
60+
if (prop === 'get') {
61+
return () => target.value;
62+
}
63+
if (prop === 'set') {
64+
return (newValue: Value | ((currentValue: Value) => Value)) => {
65+
if (typeof newValue === 'function') {
66+
target.value = (newValue as (currentValue: Value) => Value)(
67+
target.value
68+
);
69+
} else {
70+
target.value = newValue;
71+
}
72+
};
73+
}
74+
},
75+
set(target, prop: string, newValue) {
76+
if (prop === 'value') {
77+
target.value = newValue;
78+
return true;
79+
}
80+
return false;
81+
},
82+
});
83+
},
5484
// useReducedMotion: ADD ME IF NEEDED
5585
useAnimatedStyle: IMMEDIATE_CALLBACK_INVOCATION,
5686
useAnimatedGestureHandler: NOOP_FACTORY,

0 commit comments

Comments
 (0)