I have my tests passing at the moment. I just want to double check if this is the correct way to check a method. If not, please make the correction if possible
This is what I have so far : file.test.js
it ('handleChange: should call correctly ',() => {
const wrapper = shallow(<Component {...baseProps } />);
expect(wrapper).toMatchSnapshot();
wrapper.setState({e: 'test'});
expect(wrapper.instance().handleChange({target: {value : 'id'}}))
expect(wrapper.instance().handleChange({target: {name : 'key'}}))
});
it('deleteAxis : should call correctly',() => {
const wrapper = shallow(<Component {...baseProps } />);
expect(wrapper).toMatchSnapshot();
wrapper.setState({});
expect(wrapper.instance().deleteAxis({ id:{} }))
})
This is part of the main file. File.js
handleChange = (e) => {
let localState = {}
let key = e.target.name
let value = e.target.value
localState[key] = value
localState['id'] = this.props.id
this.props.addNewAxis(localState)
this.setState(localState)
}
deleteAxis = () => {
this.props.deleteAxisByID(this.props.id)
}
I expect both methods to be tested and pass the right way. I have both working at the moment but unsure if its correct. Thanks
You can move repetitive code into a describe block's
beforeEach
function and you can check and make sureaddNewAxis
anddeleteNewAxisByID
are being called with the correct values. Lastly, yourhandleChange
function is a bit confusing because of the naming convention, instead try to keep it one to one.You can change your
handleChange
function to:Then you can test the methods and subsequent function calls:
this is what I usually do when I am writing an unit test file