I am having the class named SelectedValues like below which has a global variable "this.others"
export default class SelectedValues extends React.Component {
/* istanbul ignore next */
constructor(props) {
super(props);
this.renderOption = this.renderOption.bind(this);
this.deselectOption = this.deselectOption.bind(this);
this.others= [];
this.state = {
typedText: '',
index: null,
typedContext: '',
};
}
deselectOption(event, option, index){
const selectedOptions = _.clone(this.props.value);
let selectedOptionsValue = _.split(selectedOptions.value, ';');
selectedOptionsValue.splice(index, 1);
console.log(this.others);
let othersIndex = this.others[index].OtherIndex;
let typedUpdateContext = '';
if(othersIndex === 0){
let temp = this.others.find(item => item.OtherIndex === 1);
typedUpdateContext = temp? temp.value : '';
}
_.remove(this.others, { 'key': option + index });
}
render(){
return(...);
}
}
The code is working fine but when I ran the test file by using npm run test for the above "deselectOption" method and I am getting error like below
TypeError: Cannot read property 'OtherIndex' of undefined
Later I found the issue is causing because of this.others(which is a global variable) and it is an empty array. So how to mock "this.others" before i call deselectOption Method in test file.
I am new in writing test cases for unit test cases. Please help me to resolve this error.
Thank you in advance.