【问题标题】:What should the second param of `simulate` accept?`simulate` 的第二个参数应该接受什么?
【发布时间】:2018-10-23 17:30:54
【问题描述】:

我知道在simulate 我们通常可以这样做:

simulate("change", { target: { value: '7' } });

但是如果我的onChange 函数接受一个对象作为参数,我应该在第二个参数中传递什么?

interface myObject{
  firstname: string;
  lastname: string;
}

myChangeFunction(item: myObject) {
   /* */
}

请注意我试过了:

const updates = { firstname: "john", lastname: "doe" };
simulate("change", { target: updates });

and

simulate("change", updates );

但它失败了。

【问题讨论】:

  • 能否分享一下“change”事件函数的实现。
  • 它只是用item.firstname 更新另一个状态属性。为了简洁起见,我把它省略了。

标签: javascript reactjs typescript jestjs enzyme


【解决方案1】:

simulate 函数模拟事件,您的 myChangeFunction 期望 item 作为参数而不是 event

不确定您是如何在组件中定义 onChange,但如果您尝试仅将界面更改为以下内容:

interface myObject {
  target: {
    firstname: string;
    lastname: string;
  }
}

myChangeFunction(item: myObject) {
   // here should output { firstname: "john", lastname: "doe" }
   console.log(item.target);  
}

const updates = { firstname: "john", lastname: "doe" };
simulate("change", { target: updates });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    • 2013-04-03
    • 1970-01-01
    • 2021-06-19
    • 2010-11-29
    • 1970-01-01
    相关资源
    最近更新 更多