I am trying to write a test case to verify the content in window.location.href for the below code
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
}
The test case I wrote is failing because the value obtained in window.location.href is taken as some other random value.
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');
});
});
Could someone help me understand how to test if url is getting updated when this.state.device changes
Aucun commentaire:
Enregistrer un commentaire