Redux: How to test a connected component?

前端 未结 3 794
耶瑟儿~
耶瑟儿~ 2020-12-24 03:56

I am using Enzyme to unit test my React components. I understand that in order to test the raw unconnected component I\'d have to just export it and test it (I\

3条回答
  •  失恋的感觉
    2020-12-24 04:14

    If we have a router issue, we can consider to add the router lib into the test file, eg:

    import React from 'react';
    import { Provider } from 'react-redux';
    import { BrowserRouter as Router } from 'react-router-dom';
    import { mount } from 'enzyme';
    import ReadDots from './ReadDots';
    
    const storeFake = state => ({
      default: () => {
      },
      subscribe: () => {
      },
      dispatch: () => {
      },
      getState: () => ({ ...state })
    });
    
    const store = storeFake({
      dot: {
        dots: [
          {
            id: '1',
            dot: 'test data',
            cost: '100',
            tag: 'pocket money'
          }
        ]
      }
    });
    
    describe('', () => {
      it('should render ReadDots component', () => {
        const component = mount(
          
            
              
            
          
        );
        expect(component.length).toEqual(1);
      });
    });
    

提交回复
热议问题