简体   繁体   中英

how to unit test a basic ternary using karma-mocha/chai-enzyme?

At work we are using Karma-Mocha with Chai-Enzyme to unit test our react code. I'm hoping some one could give me a basic example on how to unit test a basic ternary.

Example ternary such as this:

{myVariable === null ? 'DefaultString' : myVariable}

I'm assuming that the ternary expression is part of some JSX element.

So you can use Enzyme's text method in order to extract the text of the element.

 import mount from 'enzyme'; describe('example text', () => { it('should show', () => { const wrapper = mount( <MyComp> ); expect(wrapper.text()).to.be('DefaultString'); }); }); 

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