[英]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.