PliWould/lib/rendering.tsx

36 lines
1.1 KiB
TypeScript

import { RenderOptions, render } from "@testing-library/react-native";
import { PropsWithChildren, ReactElement } from "react";
import { Provider } from "react-redux";
import { setupStore, RootState } from "@/app/store";
import { Product, ProductData } from "@/lib/product";
export interface ExtendedRenderOptions extends Omit<RenderOptions, 'queries'> {
preloadedState?: Partial<RootState>;
store?: any; // TODO
}
export function renderWithProviders(
ui: ReactElement,
preloadedState = {
products: [] as ProductData []
},
extendedRenderOptions: ExtendedRenderOptions = {},
) {
const {
// Automatically create a store instance if no store was passed in
store = setupStore(preloadedState),
...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 })
};
}