简体   繁体   English

笑话酶模拟价值

[英]Jest Enzyme Simulate Value

I set up my wrapper as follows: 我将包装程序设置如下:

const wrapper = shallow(<Uploads
      availableTemplates={[]}
      filters={{ commodity: '' }}
      actions={{
        SetFilter: (e) => { wrapper.setProps({ filters: e }); }
      }}
    />);

The goal of the following test is to change a radio value of 'checked' from its default false to true: 以下测试的目标是将“ checked”的无线电值从其默认false更改为true:

const input = wrapper.find('input').at(0);

    input.simulate('change', { target: input.props() });
    expect(wrapper.find('input').at(0).node.props.checked).toEqual(true);

Without further ado, here is my question: When I create a test below this to check for the default value of the radio (which should be false), I'm getting true back. 事不宜迟,这是我的问题:当我在此下方创建测试以检查无线电的默认值(应该为false)时,我就回来了。 Presumably this is because I have altered it to true early. 大概是因为我早就将其更改为true。 Is there a way to reset my input to its default state for each subsequent test? 有没有一种方法可以将我的输入重置为每个后续测试的默认状态?

expect(wrapper.find('input').at(0).node.props.checked).toEqual(false);

Of course, I could just move this test above the other one but I am curious as to why the above is happening. 当然,我可以将此测试移到另一个测试之上,但是我很好奇为什么会发生上述情况。

Reason for above seems to be that I was testing the same instance of the wrapper each time. 出现上述原因的原因似乎是我每次都测试相同的包装实例。 So when I changed a value to true that would persist through the rest of my tests. 因此,当我将值更改为true时,它将在其余测试中保持不变。 To counteract this, I created a getWrapper function which I call to get a fresh instance of the wrapper each time: 为了解决这个问题,我创建了一个getWrapper函数,每次调用它都会获取包装器的新实例:

const getWrapper = () => {
  const wrapper = shallow(<Uploads
    availableTemplates={[]}
    filters={{ commodity: '' }}
    actions={{
      SetFilter: (e) => { wrapper.setProps({ filters: e }); }
    }}
  />);
  return wrapper;
};

In test: 在测试中:

const wrapper = getWrapper();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jest酶模拟(&#39;click&#39;)事件对象和值 - jest enzyme simulate('click') with event object and value 无法用玩笑和酶模拟点击菜单 Antd - Unable to simulate click on Menu Antd with jest and enzyme 用酶/笑话模拟不进行Sinn Spy的点击测试 - Simulate Click Test with Enzyme/ Jest Not Calling Sinon Spy 我可以不使用.simulate()来调用方法吗? - Can I call a method without using .simulate() - Jest Enzyme 如何使用Jest,Enzyme for React-Native模拟单元测试中的事件 - How to simulate an event on a unit test with Jest, Enzyme for React-Native 为什么 .simulate(&quot;mouseover&quot;) 在 Jest / Enzyme 测试中不起作用? - Why doesn't .simulate("mouseover") work in a Jest / Enzyme test? 开玩笑和酵素的期望值是(使用===): - Issue with jest and enzyme Expected value to be (using ===): 酶模拟提交表单,无法读取未定义的属性“值” - enzyme simulate submit form, Cannot read property 'value' of undefined 在模拟(“提交”)“ this.props.searchUser不是函数”之后,开玩笑/酶测试失败 - Jest/Enzyme test failing after simulate('submit') “this.props.searchUser is not a function” 在jest / enzyme中,如何使用[element] .addEventListener(而不是window.addEventListener)绑定的eventListener模拟元素上的单击? - In jest/enzyme, how do you simulate a click on an element with eventListener bound by [element].addEventListener (not window.addEventListener)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM