I am working on writing unit test cases and achieving code coverage for the code snippet below. Any advice on how to proceed?
itemClick($event: any) {
for (let obj of this.tocFiles) {
let results = this.getchildren(obj, label);
if (results) {
console.log(results, 'results');
this.clickedItem = results.children;
break;
}
}
}
getchildren(obj: any, label: any) {
if (obj.label == label) {
return obj;
}
if (obj.children) {
for (let item of obj.children) {
let check: any = this.getchildren(item, label);
if (check) {
return check;
}
}
}
return null;
}
Here is a sample test case that I have written,
it('When clicking on breadcrumb value, itemClick() should be called',()=>{
const onClick = spyOn(component, 'itemClick');
component.itemClick(undefined);
expect(onClick).toHaveBeenCalled();
});
However, I am facing difficulty in getting this code covered under code coverage.