How to Unit Test React-Redux Connected Components?

前端 未结 5 2160
礼貌的吻别
礼貌的吻别 2020-11-27 14:40

I am using Mocha, Chai, Karma, Sinon, Webpack for Unit tests.

I followed this link to configure my testing environment for React-Redux Code.

How to implement

5条回答
  •  情歌与酒
    2020-11-27 15:24

    Try creating 2 files, one with component itself, being not aware of any store or anything (PhoneVerification-component.js). Then second one (PhoneVerification.js), which you will use in your application and which only returns the first component subscribed to store via connect function, something like

    import PhoneVerificationComponent from './PhoneVerification-component.js'
    import {connect} from 'react-redux'
    ...
    export default connect(mapStateToProps, mapDispatchToProps)(PhoneVerificationComponent)
    

    Then you can test your "dumb" component by requiring PhoneVerification-component.js in your test and providing it with necessary mocked props. There is no point of testing already tested (connect decorator, mapStateToProps, mapDispatchToProps etc...)

提交回复
热议问题