fix rendering and storage issues for unit tests.
This commit is contained in:
36
lib/rendering.tsx
Normal file
36
lib/rendering.tsx
Normal file
@ -0,0 +1,36 @@
|
||||
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 } from "@/lib/product";
|
||||
import React from "react";
|
||||
|
||||
export interface ExtendedRenderOptions extends Omit<RenderOptions, 'queries'> {
|
||||
preloadedState?: Partial<RootState>;
|
||||
store?: any; // TODO
|
||||
}
|
||||
|
||||
export function renderWithProviders(
|
||||
ui: ReactElement,
|
||||
preloadedState = {
|
||||
products: [] as Product []
|
||||
},
|
||||
extendedRenderOptions: ExtendedRenderOptions = {},
|
||||
) {
|
||||
const {
|
||||
// Automatically create a store instance if no store was passed in
|
||||
store = setupStore(preloadedState as Partial<RootState>),
|
||||
...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 })
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user