|
| 1 | +import { cleanup, render } from '@testing-library/react-native'; |
| 2 | +import React from 'react'; |
| 3 | +import { findNodeHandle, View } from 'react-native'; |
| 4 | + |
| 5 | +import { Gesture, GestureDetector, GestureHandlerRootView } from '../index'; |
| 6 | +import RNGestureHandlerModule from '../RNGestureHandlerModule'; |
| 7 | + |
| 8 | +jest.mock('react-native/Libraries/ReactNative/RendererProxy', () => ({ |
| 9 | + findNodeHandle: jest.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +const VIEW_TAG = 123; |
| 13 | + |
| 14 | +function ChildIgnoringRef(props: { children?: React.ReactNode }) { |
| 15 | + return <View>{props.children}</View>; |
| 16 | +} |
| 17 | + |
| 18 | +class ChildWithHostInstance extends React.Component<{ |
| 19 | + children?: React.ReactNode; |
| 20 | +}> { |
| 21 | + // eslint-disable-next-line @eslint-react/no-unused-class-component-members |
| 22 | + public __internalInstanceHandle = {}; |
| 23 | + |
| 24 | + override render() { |
| 25 | + return <View>{this.props.children}</View>; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +describe('Legacy GestureDetector ref forwarding', () => { |
| 30 | + let attachSpy: jest.SpyInstance; |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + cleanup(); |
| 34 | + jest.clearAllMocks(); |
| 35 | + (findNodeHandle as jest.Mock).mockReturnValue(VIEW_TAG); |
| 36 | + attachSpy = jest.spyOn(RNGestureHandlerModule, 'attachGestureHandler'); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + attachSpy.mockRestore(); |
| 41 | + }); |
| 42 | + |
| 43 | + test('resolves the tag from the child host instance when its ref resolves', () => { |
| 44 | + render( |
| 45 | + <GestureHandlerRootView> |
| 46 | + <GestureDetector gesture={Gesture.Tap()}> |
| 47 | + <ChildWithHostInstance /> |
| 48 | + </GestureDetector> |
| 49 | + </GestureHandlerRootView> |
| 50 | + ); |
| 51 | + |
| 52 | + const resolvedRefs = (findNodeHandle as jest.Mock).mock.calls.map( |
| 53 | + (call) => call[0] |
| 54 | + ); |
| 55 | + |
| 56 | + expect(resolvedRefs.length).toBeGreaterThan(0); |
| 57 | + expect( |
| 58 | + resolvedRefs.every( |
| 59 | + (ref) => ref instanceof ChildWithHostInstance && ref !== null |
| 60 | + ) |
| 61 | + ).toBe(true); |
| 62 | + expect(attachSpy).toHaveBeenCalledWith( |
| 63 | + expect.any(Number), |
| 64 | + VIEW_TAG, |
| 65 | + expect.any(Number) |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + test('attaches gestures when the child ignores its ref', () => { |
| 70 | + render( |
| 71 | + <GestureHandlerRootView> |
| 72 | + <GestureDetector gesture={Gesture.Tap()}> |
| 73 | + <ChildIgnoringRef /> |
| 74 | + </GestureDetector> |
| 75 | + </GestureHandlerRootView> |
| 76 | + ); |
| 77 | + |
| 78 | + const resolvedRefs = (findNodeHandle as jest.Mock).mock.calls.map( |
| 79 | + (call) => call[0] |
| 80 | + ); |
| 81 | + |
| 82 | + expect(resolvedRefs.length).toBeGreaterThan(0); |
| 83 | + expect( |
| 84 | + resolvedRefs.every((ref) => ref instanceof ChildWithHostInstance) |
| 85 | + ).toBe(false); |
| 86 | + expect(attachSpy).toHaveBeenCalledWith( |
| 87 | + expect.any(Number), |
| 88 | + VIEW_TAG, |
| 89 | + expect.any(Number) |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + test('does not clobber a ref the child already has', () => { |
| 94 | + const childRef = jest.fn(); |
| 95 | + |
| 96 | + render( |
| 97 | + <GestureHandlerRootView> |
| 98 | + <GestureDetector gesture={Gesture.Tap()}> |
| 99 | + <View ref={childRef} /> |
| 100 | + </GestureDetector> |
| 101 | + </GestureHandlerRootView> |
| 102 | + ); |
| 103 | + |
| 104 | + expect(childRef).toHaveBeenCalled(); |
| 105 | + expect(childRef.mock.calls[0][0]).not.toBeNull(); |
| 106 | + }); |
| 107 | + |
| 108 | + test('moves the child instance when its ref is replaced', () => { |
| 109 | + const gesture = Gesture.Tap(); |
| 110 | + const firstRef = jest.fn(); |
| 111 | + const secondRef = jest.fn(); |
| 112 | + |
| 113 | + function App({ useSecondRef }: { useSecondRef: boolean }) { |
| 114 | + return ( |
| 115 | + <GestureHandlerRootView> |
| 116 | + <GestureDetector gesture={gesture}> |
| 117 | + <View ref={useSecondRef ? secondRef : firstRef} /> |
| 118 | + </GestureDetector> |
| 119 | + </GestureHandlerRootView> |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + const { rerender } = render(<App useSecondRef={false} />); |
| 124 | + |
| 125 | + const childInstance = firstRef.mock.calls[0][0]; |
| 126 | + expect(childInstance).not.toBeNull(); |
| 127 | + firstRef.mockClear(); |
| 128 | + |
| 129 | + rerender(<App useSecondRef />); |
| 130 | + |
| 131 | + expect(firstRef).toHaveBeenCalledWith(null); |
| 132 | + expect(secondRef).toHaveBeenCalledWith(childInstance); |
| 133 | + }); |
| 134 | + |
| 135 | + test('does not reattach gestures on re-render', () => { |
| 136 | + const gesture = Gesture.Tap(); |
| 137 | + |
| 138 | + function App() { |
| 139 | + return ( |
| 140 | + <GestureHandlerRootView> |
| 141 | + <GestureDetector gesture={gesture}> |
| 142 | + <View /> |
| 143 | + </GestureDetector> |
| 144 | + </GestureHandlerRootView> |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + const { rerender } = render(<App />); |
| 149 | + expect(attachSpy).toHaveBeenCalledTimes(1); |
| 150 | + |
| 151 | + rerender(<App />); |
| 152 | + rerender(<App />); |
| 153 | + |
| 154 | + expect(attachSpy).toHaveBeenCalledTimes(1); |
| 155 | + }); |
| 156 | +}); |
0 commit comments