Under the hood we are using @testing-library/react-native to render the component for the testing environment. This means that you can use the query methods that you would use with @testing-library/react-native.
Remember in your tests to always try to resemble how the user interacts with the UI as much as possible.
You can read more on how to query on the official docs here.
Query priority: getByRole > getByLabelText > getByPlaceholderText > getByText > getByTestId. Use queryBy* only for checking that something is not there, and findBy* for anything that appears asynchronously.
We use @testing-library/react-native v14. Rendering and events are async, and queries only see host elements (View, Text, TextInput…):
awaiteveryrenderWithWrappers,renderWithRelay,renderHook,rerender,unmount,fireEvent.*andactcall, andawaitthemockResolveLastOperation/mockRejectLastOperationhelpers. ESLint'sno-floating-promisescatches a missingawaitin test files.- Don't wrap
mockResolveLastOperation/mockRejectLastOperationinact(they already use it), and don't call them inside awaitForcallback (waitForretries it). Wait for the operation first, then resolve it. - Don't import
react-test-renderer. Importactfrom@testing-library/react-native. - Don't use
UNSAFE_*queries,UNSAFE_root,*ByA11yHint, or tree walking. Don't callelement.props.onPress()directly. UsefireEvent.press(element). - Assert on what the user sees (text, role, label,
testID) rather than on a child component's props. - Text has to be rendered inside
<Text>, including in mocks.
const Foo = () => {
const data = useLazyLoadQuery(graphql`
query FooQuery {
me {
name
}
}
`)
return <Text>{data.me.name}</Text>
}
export const FooScreen = () => {
return (
<Suspense fallback={<Flex testID="loading" />}>
<Foo />
</Suspense>
)
}
const { renderWithRelay } = setupTestWrapper<FooQuery>({
Component: FooScreen,
})
it("Displays the user's profile name", async () => {
await renderWithRelay({
Me: () => ({ name: "name" }),
})
expect(await screen.findByText("name")).toBeOnTheScreen()
expect(screen.queryByTestId("loading")).not.toBeOnTheScreen()
})const Bar = () => {
const data = useFragment(graphql`
fragment Bar_me on Me {
name
}
`)
return <Text>{data.name}</Text>
}
const { renderWithRelay } = setupTestWrapper<BarTestQuery>({
Component: Bar,
query: graphql`
query BarTestQuery @relay_test_operation {
me {
...Bar_me
}
}
`,
})
it("Displays the user's profile name", async () => {
await renderWithRelay({
Me: () => ({ name: "Mock Name" }),
})
expect(screen.getByText("Mock Name")).toBeOnTheScreen()
})TODO: add more examples from the codebase
The setupTestWrapper function is a testing helper designed to simplify the testing of components that use Relay for data fetching in a React/ React Native environment. It provides a way to render components under different Relay query configurations, allowing you to test various scenarios efficiently and easily.
| Parameter | Description |
|---|---|
Component |
The React component you want to test. This component should be typed with the expected response shape for your GraphQL query. |
preloaded |
A boolean flag that indicates whether you want to simulate preloaded data. When set to true, you must provide a query as well. Preloading is useful when you want to test scenarios where data is available immediately. |
query |
The Relay query you want to test. This query should be typed with the expected response shape for your GraphQL query. |
variables |
The variables to be used with the GraphQL query. You can provide default values if needed. |
The renderWithRelay function is returned by setupTestWrapper. It can be used to render your component and control the Relay environment for testing purposes.
const { renderWithRelay } = setupTestWrapper({
Component: Foo,
query: graphql`
query FooQuery {
me {
name
}
}
`,
})
const { env, mockResolveLastOperation, mockRejectLastOperation } = await renderWithRelay()| Parameter | Description |
|---|---|
mockResolvers |
The resolvers for your top level component query |
props |
Any initial prop that you want to pass to the component that you are testing. |
| Parameter | Description |
|---|---|
env |
The Relay environment that is used to render your component. |
mockResolveLastOperation |
A relay resolver that resolves the last operation that was executed by the Relay environment. |
mockRejectLastOperation |
A relay resolver that rejects the last operation that was executed by the Relay environment. |
The renderWithWrappers function is a testing helper designed to simplify the testing of components in our React Native testing environment. It provides a way to render components wrapped with our wrappers, allowing you to test various scenarios efficiently and easily.
We use this function when we want to test something that is not related to Relay but needs the rest of the wrappers of our app, for example, a component that uses useNavigation hook.
For Android native code (Java/Kotlin), we use JUnit and MockK for unit testing. The tests are located in android/app/src/test/java/ and follow the standard Android testing patterns.
To run Android native tests during development:
# Run all Android unit tests
yarn android:test
# Or run directly with Gradle
cd android && ./gradlew testDebugUnitTest
# Run specific test class
cd android && ./gradlew testDebugUnitTest --tests "net.artsy.app.widgets.FullBleedWidgetProviderTest"
# Run tests matching a pattern
cd android && ./gradlew testDebugUnitTest --tests "net.artsy.app.widgets.*"Widget tests are located in android/app/src/test/java/net/artsy/app/widgets/:
- FullBleedWidgetProviderTest.kt - Tests core widget functionality
- ArtworkRotationTest.kt - Comprehensive tests for artwork selection and state management
After running tests, HTML reports are generated at:
android/app/build/reports/tests/testDebugUnitTest/index.html
The tests integrate with the existing development workflow and can be run alongside React Native tests in CI/CD pipelines.