Dave Bergschneider
Dave Bergschneider

Reputation: 413

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}

Upvotes: 0

Views: 486

Answers (1)

felixmosh
felixmosh

Reputation: 35553

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');
  });
});

Upvotes: 1

Related Questions