Skip to content

Instantly share code, notes, and snippets.

@aprofromindia
Last active August 4, 2021 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aprofromindia/3855efe8b9efb38727153caf21906d66 to your computer and use it in GitHub Desktop.
Save aprofromindia/3855efe8b9efb38727153caf21906d66 to your computer and use it in GitHub Desktop.
Typescript Redux Store bypassing Singleton pattern for testing React Container Components.
import { configureStore } from '@reduxjs/toolkit';
import user from './user';
export default function getStore() {
return configureStore({
reducer: {
user,
},
});
}
export type RootState = ReturnType<ReturnType<typeof getStore>['getState']>;
export type AppDispatch = ReturnType<typeof getStore>['dispatch'];
import { ReactNode } from 'react';
import { render } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import { Provider } from 'react-redux';
import getStore, { AppDispatch } from './redux/store';
export default function renderWithProvider(
children: ReactNode,
...varargs: Parameters<AppDispatch>[0][]
) {
const store = getStore();
for (const arg of varargs) {
store.dispatch(arg);
}
return render(<Provider store={store}>{children}</Provider>);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment