Skip to content

Instantly share code, notes, and snippets.

@bonnie
Last active August 13, 2021 20:26
Show Gist options
  • Save bonnie/180532a9b0f0110c23916b17400c3799 to your computer and use it in GitHub Desktop.
Save bonnie/180532a9b0f0110c23916b17400c3799 to your computer and use it in GitHub Desktop.
import { EnhancedStore } from "@reduxjs/toolkit"; // for redux-toolkit
// import { Store } from 'redux' // for non-toolkit
import {
render as rtlRender,
RenderOptions,
RenderResult,
} from "@testing-library/react";
import { ReactElement, ReactNode } from "react";
import { Provider } from "react-redux";
import { configureStoreWithMiddlewares, RootState } from "../store";
type ReduxRenderOptions = {
preloadedState?: RootState;
store?: EnhancedStore; // for redux-toolkit
// store?: Store // for non-toolkit
renderOptions?: Omit<RenderOptions, "wrapper">;
};
function render(
ui: ReactElement,
{
preloadedState = {},
store = configureStoreWithMiddlewares(preloadedState),
...renderOptions
}: ReduxRenderOptions = {}
): RenderResult {
function Wrapper({ children }: { children?: ReactNode }): ReactElement {
return <Provider store={store}>{children}</Provider>;
}
return rtlRender(ui, { wrapper: Wrapper, ...renderOptions });
}
// re-export everything
export * from "@testing-library/react";
// override render method
export { render };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment