-
-
Notifications
You must be signed in to change notification settings - Fork 992
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
Support for useReanimatedGestureHandler in Jest #1762
Closed
Closed
Changes from 19 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
fbb73fe
Set up for Jest
piaskowyk 22c989f
Removed Platform.OS mock
piaskowyk f701290
Remove some configs
piaskowyk 8bf842a
Tap, Pan, LongPress
piaskowyk 71ba707
Add test tags
j-piasecki cad2fa8
All events
piaskowyk 5315b70
Refactor part 1
piaskowyk 475e158
Refactor part 2
piaskowyk d924158
Recognise event tags
piaskowyk 0a8e305
Rename macro
piaskowyk 7c368a4
Fix lint
piaskowyk 300449d
Fix lint
piaskowyk f3a7da1
Fix lint - last time, rly
piaskowyk edac5a3
Rename withTestTag to withTestId
j-piasecki 942ee6a
Add jest registry
j-piasecki d727e9d
Rename methods
piaskowyk dbbe739
Remove rule
j-piasecki 60109b1
Nested handlers and removed raw events
piaskowyk dd8daca
Revrite tests
piaskowyk 6196551
Support for onGestureEvent
piaskowyk f052de1
Basic support for API v2
piaskowyk 6af729e
Lint
piaskowyk 9b8a469
Lint
piaskowyk f28aeed
Merge with https://github.com/j-piasecki/react-native-gesture-handler…
piaskowyk a04e874
New approach basic
piaskowyk 60dad67
Added suport for event config
piaskowyk 857565a
Added basic support for api v2
piaskowyk a405e49
Checkpoint
piaskowyk 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
This file contains 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,219 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react-native'; | ||
import { Text, View } from 'react-native'; | ||
import { | ||
GestureHandlerRootView, | ||
TapGestureHandler, | ||
PanGestureHandler, | ||
LongPressGestureHandler, | ||
FlingGestureHandler, | ||
RotationGestureHandler, | ||
PinchGestureHandler, | ||
Gesture, | ||
GestureDetector | ||
} from 'react-native-gesture-handler'; | ||
import { | ||
fireTapGestureHandler, | ||
firePanGestureHandler, | ||
fireLongPressGestureHandler, | ||
fireRotationGestureHandler, | ||
fireFlingGestureHandler, | ||
firePinchGestureHandler, | ||
ghTagEventMacro | ||
} from '../src/jestUtils' | ||
import { useAnimatedGestureHandler } from 'react-native-reanimated'; | ||
|
||
const mockEventFunctions = () => { | ||
return { | ||
begin: jest.fn(), | ||
progress: jest.fn(), | ||
end: jest.fn(), | ||
fail: jest.fn(), | ||
cancel: jest.fn(), | ||
finish: jest.fn() | ||
}; | ||
} | ||
|
||
const assertEventCalls = (eventFunctions, counts) => { | ||
expect(eventFunctions.begin).toHaveBeenCalledTimes( | ||
counts?.begin ? counts.begin : 1 | ||
); | ||
expect(eventFunctions.progress).toHaveBeenCalledTimes( | ||
counts?.progress ? counts.progress : 1 | ||
); | ||
expect(eventFunctions.end).toHaveBeenCalledTimes( | ||
counts?.end ? counts.end : 1 | ||
); | ||
} | ||
|
||
const App = (props) => { | ||
const eventHandler = useAnimatedGestureHandler({ | ||
onStart: () => props.eventFunctions.begin(), | ||
onActive: () => props.eventFunctions.progress(), | ||
onEnd: () => props.eventFunctions.end() | ||
}); | ||
|
||
return ( | ||
<GestureHandlerRootView> | ||
<TapGestureHandler onHandlerStateChange={eventHandler}> | ||
<Text {...ghTagEventMacro()}>TapGestureHandlerTest</Text> | ||
</TapGestureHandler> | ||
|
||
<PanGestureHandler onHandlerStateChange={eventHandler} onGestureEvent={eventHandler}> | ||
<Text {...ghTagEventMacro()}>PanGestureHandlerTest</Text> | ||
</PanGestureHandler> | ||
|
||
<LongPressGestureHandler onHandlerStateChange={eventHandler}> | ||
<Text {...ghTagEventMacro()}>LongPressGestureHandlerTest</Text> | ||
</LongPressGestureHandler> | ||
|
||
<RotationGestureHandler onHandlerStateChange={eventHandler}> | ||
<Text {...ghTagEventMacro()}>RotationGestureHandlerTest</Text> | ||
</RotationGestureHandler> | ||
|
||
<FlingGestureHandler onHandlerStateChange={eventHandler}> | ||
<Text {...ghTagEventMacro()}>FlingGestureHandlerTest</Text> | ||
</FlingGestureHandler> | ||
|
||
<PinchGestureHandler onHandlerStateChange={eventHandler}> | ||
<Text {...ghTagEventMacro()}>PinchGestureHandlerTest</Text> | ||
</PinchGestureHandler> | ||
|
||
<PanGestureHandler onHandlerStateChange={eventHandler}> | ||
<View> | ||
<Text {...ghTagEventMacro()}>NestedGestureHandlerTest1</Text> | ||
<TapGestureHandler onHandlerStateChange={eventHandler}> | ||
<Text {...ghTagEventMacro()}>NestedGestureHandlerTest2</Text> | ||
</TapGestureHandler> | ||
</View> | ||
</PanGestureHandler> | ||
|
||
</GestureHandlerRootView> | ||
); | ||
}; | ||
|
||
test('test fireTapGestureHandler', () => { | ||
const eventFunctions = mockEventFunctions(); | ||
const { getByText } = render(<App eventFunctions={eventFunctions} />); | ||
fireTapGestureHandler(getByText('TapGestureHandlerTest')); | ||
assertEventCalls(eventFunctions); | ||
}); | ||
|
||
test('test firePanGestureHandler', () => { | ||
const eventFunctions = mockEventFunctions(); | ||
const { getByText } = render(<App eventFunctions={eventFunctions} />); | ||
firePanGestureHandler( | ||
getByText('PanGestureHandlerTest'), | ||
{ | ||
configBegin: { x: 1, y: 1 }, | ||
configProgress: [{ x: 2, y: 2 }, { x: 3, y: 3 }], | ||
configEnd: { x: 4, y: 4 } | ||
}, | ||
); | ||
assertEventCalls(eventFunctions, { progress: 2 }); | ||
}); | ||
|
||
test('test fireLongPressGestureHandler', () => { | ||
const eventFunctions = mockEventFunctions(); | ||
const { getByText } = render(<App eventFunctions={eventFunctions} />); | ||
fireLongPressGestureHandler( | ||
getByText('LongPressGestureHandlerTest'), | ||
{ configBegin: { x: 1, y: 1 } }, | ||
); | ||
assertEventCalls(eventFunctions); | ||
}); | ||
|
||
test('test fireRotationGestureHandler', () => { | ||
const eventFunctions = mockEventFunctions(); | ||
const { getByText } = render(<App eventFunctions={eventFunctions} />); | ||
fireRotationGestureHandler( | ||
getByText('RotationGestureHandlerTest'), | ||
{ | ||
configBegin: { | ||
rotation: 0, | ||
velocity: 0, | ||
anchorX: 0, | ||
anchorY: 0, | ||
}, | ||
configProgress: { | ||
rotation: 5, | ||
velocity: 5, | ||
anchorX: 5, | ||
anchorY: 5, | ||
}, | ||
configEnd: { | ||
rotation: 0, | ||
velocity: 0, | ||
anchorX: 0, | ||
anchorY: 0, | ||
}, | ||
} | ||
); | ||
assertEventCalls(eventFunctions); | ||
}); | ||
|
||
test('test fireFlingGestureHandler', () => { | ||
const eventFunctions = mockEventFunctions(); | ||
const { getByText } = render(<App eventFunctions={eventFunctions} />); | ||
fireFlingGestureHandler( | ||
getByText('FlingGestureHandlerTest'), | ||
{ configBegin: { x: 1, y: 1 } }, | ||
); | ||
assertEventCalls(eventFunctions); | ||
}); | ||
|
||
test('test firePinchGestureHandler', () => { | ||
const eventFunctions = mockEventFunctions(); | ||
const { getByText } = render(<App eventFunctions={eventFunctions} />); | ||
firePinchGestureHandler( | ||
getByText('PinchGestureHandlerTest'), | ||
{ configBegin: { x: 1, y: 1 } }, | ||
); | ||
assertEventCalls(eventFunctions); | ||
}); | ||
|
||
test('test nestedGestureHandler', () => { | ||
const eventFunctions = mockEventFunctions(); | ||
const { getByText } = render(<App eventFunctions={eventFunctions} />); | ||
firePanGestureHandler(getByText('NestedGestureHandlerTest1')); | ||
firePanGestureHandler(getByText('NestedGestureHandlerTest2')); | ||
fireTapGestureHandler(getByText('NestedGestureHandlerTest2')); | ||
assertEventCalls(eventFunctions, { begin: 3, progress: 3, end: 3 }); | ||
}); | ||
|
||
const AppAPIv2 = props => { | ||
const tap = Gesture.Tap() | ||
.onBegin(() => { | ||
props.eventFunctions.begin() | ||
}) | ||
.onEnd(() => { | ||
props.eventFunctions.progress() | ||
}); | ||
|
||
const pan = Gesture.Pan() | ||
.onBegin(() => { | ||
props.eventFunctions.begin() | ||
}) | ||
.onEnd(() => { | ||
props.eventFunctions.progress() | ||
}); | ||
|
||
return ( | ||
<GestureHandlerRootView> | ||
<GestureDetector gesture={Gesture.Race(tap, pan)}> | ||
<View> | ||
<Text {...ghTagEventMacro()}>Text</Text> | ||
</View> | ||
</GestureDetector> | ||
</GestureHandlerRootView> | ||
); | ||
}; | ||
|
||
test('test API v2', () => { | ||
const eventFunctions = mockEventFunctions(); | ||
const {getByText} = render(<AppAPIv2 eventFunctions={eventFunctions} />); | ||
fireTapGestureHandler(getByText('Text')); | ||
firePanGestureHandler(getByText('Text')); | ||
expect(eventFunctions.begin).toHaveBeenCalledTimes(2); | ||
expect(eventFunctions.progress).toHaveBeenCalledTimes(2); | ||
}); |
This file contains 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,7 @@ | ||
module.exports = { | ||
presets: [ | ||
'@babel/preset-env', | ||
'@babel/preset-typescript', | ||
'module:metro-react-native-babel-preset', | ||
], | ||
}; |
This file contains 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,9 @@ | ||
module.exports = { | ||
preset: 'react-native', | ||
setupFiles: ['./jestSetup.js'], | ||
setupFilesAfterEnv: ['@testing-library/jest-native/extend-expect'], | ||
transformIgnorePatterns: [ | ||
"node_modules/?!(react-native-reanimated)", | ||
"node_modules/?!(react-native)" | ||
] | ||
}; |
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -32,6 +32,7 @@ import { tapGestureHandlerProps } from '../TapGestureHandler'; | |
import { State } from '../../State'; | ||
import { EventType } from '../../EventType'; | ||
import { ComposedGesture } from './gestureComposition'; | ||
import { decorateChildrenWithTag } from '../../jestUtils'; | ||
|
||
const ALLOWED_PROPS = [ | ||
...baseGestureHandlerWithMonitorProps, | ||
|
@@ -416,6 +417,28 @@ export const GestureDetector: React.FunctionComponent<GestureDetectorProps> = ( | |
); | ||
} | ||
|
||
// @ts-ignore @typescript-eslint/ban-ts-comment | ||
if (process.env.JEST_WORKER_ID) { | ||
for (const handler of gesture) { | ||
const handlers = handler.handlers; | ||
// eslint-disable-next-line react-hooks/rules-of-hooks | ||
const reaGestureHandler = Reanimated.useAnimatedGestureHandler({ | ||
onStart: handlers.onBegin, | ||
onActive: handlers.onUpdate, | ||
onEnd: handlers.onEnd, | ||
onFinish: handlers.onFinalize, | ||
}); | ||
const handlerProperties = { | ||
handlerType: handler.handlerName, | ||
handlerTag: handler.handlerTag, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a potential problem, as |
||
onGestureEvent: reaGestureHandler, | ||
onHandlerStateChange: reaGestureHandler, | ||
}; | ||
// @ts-ignore @typescript-eslint/ban-ts-comment | ||
decorateChildrenWithTag({ props }, handlerProperties); | ||
} | ||
} | ||
|
||
if (preparedGesture.firstExecution) { | ||
gestureConfig?.initialize?.(); | ||
} | ||
|
This file contains 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.
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.
I'm not sure if emulating the new API using the old one is the best approach.