Last active
January 24, 2020 07:05
-
-
Save sketchbuch/b71b3f4dad1b193445cdaa4ff9fb6f52 to your computer and use it in GitHub Desktop.
TESTING - Shallow render with Redux and genric Typescript
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 * as React from 'react' | |
import configureMockStore from 'redux-mock-store' | |
import { Provider } from 'react-redux' | |
import { shallow, ShallowWrapper } from 'enzyme' | |
export type StringObject = { [key: string]: string } | |
export type StoreData = { [key: string]: string | StringObject } | |
export interface RenderShallowWithReduxOptions<T> { | |
children?: React.ReactNode | |
props?: Partial<T> | |
storeData?: StoreData | |
} | |
const renderShallowWithRedux = <T extends {}>( | |
Component: React.ComponentType, | |
{ children = null, props = {}, storeData = {} }: RenderShallowWithReduxOptions<T> = { | |
props: {}, | |
storeData: {}, | |
} | |
): ShallowWrapper => { | |
const mockStore = configureMockStore([]) | |
const store = mockStore(storeData) | |
store.dispatch = jest.fn() | |
if (children) { | |
return shallow( | |
<Provider store={store}> | |
<Component {...props}>{children}</Component> | |
</Provider> | |
) | |
} | |
return shallow( | |
<Provider store={store}> | |
<Component {...props} /> | |
</Provider> | |
) | |
} | |
export default renderShallowWithRedux |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment