玩笑覆盖-switch语句中的if语句

时间:2019-02-13 14:19:22

标签: reactjs jestjs code-coverage

我正在尝试实现100%的测试覆盖率,而且似乎无法确定为什么我的测试未涵盖此代码

代码:

    case "Premier":
        {
            if(usagePercentage !== '100%'){
                return (
                    <div className='messageText__paid'>
                        <p className='messageText__paid__revenueGenerated'>Revenue generated this cycle: ${revenue}</p>
                        <p className='messageText__paid__review'>Love boop? <a href=''>Leave a review</a></p>
                    </div>
                    )
            }
            if(usagePercentage === '100%'){
                return (
                    <div className='messageText__paid__limitCap'>
                        <p>You’ve hit your usage cap for your current plan, contact us</p>
                    </div>
                    )
            }
        }

测试:

let generatedMessage = shallow(generateMessage('Premier', 1756.11, '100%'))

        it('should generate the plan cap warning', () => {
            expect(generatedMessage.hasClass('messageText__paid__limitCap')).toEqual(true)
        })
        let generatedMessage = shallow(generateMessage('Premier', 1756.11, '55%'))

        it('should generate the paid plan message', () => {
            expect(generatedMessage.hasClass('messageText__paid')).toEqual(true)
        })

1 个答案:

答案 0 :(得分:1)

您似乎还需要测试语句中的第一个分支;如果useagePercentage不是100%,会发生什么情况。

类似这样的东西:

let otherGeneratedMessage = shallow(generateMessage('Premier', 1756.11, '50%'))

    it('should generate revenue cycle message', () => {
        expect(otherGeneratedMessage.hasClass('messageText__paid')).toEqual(true)
    })