Files
iNaturalistReactNative/tests/helpers/render.js
Johannes Klein 49091f8fc4 Enable React Compiler (#3082)
* Update package.json

* Update package-lock.json

* Enable plugin

* Update package.json

* Update package-lock.json

* Update .eslintrc.js

* Create a new object instead of mutating the existing one

* Update SoundRecorder.js

* Do not mutate prop directly

* Do not mutate store value directly

* Disable compiler for zoom hook

Because it uses reanimated logic, and previous work laid out we should specifically disregard a rule of react here, unsure what to do here for now.

* Move array for tracking results into component

* Disable compiler for StandardCamera

* Do not mutate store value directly

* Disable compiler for FullPageWebView

* Do not mutate route param

* Remove unnecessary check if state is true before setting it to false

* Fix: user might be undefined

* Update reanimated usage with new compiler compliant API

* Fix: taxon might be undefined

* Fix: taxon might be undefined

* Fix: common pitfall of trying to render text outside of Text component

* Update interface

* Add package override

* Update package-lock.json

* Refactor updating of user preferences into User model

* Remove no memo directive from zoom

* Update file imports

* Migrate safeRealmWrite to TS

* Update interface and import path

* Update interface

* Refactor useLocaalObservation hook to not mutate return value of a function that should not be mutated

* Update tests to new return structure

* Remove useSafeRoute hook

We get the same information from error contexts and surrounding documents, and this hook was violating the rules of react by conditionally calling another hook.

* Use optional chaining

* Update TaxonNamesSetting.test.js

* Fix: use optional chaining

* Extend on RNTLs renderHook instead of re-implementing

* Remove unnecessary mock

* Remove unnecessary mock

* Remove unecessary mock

* Remove trailing spaces
2025-09-01 17:06:51 +02:00

147 lines
4.4 KiB
JavaScript

import { BottomSheetModalProvider } from "@gorhom/bottom-sheet";
import { DefaultTheme, NavigationContainer } from "@react-navigation/native";
import {
QueryClient,
QueryClientProvider
} from "@tanstack/react-query";
import { render, renderHook, screen } from "@testing-library/react-native";
import App from "components/App";
import INatPaperProvider from "providers/INatPaperProvider";
import React from "react";
import { GestureHandlerRootView } from "react-native-gesture-handler";
import Observation from "realmModels/Observation";
const queryClient = new QueryClient( {
defaultOptions: {
queries: {
// No need to do default retries in tests
retry: false,
// Prevent `Jest did not exit one second after the test run has completed.` error
// https://react-query-v3.tanstack.com/guides/testing#set-cachetime-to-infinity-with-jest
gcTime: Infinity
}
}
} );
const mockNavigationTheme = {
...DefaultTheme,
// React Navigation 7 has stricter requirements around themes
// fonts.regular in particular causes errors in a few integration tests
// where we're unmocking react-navigation
fonts: {
regular: {
fontFamily: "System",
fontWeight: "normal"
}
}
};
function renderComponent( component, update = null, renderOptions = {} ) {
const renderMethod = update || render;
return renderMethod(
<QueryClientProvider client={queryClient}>
<INatPaperProvider>
<GestureHandlerRootView className="flex-1">
<BottomSheetModalProvider>
<NavigationContainer theme={mockNavigationTheme}>
{ component }
</NavigationContainer>
</BottomSheetModalProvider>
</GestureHandlerRootView>
</INatPaperProvider>
</QueryClientProvider>,
renderOptions
);
}
function renderAppWithComponent( component, update = null ) {
return renderComponent( <App>{ component }</App>, update );
}
function renderApp( update = null ) {
return renderAppWithComponent( null, update );
}
async function renderAppWithObservations(
observations,
realmIdentifier
) {
if ( observations.length > 0 ) {
await Promise.all( observations.map( async observation => {
// If it looks like it was supposed to be unsynced, save it like a new
// local obs
if ( observation.needsSync && observation.needsSync( ) ) {
// Save the mock observation in Realm
return Observation.saveLocalObservationForUpload(
observations[0],
global.mockRealms[realmIdentifier]
);
}
// Otherwise save it like a remote obs
return new Promise( resolve => {
resolve(
Observation.upsertRemoteObservations( [observation], global.mockRealms[realmIdentifier] )
);
} );
} ) );
}
// Render the whole app with all the navigators
renderAppWithComponent( );
// If we don't wait for the obs to render we get errors about things
// happening outside of act(). Most tests will do this anyway, but this
// caused me a lot of confusion when I was trying to debug other problems
// by removing code until I was just rendering the stack navigator... and
// that was still erroring out. Hopefully this will prevent that particular
// point of confusion in the future. ~~~kueda 20240104
await screen.findByTestId( `MyObservations.obsGridItem.${observations[0].uuid}` );
}
function wrapInNavigationContainer( component ) {
return (
<NavigationContainer>
{component}
</NavigationContainer>
);
}
function wrapInQueryClientContainer( component ) {
return (
<QueryClientProvider client={queryClient}>
{component}
</QueryClientProvider>
);
}
const Wrapper = ( { children } ) => (
<QueryClientProvider client={queryClient}>
<INatPaperProvider>
<GestureHandlerRootView className="flex-1">
<BottomSheetModalProvider>
<NavigationContainer theme={mockNavigationTheme}>
<App>
{children}
</App>
</NavigationContainer>
</BottomSheetModalProvider>
</GestureHandlerRootView>
</INatPaperProvider>
</QueryClientProvider>
);
function renderHookInApp( hookToRender ) {
return renderHook( hookToRender, {
wrapper: Wrapper
} );
}
export {
queryClient,
renderApp,
renderAppWithComponent,
renderAppWithObservations,
renderComponent,
renderHookInApp,
wrapInNavigationContainer,
wrapInQueryClientContainer
};