mercredi 24 octobre 2018

Test recompose funtions

I'm developping a react application & I'm using Jest to test my functions.

But I'm finding difficulties testing these kind of functions where I'm using recompose.

When I analyse my code coverage. It shows that withProps & initForm inside lifecycle are not tested.

export const initForm = ({ anParams, visibilityFields, init }) => {
  if (!anParams) {
    init(visibilityFields);
  }
};

export const enhance = compose(
  connect(
    mapStateToProps,
    mapDispatchToProps,
  ),
  withProps(({ annuities }) => ({
    anOptions: annuities.map(({ label, code }) => ({
      label,
      value: code,
    })),
  })),
  lifecycle({
    componentDidMount() {
      initForm(this.props);
    },
    componentDidUpdate() {
      initForm(this.props);
    },
  }),
);

This is my test:

describe('initForm', () => {
  it('should call init if params is not set', () => {
    const init = jest.fn();
    initForm({
      init,
      anParams: undefined,
      visibilityFields: {
        computationDateVisible: true,
      },
    });
    expect(init).toHaveBeenCalledWith({
      computationDateVisible: true,
    });
  });

  it('should not call init if params is set', () => {
    const init = jest.fn();
    initForm({
      init,
      anParams: {
        computationDate: {
          viewValue: '01/01/2018',
          errorMessage: null,
        },
      },
      visibilityFields: {
        computationDateVisible: true,
      },
    });

    expect(init).toHaveBeenCalledTimes(0);
  });
});

Aucun commentaire:

Enregistrer un commentaire