I am new to unit testing and I ran into the this case. I am using JEST and Enzyme - REACT JS
I am familiar calling clicks and onChange events but not sure how to set up a test for the following:
updateUser = (e) => {
var tempUser = this.state.user;
switch(e.target.id){
case "firstName":
tempUser.FirstName = e.target.value;
break;
case "lastName":
tempUser.LastName = e.target.value;
break;
case "email":
tempUser.Email = e.target.value;
break;
case "userName":
tempUser.Username = e.target.value;
break;
default:
break;
}
this.setState({
user: tempUser,
})
}
So I tried to apply the same set up I have been using to test updateUser - not sure if its the correct approach.
describe(' Test', () => {
let wrapper;
beforeEach(() => wrapper = shallow(<Component {...baseProps} />));
it('UpdateUser method', () => {
wrapper.instance().updateUser = jest.fn();
wrapper.setState({
user:{
tempUser :{
FirstName: "",
LastName:"",
Email:"",
Username:"",
},
},
}),
wrapper.update();
expect(wrapper.instance().updateUser).toBeDefined();
expect(wrapper.state().user).toEqual({});
})
Thanks for the help - hope to learn how to test switch cases and get this test to pass.