如何解决使用mocha或chai的window.location.href url更新测试
我正在尝试编写一个测试用例,以验证window.location.href中以下代码的内容
class MyDevice extends Component {
constructor(props) {
super(props);
this.state = {
device: window.location.href.split('?param=')[1],};
}
componentDidMount() {
if (this.state.device) {
this.onDeviceNameEntered();
}
}
onDeviceNameEntered = async () => {
const DeviceName = this.state.device;
if (window.location.href.split('?param=')[1] !== DeviceName) {
window.location.href = window.location.href.split('?param=')[0] + '?param=' + DeviceName;
}
document.getElementById('deviceName').value = this.state.targetdevice;
// an axios request is done and response is obtained
//visualize the response
}
我编写的测试用例失败,因为在window.location.href中获得的值被当作其他随机值。
Test Case written using mocha,chai and enzyme
describe('urlUpdate',() => {
it('renders correctly',() => {
window.location.href = 'https://www.example.com/?param=DeviceA';
const wrapper = mount(<MyDevice />);
wrapper.setState({ device: 'DeviceA' });
expect(window.location.href.split('?param=')[1]).to.equal('DeviceA');
});
});
有人可以帮助我了解如何在this.state.device更改时测试url是否正在更新 我需要测试何时拥有网址https://www.example.com/?param=DeviceA,将document.getElementById('deviceName')。value更改为DeviceA 另外,当我将#deviceName(这是一个输入文本框)更改为DeviceB时, window.location.href已更新为https://www.example.com/?param=DeviceB
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。