我想编写一个测试来检查我何时更改 React 应用程序中选择元素的值。
import React, { Component } from 'react';
const TimeList =(props) =>{
return(
<div>
<label>
Time
<br/>
<select name="lessonTime" value={props.defaultTime} onChange={props.handleChange}>
<option value="8:00">8:00</option>
<option value="8:30">8:30</option>
<option value="9:00">9:00</option>
<option value="10:00">10:00</option>
<option value="12:00">12:00</option>
<option value="13:30">13:30</option>
<option value="19:00">19:00</option>
<option value="19:30">19:30</option>
</select>
</label>
</div>
);
};
export default TimeList;
我的测试代码:
it('should select correct time',() =>{
const mockFunc = jest.fn();
const wrapper = mount(<TimeList value='10:00'
onChange={mockFunc}/>)
console.log(wrapper.props());
wrapper.find('select').simulate('change',{target:{value:'8:00'}});
expect(wrapper.find('select').props().value).toBe('8:00');
});
我得到的错误是:
Expected value to be (using ===):
"8:00"
Received:
undefined
Difference:
Comparing two different types of values. Expected string but received undefined.
似乎我还没有理解如何测试 select 元素。
关于如何创建这种测试的任何想法?