0

I have componentDidMount which have one const. I am trying to cover unit test case for const which have ternary operator. Can anyone please help me on this how to cover ternary operator condition.

componentDidMount() {
    const learnerId = (this.props.routeParam) ?
            this.props.routeParam.learnerId :
            MaterialModuleList.getQueryString('learnerId');
}
Mayank Shukla
  • 100,735
  • 18
  • 158
  • 142

1 Answers1

0

I suppose you are using Enzyme with Jest to write tests. If you aren't already, checkout Enzyme.

You need two cases to achieve 100% coverage. First, mount the component the component with the routeParam, Second, mount the component by setting routeParam to false. Alternatively, you can use the setProps method (from enzyme) for the latter case.

component.setProps({
    routeParam: false
});

Here, for the latter case, you can MaterialModuleList.getQueryString method to assert whether it was called.

For the first case however, you CANNOT write an assertion(The test will achieve 100% coverage though). To write an assertion, you should either return the const, or set it to some global variable, which you have access to.

Pubudu Dodangoda
  • 2,742
  • 2
  • 22
  • 38