Add testing-library compatibility props#3357
Conversation
| * Used for testing-library compatibility, not passed to the native component. | ||
| */ | ||
| // eslint-disable-next-line @typescript-eslint/ban-types | ||
| testOnly_onPress?: Function | null; |
There was a problem hiding this comment.
What kind of argument should be passed to testOnly_onPress?
There was a problem hiding this comment.
testOnly_onPress will currently always be of type ((event: PressableEvent) => void) | null, but this may change if more of our components will implement testOnly_onPress.
I've made it of type Function, because RawButtonProps usage is not specific to Pressable.
Link to type definitions: (link)
Extract from type definitions:
export type InnerPressableEvent = {
changedTouches: InnerPressableEvent[];
identifier: number;
locationX: number;
locationY: number;
pageX: number;
pageY: number;
target: number;
timestamp: number;
touches: InnerPressableEvent[];
force?: number;
};
export type PressableEvent = { nativeEvent: InnerPressableEvent };
export interface PressableProps
extends AccessibilityProps,
Omit<ViewProps, 'children' | 'style' | 'hitSlop'> {
[...]
onPress?: null | ((event: PressableEvent) => void);
onPressIn?: null | ((event: PressableEvent) => void);
onPressOut?: null | ((event: PressableEvent) => void);
[...]
}| ? children({ pressed: pressedState }) | ||
| : children; | ||
|
|
||
| const inJestEnv = typeof jest !== 'undefined'; |
There was a problem hiding this comment.
Perhaps we should use process.env.NODE_ENV === 'test'; as more typical option for checking test env.
There was a problem hiding this comment.
We already have isJest function (defined here). Can't we use that?
There was a problem hiding this comment.
NODE_ENV === 'test' also works with Mocha and Jasmine, while isJest which uses JEST_WORKER_ID only works with Jest. I think it'd actually be worth changing isJest function to the implementation present in this PR.
There was a problem hiding this comment.
In the future RNTL would like to support other test runners, like vitest or bun. If you hardcode to Jest instead of testing, this would break in such case.
There was a problem hiding this comment.
In that case why don't we change isJestEnv implementation instead of adding new check?
There was a problem hiding this comment.
I agree the isJestEnv implementation can be changed, mentioned this in #3357 (comment) as well.
Done in f13e7b0
mdjastrzebski
left a comment
There was a problem hiding this comment.
Generally looks good, I've left some recommended tweaks
….com/software-mansion/react-native-gesture-handler into @latekvo/add-testing-library-support
|
Note on c63a561 - I tried using |
|
|
||
| // @ts-ignore Adding type definitions for @types/node causes multiple conflicts with react-native/types, | ||
| // and as far as i know, there is no simple way of automatically resolving them. | ||
| const inTestEnv = process.env.NODE_ENV === 'test'; |
There was a problem hiding this comment.
Can we use this
react-native-gesture-handler/src/utils.ts
Lines 37 to 40 in 5b535c9
There was a problem hiding this comment.
THis would tie the implementation only to Jest test runner. In case of user using other test runners Mocha, Vitest, Bun (not all of these are supported by RNTL atm) this props will not be set. The process.env.NODE_ENV === 'test'; should support all test runners.
|
@latekvo could you also expose |
|
I've implemented RNTL counterpart here: callstack/react-native-testing-library#1741 |
| testOnly_onPress={isTestEnv() ? onPress : undefined} | ||
| testOnly_onPressIn={isTestEnv() ? onPressIn : undefined} | ||
| testOnly_onPressOut={isTestEnv() ? onPressOut : undefined}> |
There was a problem hiding this comment.
We could do something similar to Reanimated, i.e. call this only once and store result in constant
const IS_TEST_ENV = isTestEnv()There was a problem hiding this comment.
I though about that, but isTestEnv() is just a property read, and both notations are just as readable, so i don't think there's a point in caching the value.
There was a problem hiding this comment.
Well, it calls hasProperty and then compares values, so it's more than just property read. While it's not crucial in test environment, I think it is better to memoize this value.
@mdjastrzebski sure, done in 6184078 |
|
@latekvo could you make a release with this PR? |
|
@mdjastrzebski, FYI, this PR has just been released in 2.23.0. |
Description
This PR adds 3 props to the
Pressableused for compatibility with react-native-testing-libraryFull discussion: callstack/react-native-testing-library#1738
Test plan