The React Native Testing Library is designed to simulate the core behaviors of React Native. However, it does not replicate the internal logic of third-party libraries. This guide explains how to integrate your library with RNTL.
RNTL provides two subsystems to simulate events:
In many third-party libraries, event handling involves native code, which means RNTL cannot fully simulate the event flow, as it runs only JavaScript code. To address this limitation, you can use testOnly_on*
props on host components to expose custom events to RNTL’s event subsystems. Both subsystems will first attempt to locate the standard on*
event handlers; if these are not available, they fall back to the testOnly_on*
handlers.
React Native Gesture Handler (RNGH) provides a composite Pressable component with onPress*
props. These event handlers are not exposed on the rendered host views; instead, they are invoked via RNGH’s internal event flow, which involves native modules. As a result, they are not accessible to RNTL’s event subsystems.
To enable RNTL to interact with RNGH’s Pressable
component, the library exposes testOnly_onPress*
props on the NativeButton
host component rendered by Pressable
. This adjustment allows RNTL to simulate interactions during testing.