【问题标题】:Enzym doesn't seem to read updated state of a React component correctlyEnzym 似乎没有正确读取 React 组件的更新状态
【发布时间】:2018-05-15 07:27:01
【问题描述】:

我测试了一个 React 组件:

onDateSelect(newDate) {
    const date = moment(this.state.date);
    const { day, month, year } = newDate;

    date.date(day);
    date.month(month-1);
    date.year(year);

    this.setState({
        date,
        value: date.format(DATE_FORMAT)
    });
}

与:

test('Method onDateSelect', () => {
    const wrapper = shallow(<DatePickerField date={ moment('2022-11-22') } />)

    wrapper.instance().onDateSelect({ day: 23, month: 11, year: 2022 });  // Run it once because of an issue with testing moment in Jest

    expect(wrapper.state().date).toBe(moment('2022-11-23'));
})

Jest 返回一个错误,虽然没有:

Expected value to be (using ===):
  "2022-11-22T23:00:00.000Z"
Received:
  "2022-11-21T23:00:00.000Z"

Difference:

- Expected
+ Received

- "2022-11-22T23:00:00.000Z"
+ "2022-11-21T23:00:00.000Z"

我在 CET 时区,因此两者都显示提前一小时。但酶态仍包含旧值。该功能运行良好。

我错过了什么?

【问题讨论】:

    标签: reactjs components state jestjs enzyme


    【解决方案1】:

    如果要调用实例方法并与状态交互,则需要使用mount而不是shallow

    【讨论】:

    • 我不能使用 mount :-(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    • 2019-03-10
    • 2021-05-15
    • 1970-01-01
    相关资源
    最近更新 更多