Advertisement
Guest User

Untitled

a guest
Jan 11th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as Redux from 'redux';
  2. import configureStore, { MockStore } from 'redux-mock-store';
  3.  
  4. /**
  5.  * Returns mock store.
  6.  * @param state Initial state
  7.  * @param middlewares Store middlewares.
  8.  * @returns Redux mock store.
  9.  */
  10. export const createMockStore = (state: object = {}, middlewares?: Redux.Middleware[]): MockStore<object> => {
  11.     const mockStore = configureStore(middlewares || []);
  12.  
  13.     return mockStore({ ...{}, ...state });
  14. };
  15.  
  16. // ------------------------------------------------------------------------------------------------
  17. import * as React from 'react';
  18. import { connect } from 'react-redux';
  19. import { AnyAction, bindActionCreators, Dispatch } from 'redux';
  20.  
  21. class Test extends React.Component<{ testProp: string, myProp?: string, testA: () => void }> {
  22.     render () {
  23.         return React.createElement(
  24.             'div',
  25.             null,
  26.             `Hello ${this.props.myProp} - ${this.props.testProp}`,
  27.         );
  28.     }
  29. }
  30.  
  31. const mapStateToProps = (state: { testProp: string, myProp: string }): { testProp: string } =>
  32.     ({ testProp: state.testProp });
  33.  
  34. const mapDispatchToProps = (dispatch: Dispatch<AnyAction>): { testA: () => void } => (
  35.     bindActionCreators({ testA: () => ({ type: 'A' })}, dispatch)
  36. );
  37.  
  38. export default connect(mapStateToProps, mapDispatchToProps)(Test);
  39.  
  40. // ------------------------------------------------------------------------------------------------
  41.  
  42.  
  43. import { shallow } from 'enzyme';
  44. import * as React from 'react';
  45. import { createMockStore } from 'test/utils';
  46. import ConnectedTestCmp from '../../test';
  47.  
  48. describe('test', () => {
  49.     it('test', () => {
  50.         const store = createMockStore({ testProp: 'Teeessttttt' });
  51.         const cmp1 = shallow(<ConnectedTestCmp myProp="world" />, { context: { store }});
  52.  
  53.         // @ts-ignore TS2345
  54.         const cmp = React.createElement(ConnectedTestCmp, { myProp: 'world' });
  55.         const cmp2 = shallow(cmp, { context: { store }});
  56.  
  57.         expect(cmp1.prop('myProp')).toEqual('world');
  58.         expect(cmp1.prop('testProp')).toEqual('Teeessttttt');
  59.         expect(cmp2.prop('myProp')).toEqual('world');
  60.         expect(cmp2.prop('testProp')).toEqual('Teeessttttt');
  61.     });
  62. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement