input onChange不会在玩笑和酶中更新状态

问题描述 投票:0回答:1
import { BrowserRouter as Router } from "react-router-dom";
const setup = (props = {}, state = null,initialState={}) => {
  const store = storeFactory(initialState);
  const wrapper = mount(
  <Router
     <Component {...props} store={store}/>
  </Router>
  );
  if (state) wrapper.setState(state);
  return wrapper;
};
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 protected]" } });
  console.log(wrapper.state('email')) //Empty ""
  expect(wrapper.state("email")).toBe("[email protected]");
});

wrapper.state('email')应该已经返回[email protected],但未更新。任何原因?

reactjs enzyme jest
1个回答
0
投票

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

emailInput.simulate("change", {
        target: {
          id: "your id here"
          value: "[email protected]"
        }
      });
© www.soinside.com 2019 - 2024. All rights reserved.