I am new to unit testing with Jest. I am testing a component in a React app. There are two components: Home
and LogOutButton
This is The Home component:
import 'bootstrap/dist/css/bootstrap.css';
import 'bootstrap/dist/css/bootstrap-theme.css';
import React from 'react';
import ReactDOM from 'react-dom';
import { LogOutButton } from './LogOutButton.js';
class Home extends React.Component {
displayName = Home.name;
state = {
result: 0,
val1: 0,
val2: 0,
};
handleChangeOne = event => {
this.setState({ val1: event.target.value });
};
handleChangeTwo = event => {
this.setState({ val2: event.target.value });
};
add = () => {
this.setState({
result: parseInt(this.state.val1) + parseInt(this.state.val2)
});
};
onLogoutClick = () => {
window.location.href = 'https://www.MICROSOFT.com';
}
render() {
return (
<div>
<h1>Hello world! The result is: {this.state.result}</h1>
<input type="text" onChange={this.handleChangeOne} />
+
<input type="text" onChange={this.handleChangeTwo} />
= <br />
<button onClick={this.add}>Add</button>
<br/><br/>
<LogOutButton onLogout={this.onLogoutClick} />
</div>
);
}
}
export default Home;
const rootElement = document.getElementById("root");
ReactDOM.render(<Home />, rootElement);
Home
component can get 2 numbers and render their sum. Where it also imports LogOutButton
Component and when it is clicked, it should redirect you to Microsoft website.
I want to test a scenario where the user click LogOut
.
This is my suggestion:
describe('Home />', () => {
it('Directing to Microsoft site when LogOut is clicked', () => {
const homeWrapper = shallow(<Home />);
homeWrapper.find('LogOutButton').simulate('click');
homeWrapper.update();
expect(homeWrapper.html).toEqual('https://www.MICROSOFT.com');
});
}
This does not work.. I am looking for an assistance to write the right test method. Thanks in advance