This is a very minimal example of using the fantastic react-testing-library to test React Native components.
The basic premise is to alias react-native to react-native-web so that components can be rendered in a DOM and operated on by the functions in react-testing-library (credits to James Long).
-
The
__tests__folder contains a single test for theCountercomponent. It tests for rendered text and simulates anonPress()event. -
Whatever is included with
react-native initand:jestreact-testing-libraryreact-native-webreact-domnode-noop
-
{ "preset": "react-native", "transform": { "^.+\\.jsx?$": "<rootDir>/node_modules/react-native/jest/preprocessor.js" }, "testEnvironment": "jsdom", "moduleNameMapper": { // alias imports of react-native to react-native-web "^react-native$": "react-native-web", // block react-art "^react-art$": "node-noop" } }
-
You can simulate a press event as follows:
fireEvent['press'] = (node, init) => { fireEvent.mouseDown(node, init) fireEvent.mouseUp(node, init) } ... fireEvent.press(getByText("Tap here"))