2024-06-27 23:31:59 +02:00
|
|
|
import { RenderOptions, render } from "@testing-library/react-native";
|
|
|
|
import { PropsWithChildren, ReactElement } from "react";
|
|
|
|
import { Provider } from "react-redux";
|
|
|
|
import { setupStore, RootState } from "@/app/store";
|
2024-07-06 00:00:08 +02:00
|
|
|
import { Product } from "@/lib/product";
|
|
|
|
import { ProductData } from "./product";
|
2024-06-27 23:31:59 +02:00
|
|
|
|
|
|
|
export interface ExtendedRenderOptions extends Omit<RenderOptions, 'queries'> {
|
|
|
|
preloadedState?: Partial<RootState>;
|
|
|
|
store?: any; // TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
export function renderWithProviders(
|
|
|
|
ui: ReactElement,
|
2024-06-28 15:04:50 +02:00
|
|
|
preloadedState = {
|
2024-06-30 18:37:27 +02:00
|
|
|
products: [] as ProductData []
|
2024-06-28 15:04:50 +02:00
|
|
|
},
|
2024-06-27 23:31:59 +02:00
|
|
|
extendedRenderOptions: ExtendedRenderOptions = {},
|
|
|
|
) {
|
|
|
|
const {
|
|
|
|
// Automatically create a store instance if no store was passed in
|
2024-06-28 23:53:48 +02:00
|
|
|
store = setupStore(preloadedState),
|
2024-06-27 23:31:59 +02:00
|
|
|
...renderOptions
|
|
|
|
} = extendedRenderOptions;
|
|
|
|
|
|
|
|
const Wrapper = ({ children }: PropsWithChildren) => (
|
|
|
|
<Provider store={store}>{children}</Provider>
|
|
|
|
);
|
|
|
|
|
|
|
|
// Return an object with the store and all of RTL's query functions
|
|
|
|
return {
|
|
|
|
store,
|
|
|
|
...render(ui, { wrapper: Wrapper, ...renderOptions })
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|