Skip to content

Instantly share code, notes, and snippets.

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