Skip to content

Instantly share code, notes, and snippets.

@sketchbuch
Last active January 24, 2020 07:05
Show Gist options
  • Save sketchbuch/b71b3f4dad1b193445cdaa4ff9fb6f52 to your computer and use it in GitHub Desktop.
Save sketchbuch/b71b3f4dad1b193445cdaa4ff9fb6f52 to your computer and use it in GitHub Desktop.
TESTING - Shallow render with Redux and genric Typescript
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