开玩笑说,测试一个道具叫做

时间:2020-09-22 15:41:29

标签: reactjs jestjs

我有一个带有道具的组件,这是一个叫做changeReason()

的动作

然后,当我按下一个按钮时,我有一个函数被调用。功能是:

const handleNext = React.useCallback(() => {
  if (!reason) {
    changeReason({
      code: defaultReason,
      value: defaultReason
    });
  }

}

我想测试调用changeReason()道具,我正在尝试进行如下测试:

const changeReason = jest.fn();

const rendered = render(
  <TransactionReason
    changeReason={changeReason}
    reason={null}
  />
);

// When some button is pressed...
const reasonNext = rendered.getByTestId('reasonButton');
// it must fire an event
fireEvent.press(reasonNext);

const FakeFun = jest.spyOn(rendered, 'changeReason');
expect(FakeFun).toHaveBeenCalled();

我得到:

Cannot spy the handleNext property because it is not a function; undefined given instead

我如何测试我正在调用该动作?

0 个答案:

没有答案