Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/handlers/createHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import deepEqual from 'lodash/isEqual';
import RNGestureHandlerModule from '../RNGestureHandlerModule';
import type RNGestureHandlerModuleWeb from '../RNGestureHandlerModule.web';
import { State } from '../State';
import { handlerIDToTag, getNextHandlerTag } from './handlersRegistry';
import {
handlerIDToTag,
getNextHandlerTag,
unregisterJestHandler,
registerJestHandler,
} from './handlersRegistry';

import {
BaseGestureHandlerProps,
Expand All @@ -21,6 +26,7 @@ import {
findNodeHandle,
} from './gestureHandlerCommon';
import { ValueOf } from '../typeUtils';
import { isJest } from '../utils';

const UIManagerAny = UIManager as any;

Expand Down Expand Up @@ -152,7 +158,8 @@ export default function createHandler<

constructor(props: T & InternalEventHandlers) {
super(props);
this.handlerTag = getNextHandlerTag();
this.handlerTag =
isJest() && props.testId ? props.testId : getNextHandlerTag();
this.config = {};
this.propsRef = React.createRef();
this.state = { allowTouches };
Expand Down Expand Up @@ -220,6 +227,10 @@ export default function createHandler<
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete handlerIDToTag[handlerID];
}

if (isJest()) {
unregisterJestHandler(this.handlerTag);
}
}

private onGestureHandlerEvent = (event: GestureEvent<U>) => {
Expand Down Expand Up @@ -293,6 +304,10 @@ export default function createHandler<
false
);
}

if (isJest()) {
registerJestHandler(this.handlerTag, this);
}
};

private updateGestureHandler = (
Expand All @@ -301,6 +316,10 @@ export default function createHandler<
this.config = newConfig;

RNGestureHandlerModule.updateGestureHandler(this.handlerTag, newConfig);

if (isJest()) {
registerJestHandler(this.handlerTag, this);
}
};

private update() {
Expand Down
1 change: 1 addition & 0 deletions src/handlers/gestureHandlerCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export type BaseGestureHandlerProps<
id?: string;
waitFor?: React.Ref<unknown> | React.Ref<unknown>[];
simultaneousHandlers?: React.Ref<unknown> | React.Ref<unknown>[];
testId?: number;
// TODO(TS) - fix event types
onBegan?: (event: HandlerStateChangeEvent) => void;
onFailed?: (event: HandlerStateChangeEvent) => void;
Expand Down
20 changes: 19 additions & 1 deletion src/handlers/gestures/GestureDetector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import {
CALLBACK_TYPE,
} from './gesture';
import { Reanimated, SharedValue } from './reanimatedWrapper';
import { registerHandler, unregisterHandler } from '../handlersRegistry';
import {
registerHandler,
registerJestHandler,
unregisterHandler,
unregisterJestHandler,
} from '../handlersRegistry';
import RNGestureHandlerModule from '../../RNGestureHandlerModule';
import {
baseGestureHandlerWithMonitorProps,
Expand All @@ -32,6 +37,7 @@ import { tapGestureHandlerProps } from '../TapGestureHandler';
import { State } from '../../State';
import { EventType } from '../../EventType';
import { ComposedGesture } from './gestureComposition';
import { isJest } from '../../utils';

const ALLOWED_PROPS = [
...baseGestureHandlerWithMonitorProps,
Expand Down Expand Up @@ -76,6 +82,10 @@ function dropHandlers(preparedGesture: GestureConfigReference) {
RNGestureHandlerModule.dropGestureHandler(handler.handlerTag);

unregisterHandler(handler.handlerTag);

if (isJest()) {
unregisterJestHandler(handler.handlerTag);
}
}
}

Expand Down Expand Up @@ -147,6 +157,10 @@ function attachHandlers({
viewTag,
!useAnimated // send direct events when using animatedGesture, device events otherwise
);

if (isJest()) {
registerJestHandler(gesture.handlerTag, gesture);
}
}

if (preparedGesture.animatedHandlers) {
Expand Down Expand Up @@ -198,6 +212,10 @@ function updateHandlers(
);

registerHandler(handler.handlerTag, handler);

if (isJest()) {
registerJestHandler(handler.handlerTag, handler);
}
}

if (preparedGesture.animatedHandlers) {
Expand Down
11 changes: 10 additions & 1 deletion src/handlers/gestures/gesture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { PinchGestureHandlerEventPayload } from '../PinchGestureHandler';
import { RotationGestureHandlerEventPayload } from '../RotationGestureHandler';
import { TapGestureHandlerEventPayload } from '../TapGestureHandler';
import { NativeViewGestureHandlerPayload } from '../NativeViewGestureHandler';
import { isJest } from '../../utils';

export type GestureType =
| BaseGesture<Record<string, unknown>>
Expand All @@ -41,6 +42,7 @@ export interface BaseGestureConfig
simultaneousWith?: GestureRef[];
needsPointerData?: boolean;
manualActivation?: boolean;
testId?: number;
}

type TouchEventHandlerType = (
Expand Down Expand Up @@ -246,8 +248,15 @@ export abstract class BaseGesture<
return this;
}

withTestId(id: number) {
this.config.testId = id;
return this;
}

initialize() {
this.handlerTag = getNextHandlerTag();
this.handlerTag =
isJest() && this.config.testId ? this.config.testId : getNextHandlerTag();

this.handlers = { ...this.handlers, handlerTag: this.handlerTag };

if (this.config.ref) {
Expand Down
15 changes: 15 additions & 0 deletions src/handlers/handlersRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,18 @@ export function unregisterHandler(handlerTag: number) {
export function findHandler(handlerTag: number) {
return handlers.get(handlerTag);
}

// store all gestures and handlers for Jest
const jestHandlers = new Map<number, any>();

export function registerJestHandler(handlerTag: number, handler: any) {
jestHandlers.set(handlerTag, handler);
}

export function unregisterJestHandler(handlerTag: number) {
jestHandlers.delete(handlerTag);
}

export function findJestHandler(handlerTag: number) {
return jestHandlers.get(handlerTag);
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { initialize } from './init';

export { findJestHandler } from './handlers/handlersRegistry';

export { Directions } from './Directions';
export { State } from './State';
export { default as gestureHandlerRootHOC } from './gestureHandlerRootHOC';
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ export function toArray<T>(object: T | T[]): T[] {

return object;
}

export function isJest(): boolean {
return !!process.env.JEST_WORKER_ID;
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"esModuleInterop": true,
"jsx": "react-native",
"lib": ["esnext"],
"types": [],
"types": ["node"],
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
Expand Down