Should I test contrary cases?

i am using tdd and i not sure if i should test contrary cases or only expected case.

test 1

test('should render loading placeholder if isloading = true', () => {
    const wrapper = shallow(
      <OrderDetails id="10" />
    );
    wrapper.setState({ isLoading: true });
    expect(wrapper.find(LoadingPlaceholder).length).toBe(1);
})

test 2

test('should not render loading placeholder if isloading = false', () => {
    const wrapper = shallow(
      <OrderDetails id="10" />
    );
    wrapper.setState({ isLoading: false});
    expect(wrapper.find(LoadingPlaceholder).length).toBe(0);
})

component

export default class OrderDetails extends PureComponent {
  state = {
    isLoading: true,
  }
  props: Props;
  render() {
    return (
      <div>
        {this.state.isLoading && (
           <LoadingPlaceholder />
        )}
      </div>
    )
  }
}

notes if only add the first test can pass the test with this for example unexpecected pass test

export default class OrderDetails extends PureComponent {
  state = {
    isLoading: true,
  }
  props: Props;
  render() {
    return (
      <div>
         <LoadingPlaceholder />
      </div>
    )
  }
}

Upvotes: 0

Views: 52

Answers (1)

EricSchaefer
EricSchaefer

Reputation: 26370

You should have at least as many tests as possible (distinguishable) states for the unit under test. If there are two states (with isLoading being true or false) then you also need two tests, one for each possible state.

To put it differently and more formal: The number of tests for a unit should be greater or equal to the cyclomatic complexity of the unit.

Upvotes: 1

Related Questions