Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 输入onChange不更新jest和Ezyme中的状态_Reactjs_Jestjs_Enzyme - Fatal编程技术网

Reactjs 输入onChange不更新jest和Ezyme中的状态

Reactjs 输入onChange不更新jest和Ezyme中的状态,reactjs,jestjs,enzyme,Reactjs,Jestjs,Enzyme,wrapper.state('email')应已返回email@email.com但是它没有更新。有什么原因吗?您需要在模拟事件中传递id: test("Rendered email input and test valid", () => { const email = ""; const wrapper = setup(null, { email }); const emailInput = findByTestAttribute( wrapper, som

wrapper.state('email')应已返回email@email.com但是它没有更新。有什么原因吗?

您需要在模拟事件中传递
id

test("Rendered email input and test valid", () => {
  const email = "";
  const wrapper = setup(null, { email });
  const emailInput = findByTestAttribute(
    wrapper,
    someId
  );
  emailInput.simulate("change", { target: { value: "email@email.com" } });
  console.log(wrapper.state('email')) //Empty ""
  expect(wrapper.state("email")).toBe("email@email.com");
});
此外,由于更新是异步完成的,所以在执行任何断言之前,您应该等待您的承诺

定义
flushPromises
功能:

emailInput.simulate("change", {
        target: {
          id: "your id here"
          value: "email@email.com"
        }
      });
然后在测试中等待承诺:

function flushPromises() {
  return new Promise(resolve => setImmediate(resolve));
}

您需要在模拟事件中传递
id

test("Rendered email input and test valid", () => {
  const email = "";
  const wrapper = setup(null, { email });
  const emailInput = findByTestAttribute(
    wrapper,
    someId
  );
  emailInput.simulate("change", { target: { value: "email@email.com" } });
  console.log(wrapper.state('email')) //Empty ""
  expect(wrapper.state("email")).toBe("email@email.com");
});
此外,由于更新是异步完成的,所以在执行任何断言之前,您应该等待您的承诺

定义
flushPromises
功能:

emailInput.simulate("change", {
        target: {
          id: "your id here"
          value: "email@email.com"
        }
      });
然后在测试中等待承诺:

function flushPromises() {
  return new Promise(resolve => setImmediate(resolve));
}

onChange函数看起来像什么?onChangeHandler=id=>async event=>{this.setState({[id]:event.target.value});}onChange函数看起来像什么?onChangeHandler=id=>async event=>{this.setState({[id]:event.target.value});}不工作..不知道路由器或存储是否与模拟事件有关。抱歉@Ismael Padilla不幸的是,它不工作。我已将onChange更改为onChangeHandler=event=>{this.setState({email:event.target.value});}仍然不工作不工作..不知道路由器或存储是否与模拟事件有关。抱歉@Ismael Padilla不幸的是它不工作我已将onChange更改为onChangeHandler=event=>{this.setState({email:event.target.value});}仍然不工作