代码之家  ›  专栏  ›  技术社区  ›  josemigallas

酶反应包装未更新

  •  0
  • josemigallas  · 技术社区  · 6 年前

    我正在用酶和Jest测试React应用程序。该应用程序具有一组表示api列表的复选框。每次单击这样的复选框(触发onChange)时,都会将其服务id添加到应用程序的状态中,以便呈现输入 选中的 .

    state = {
      checkedServicesIds: [] // Array<number>
    }
    

    规格:

    it.only('should check and uncheck services when clicked', () => {
      wrapper.setProps({ [{ id: 0, name: 'The Super API' }, { id: 1, name: 'Cool Villains Hub' }] })
      wrapper.setState({ checkedServicesIds: [] })
    
      // Gettings wrappers for each checkbox
      const input0 = wrapper.find(`input#service_ids_${0}`)
      const input1 = wrapper.find(`input#service_ids_${1}`)
    
      expect(input0.prop('checked')).toBe(false) // --> passes
      expect(input1.prop('checked')).toBe(false) // --> passes
    
      input0.simulate('change') // Here id: 0 is added to the array
    
      // wrapper.update() and/or input0.update() does nothing here
    
      expect(wrapper.state('checkedServicesIds')).toEqual([0]) // --> passes
    
      // Now here's the thing: the old wrapper did not update
      expect(wrapper.find(`input#service_ids_${0}`).prop('checked')).toBe(true) // --> passes
      expect(input0.prop('checked')).toBe(true) // --> Expected: true, Received: false !!!
    })
    

    我不是在用吗 update 正确地?

    1 回复  |  直到 6 年前
        1
  •  0
  •   nubinub    6 年前

    我想你需要更新包装。

    wrapper.update();
    

    enzyme documentation :

    注意:只能在也是根实例的包装器实例上调用。