简体   繁体   中英

How do i unit test using mocha/chai/sinon if a variable has a react component as value?

I have a variable which gets dynamically assigned a React component based on a condition is true or false.

if(isValid) {icon = <ErrorIcon className: "error"/>}
else {icon = <SuccessIcon className: "success"/>

how do i test for expected value of icon is one component or other in unit test?

Not sure how reliable this API but each react component instance has a type property, so you can check it against the component definition:

const isErrorIcon = icon.type === ErrorIcon

So in your case:

expect(icon.type).to.equal(ErrorIcon)
if (icon.type === SuccessIcon) {

} else if (icon.type === ErrorIcon) {

}

Should do the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM