我该如何进行分支测试,即使用笑话和酶进行SWITCH案例,IF和ELSE语句?

时间:2019-07-13 08:20:24

标签: javascript reactjs testing jestjs enzyme

我正在测试项目中的“卡片”组件。在类方法“ _handleInputValue”和“ _handleAuthorizeCardDetails”中,我有一系列嵌套的If和Else语句以及SWITCH案例。如何测试这些方法和其中嵌套的If Else语句? 下面是类方法

_handleInputValue (e) {
        let inputField = e.target.name;
        console.log(inputField)
        if (inputField === 'pin'){
            console.log('pin')
            console.log(e.target.value.length)
            if (e.target.value.length > 4){
                return;
            }
            this.setState({
                [e.target.name] : e.target.value,
            });
        }
        
        if (inputField === 'otp'){
            console.log('otp')
            this.setState({
                [e.target.name] : e.target.value,
            });
            return;
        }
        
}
    

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.1.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.1.0/umd/react-dom.production.min.js"></script>


 _handleAuthorizeCardDetails(){
        let paymentInfo = this.props.paymentInfo;
        let url = this.state.url
        axios({
            headers: {
                'crossDomain': true,
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': '*',
            },
            method: 'post',
            url: url,
            data: {
                    data: data,
                }  
        })
        .then( res => {
            console.log(res);
            let data = res.data; 
            if(data.status == 'pending') {
                switch(data.action) {
                    case 'authorize':
                        this.setState({
                                showCardDetails: false,
                                showCardOtp: false,
                                showCardPin: true
                        })
                        break;
                        
                    case 'validate': 
                        this.setState({
                                showCardDetails: false,
                                showCardOtp: true,
                                showCardPin: false,
                                otpField: data.data
                        })                        
                        break;
                        
                    case 'redirect': 
                        window.open(this.state.url + `/redirect/${paymentInfo.payment_code}`);                                                                            
                        break;
                    default:
                        this.setState({
                                showCardDetails: true,
                                showCardOtp: false,
                                showCardPin: false
                        })
                }
                
                return;

            }else if(data.status == 'complete') {
                console.log('trasaction successful')

            }else {  
                
                let message = data.message;
                console.log(message) 
              
                
            }
        })
        .catch( err => {
            console.log(err);

        })
    
    }

0 个答案:

没有答案