Last active
August 4, 2021 13:29
-
-
Save aprofromindia/3855efe8b9efb38727153caf21906d66 to your computer and use it in GitHub Desktop.
Typescript Redux Store bypassing Singleton pattern for testing React Container Components.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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