-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Pass empty callbacks to UI when runOnJS is true
#4326
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
Merged
+137
−2
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
packages/react-native-gesture-handler/src/__tests__/runOnJSReanimatedHandlers.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import { renderHook } from '@testing-library/react-native'; | ||
|
|
||
| import { useTapGesture } from '../v3/hooks/gestures'; | ||
| import type { SharedValue } from '../v3/types'; | ||
|
|
||
| jest.mock('../handlers/gestures/reanimatedWrapper', () => { | ||
| const Reanimated = { | ||
| useHandler: jest.fn(() => ({ | ||
| doDependenciesDiffer: false, | ||
| context: { lastUpdateEvent: undefined }, | ||
| })), | ||
| useEvent: jest.fn(() => jest.fn()), | ||
| isSharedValue: (value: unknown): boolean => | ||
| value !== null && typeof value === 'object' && 'value' in value, | ||
| isWorkletFunction: (value: unknown): boolean => | ||
| typeof value === 'function' && '__workletHash' in value, | ||
| makeMutable: <T,>(value: T) => ({ value }), | ||
| runOnUI: () => jest.fn(), | ||
| runOnJS: | ||
| (fn: (...args: unknown[]) => unknown) => | ||
| (...args: unknown[]) => | ||
| fn(...args), | ||
| useSharedValue: <T,>(value: T) => ({ value }), | ||
| setGestureState: jest.fn(), | ||
| }; | ||
|
|
||
| return { Reanimated }; | ||
| }); | ||
|
|
||
| const { Reanimated: MockedReanimated } = jest.requireMock( | ||
| '../handlers/gestures/reanimatedWrapper' | ||
| ) as unknown as { Reanimated: { useHandler: jest.Mock } }; | ||
|
|
||
| const lastHandlers = () => | ||
| MockedReanimated.useHandler.mock.calls.at(-1)?.[0] as Record<string, unknown>; | ||
|
|
||
| const makeFakeSharedValue = (value: boolean) => | ||
| ({ | ||
| value, | ||
| addListener: jest.fn(), | ||
| removeListener: jest.fn(), | ||
| modify: jest.fn(), | ||
| }) as unknown as SharedValue<boolean>; | ||
|
|
||
| // When `runOnJS` is statically `true`, the Reanimated event path can never | ||
| // receive events, so user callbacks must not be serialized to the UI runtime | ||
| // (in dev, Worklets freezes every plain object captured in their closures, | ||
| // silently dropping later writes). These tests guard the substitution in | ||
| // `useGestureCallbacks` and the re-registration when `runOnJS` changes. | ||
| describe('[API v3] Reanimated handler registration vs runOnJS', () => { | ||
| beforeEach(() => { | ||
| MockedReanimated.useHandler.mockClear(); | ||
| }); | ||
|
|
||
| test('registers an empty handler set when runOnJS is statically true', () => { | ||
| const onActivate = jest.fn(); | ||
|
|
||
| renderHook(() => useTapGesture({ onActivate, runOnJS: true })); | ||
|
|
||
| expect(MockedReanimated.useHandler).toHaveBeenCalled(); | ||
| for (const call of MockedReanimated.useHandler.mock.calls) { | ||
| expect(Object.keys(call[0] as object)).toHaveLength(0); | ||
| } | ||
| }); | ||
|
|
||
| test('registers real callbacks when runOnJS is not set', () => { | ||
| const onActivate = jest.fn(); | ||
|
|
||
| renderHook(() => useTapGesture({ onActivate })); | ||
|
|
||
| expect(lastHandlers().onActivate).toBe(onActivate); | ||
| }); | ||
|
|
||
| test('registers real callbacks when runOnJS is false', () => { | ||
| const onActivate = jest.fn(); | ||
|
|
||
| renderHook(() => useTapGesture({ onActivate, runOnJS: false })); | ||
|
|
||
| expect(lastHandlers().onActivate).toBe(onActivate); | ||
| }); | ||
|
|
||
| test('re-registers real callbacks when runOnJS changes from true to false', () => { | ||
| const onActivate = jest.fn(); | ||
|
|
||
| const { rerender } = renderHook( | ||
| ({ runOnJS }: { runOnJS: boolean }) => | ||
| useTapGesture({ onActivate, runOnJS }), | ||
| { initialProps: { runOnJS: true } } | ||
| ); | ||
|
|
||
| expect(Object.keys(lastHandlers())).toHaveLength(0); | ||
|
|
||
| rerender({ runOnJS: false }); | ||
|
|
||
| expect(lastHandlers().onActivate).toBe(onActivate); | ||
| }); | ||
|
|
||
| test('swaps back to the empty handler set when runOnJS returns to true', () => { | ||
| const onActivate = jest.fn(); | ||
|
|
||
| const { rerender } = renderHook( | ||
| ({ runOnJS }: { runOnJS: boolean }) => | ||
| useTapGesture({ onActivate, runOnJS }), | ||
| { initialProps: { runOnJS: false } } | ||
| ); | ||
|
|
||
| expect(lastHandlers().onActivate).toBe(onActivate); | ||
|
|
||
| rerender({ runOnJS: true }); | ||
|
|
||
| expect(Object.keys(lastHandlers())).toHaveLength(0); | ||
| }); | ||
|
|
||
| test('keeps real callbacks when runOnJS is a SharedValue holding true', () => { | ||
| const onActivate = jest.fn(); | ||
|
|
||
| renderHook(() => | ||
| useTapGesture({ onActivate, runOnJS: makeFakeSharedValue(true) }) | ||
| ); | ||
|
|
||
| expect(lastHandlers().onActivate).toBe(onActivate); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add jest test