Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import * as Redux from 'redux';
- import configureStore, { MockStore } from 'redux-mock-store';
- /**
- * Returns mock store.
- * @param state Initial state
- * @param middlewares Store middlewares.
- * @returns Redux mock store.
- */
- export const createMockStore = (state: object = {}, middlewares?: Redux.Middleware[]): MockStore<object> => {
- const mockStore = configureStore(middlewares || []);
- return mockStore({ ...{}, ...state });
- };
- // ------------------------------------------------------------------------------------------------
- import * as React from 'react';
- import { connect } from 'react-redux';
- import { AnyAction, bindActionCreators, Dispatch } from 'redux';
- class Test extends React.Component<{ testProp: string, myProp?: string, testA: () => void }> {
- render () {
- return React.createElement(
- 'div',
- null,
- `Hello ${this.props.myProp} - ${this.props.testProp}`,
- );
- }
- }
- const mapStateToProps = (state: { testProp: string, myProp: string }): { testProp: string } =>
- ({ testProp: state.testProp });
- const mapDispatchToProps = (dispatch: Dispatch<AnyAction>): { testA: () => void } => (
- bindActionCreators({ testA: () => ({ type: 'A' })}, dispatch)
- );
- export default connect(mapStateToProps, mapDispatchToProps)(Test);
- // ------------------------------------------------------------------------------------------------
- import { shallow } from 'enzyme';
- import * as React from 'react';
- import { createMockStore } from 'test/utils';
- import ConnectedTestCmp from '../../test';
- describe('test', () => {
- it('test', () => {
- const store = createMockStore({ testProp: 'Teeessttttt' });
- const cmp1 = shallow(<ConnectedTestCmp myProp="world" />, { context: { store }});
- // @ts-ignore TS2345
- const cmp = React.createElement(ConnectedTestCmp, { myProp: 'world' });
- const cmp2 = shallow(cmp, { context: { store }});
- expect(cmp1.prop('myProp')).toEqual('world');
- expect(cmp1.prop('testProp')).toEqual('Teeessttttt');
- expect(cmp2.prop('myProp')).toEqual('world');
- expect(cmp2.prop('testProp')).toEqual('Teeessttttt');
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement