import React , { ComponentType } from 'react' ; import { Store } from 'redux' ; import { ReactWrapper } from 'enzyme' ; import { act } from 'react-dom/test-utils' ; import { mountWithIntl } from '../enzyme_helpers' ; import { WithMemoryRouter , WithRoute , WithStore } from '../helpers' ; import { MemoryRouterConfig } from './types' ; interface Config { Component : ComponentType ; memoryRouter : MemoryRouterConfig ; store : Store | null ; props : any ; onRouter : ( router : any ) => void ; } const getCompFromConfig = ( { Component , memoryRouter , store , onRouter } ) : ComponentType => { const wrapWithRouter = memoryRouter . wrapComponent !== false ; let Comp = store !== null ? WithStore ( store ) ( Component ) : Component ; if ( wrapWithRouter ) { const { componentRoutePath , initialEntries , initialIndex } = memoryRouter ! ; Comp = WithMemoryRouter ( initialEntries , initialIndex ) ( WithRoute ( componentRoutePath , onRouter ) ( Comp ) ) ; } return Comp ; } ; export const mountComponentSync = ( config ) : ReactWrapper => { const Comp = getCompFromConfig ( config ) ; return mountWithIntl ( < Comp { ... config . props } / > ) ; } ; export const mountComponentAsync = async ( config ) : Promise < ReactWrapper > => { const Comp = getCompFromConfig ( config ) ; let component ; await act ( async ( ) => { component = mountWithIntl ( < Comp { ... config . props } / > ) ; } ) ; return component . update ( ) ; } ; export const getJSXComponentWithProps = ( Component , props ) => ( < Component { ... props } / > ) ;