Created
September 25, 2017 07:59
-
-
Save skayred/d85cd0e9c3b590f47a498d49e1195cfa to your computer and use it in GitHub Desktop.
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 { mount } from 'enzyme'; | |
import { Provider } from 'react-redux'; | |
import thunk from 'redux-thunk'; | |
import configureStoreMock from 'redux-mock-store'; | |
import { configureStore } from '../redux/store'; | |
import { client } from '../../client/client'; | |
import { createMakeRequest } from './network_helper'; | |
import { requestMiddleware } from './request_helper'; | |
const mockContext = { | |
makeRequest: createMakeRequest(client, 'mock.backend-content.dev2.cian.ru', 'http', ''), | |
}; | |
const middlewares = [ | |
(thunk as any).withExtraArgument(mockContext), | |
requestMiddleware(mockContext), | |
]; | |
const mockStore = configureStoreMock(middlewares); | |
/** Render Component */ | |
const renderComponent = <P, S>(Component: React.ComponentClass<P> | React.SFC<P>, | |
props?: P, state?: S, context?: any, contextType?: any) => { | |
function addContext() { | |
class ComponentWithContext extends React.Component<{}, void> { | |
public static childContextTypes: React.ValidationMap<void> = contextType; | |
public getChildContext() { | |
return context; | |
} | |
public render() { | |
return <Component {...(props as any)} />; | |
} | |
} | |
return <ComponentWithContext />; | |
} | |
return mount( | |
<Provider store={configureStore(mockContext, null)}> | |
{(context && contextType) ? addContext() : <Component {...(props as any)} />} | |
</Provider>, | |
); | |
}; | |
export { mockStore, renderComponent }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment