输入onChange不更新玩笑和酶的状态

时间:2020-01-27 12:10:52

标签: reactjs jestjs enzyme

[{"p_id" : 1,"author": "X1","editor": "X2"},{"p_id" : 2, "author": "Y1"},{"p_id" : 3,"author": "Z1","editor": "Z2"},{...
print("Vector: ", cvss3_v.group())
import { BrowserRouter as Router } from "react-router-dom";

wrapper.state('email')应该返回了email@email.com,但未更新。任何原因?

1 个答案:

答案 0 :(得分:1)

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

emailInput.simulate("change", {
        target: {
          id: "your id here"
          value: "email@email.com"
        }
      });

此外,由于更新是异步完成的,因此在执行任何断言之前,您应该等待诺言。

定义一个flushPromises函数:

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

然后等待测试中的承诺:

test("Rendered email input and test valid", () => {
  const email = "";
  const wrapper = setup(null, { email });
  const emailInput = findByTestAttribute(
    wrapper,
    someId
  );
  emailInput.simulate("change", { target: { id: "your id here", value: "email@email.com" } });
  await flushPromises; // await for promises to return
  console.log(wrapper.state('email')) //Empty ""
  expect(wrapper.state("email")).toBe("email@email.com");
});