簡體   English   中英

通過 Jest 在 React 中測試 componentDidUpdate 中的 prevProps

[英]Test prevProps in componentDidUpdate in React via Jest

具有以下代碼:

componentDidUpdate(prevProps: JsonInputProps) {
  if (prevProps.value !== this.props.value) {
    this.validateJsonSchema(this.props.value || '');
  }
}

和測試代碼:

  it('componentDidUpdate should mount', () => {
    const onChange = jest.fn();
    const event = { target: { value: 'simulate-event' } };
    const wrapper = enzyme
      .shallow(<JsonInput onChange={onChange} onValueChange={mockOnValueChange}/>)
      .simulate('change', event);
    wrapper.setProps({ value: 'example' });
    expect(onChange).toBeCalled;
  });

和測試覆蓋率:

在此處輸入圖像描述

我得到了“未采用其他路徑”,我不想忽略其他路徑,但不知道如何更改道具。 任何想法?

為了覆蓋其他情況,您可以傳遞相同的道具值以及可能的其他內容,以便在更新時覆蓋其他情況。

  it('componentDidUpdate should mount', () => {
    const onChange = jest.fn();
    const event = { target: { value: 'simulate-event' } };
    const wrapper = enzyme
      .shallow(<JsonInput onChange={onChange} onValueChange={mockOnValueChange}/>)
      .simulate('change', event);
    wrapper.setProps({ foo: 'something' });
    expect(onChange).toBeCalled;
  });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM