反应: 16.0.0
酶: 3.2.0
Jest: 21.2.1
以下测试不起作用。我还写下了我已经尝试过的无效代码的列表(在评论中)。
import React from 'react';
import { configure, mount, shallow, render } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });
/* ... */
it('enter valid data for connection; enable proxy; check if button is disabled', () => {
const component = getComponent(mount); // mount from enzyme
enterCredentials(component);
const proxySelect = component.find('select');
// currently tried these variants:
proxySelect.simulate('change', { target: { value: true } } );
// component.find('select').simulate('select', true);
// component.find('select').simulate('select', 'Yes');
// component.find('select').simulate('change', { target: { value: true } } );
// /* Mutating: */
// proxySelect.node.selectedIndex = 1;
//
// Producing this error:
//
// Attempted to access ReactWrapper::node, which was previously a private property on
// Enzyme ReactWrapper instances, but is no longer and should not be relied upon.
// Consider using the getElement() method instead.
//
// at ReactWrapper.get (node_modules/enzyme/build/ReactWrapper.js:1689:15)
expect(proxySelect.prop('value')).toBe(true);
expect(component.find('button').prop('disabled')).toBe(false);
});
选择元素的标记:
<select>
<option value="false">No</option>
<option value="true">Yes</option>
</select>
在github上发现了一个问题:https://github.com/airbnb/enzyme/issues/389 但该问题的解决方案也不起作用。 你如何在测试中改变选择的值?
答案 0 :(得分:0)
我认为问题出在“事件” value
的类型上,它应该是像这样的字符串:
// notice the quotes around "true"
proxySelect.simulate('change', { target: { value: 'true' } } );
答案 1 :(得分:0)
尝试这种方法
wrapper.find('option').at(0).instance().selected = false;
wrapper.find('option').at(1).instance().selected = true;