복붙노트

[REACTJS] 효소는 OnChange 이벤트를 시뮬레이션

REACTJS

효소는 OnChange 이벤트를 시뮬레이션

해결법


  1. 1.당신은 단순히 프로토 타입을 통해 직접 방법으로 감시 할 수 있습니다.

    당신은 단순히 프로토 타입을 통해 직접 방법으로 감시 할 수 있습니다.

    it("responds to name change", done => {
      const handleChangeSpy = sinon.spy(New.prototype, "handleChange");
      const event = {target: {name: "pollName", value: "spam"}};
      const wrap = mount(
        <New />
      );
      wrap.ref('pollName').simulate('change', event);
      expect(handleChangeSpy.calledOnce).to.equal(true);
    })
    

    또는, 인스턴스의 방법에 스파이를 사용할 수 있지만, 당신은 이미 마운트 한 후 렌더링되는 구성 요소가 이미 원래에 바인딩 onchange를 의미하는 호출되기 때문에 강제 업데이 트를해야한다.

    it("responds to name change", done => {
      const event = {target: {name: "pollName", value: "spam"}};
      const wrap = mount(
        <New />
      );
      const handleChangeSpy = sinon.spy(wrap.instance(), "handleChange");
      wrap.update(); // Force re-render
      wrap.ref('pollName').simulate('change', event);
      expect(handleChangeSpy.calledOnce).to.equal(true);
    })
    
  2. from https://stackoverflow.com/questions/43426885/enzyme-simulate-an-onchange-event by cc-by-sa and MIT license