I am working with angular-tree-component to show a category tree in a stateless component in angular. Currently, I have up and running the component and is time to make some unit tests. I am experiencing a problem because I want to test a function that executes the method update()
of treeModel, which is the property of the tree component that exposes the API of angular-tree-component. (See documentation here).
My function, called updateTree()
just run update()
on treeModel as below:
export class CategoryTreeComponent implements AfterViewInit {
@ViewChild('tree') treeComponent: TreeComponent;
treeModel: TreeModel;
constructor() {
}
ngAfterViewInit() {
debugger;
this.treeModel = this.treeComponent.treeModel;
}
updateTreeFn() {
this.treeModel.update();
}
}
So, I had written my unit test as shown below.
it('should call update method of treeModel', () => {
component.ngAfterViewInit();
fixture.detectChanges();
const spy = spyOn(component.treeModel, 'update');
component.updateTree();
expect(spy).toHaveBeenCalled();
});
The problem is that treeModel
is always undefined
. I notice that treeModel is a class property of type TreeModel on TreeComponent Class and update is a method of TreeModel object. Probably, this is why I am getting that error.
Any ideas on how I can test that update method is called and recognize treeModel object to spy it.