我正在尝试实现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)
})
答案 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)
})