I will be very grateful if you help me to understand something. I have a component, which display some data. In ngOnInit() the http-service method is called to get data from server. Here is code:
// isLogged - is get property
ngOnInit() {
if (this.isLogged) {
this.loadData();
};
}
// Can't call in ngOnInit because this method is being used in other functions
loadData() {
this.http.getData()
.subscribe((result) => this.dataToDisplay = result);
}
get isLogged() {
// this function checks for token in localStorage via custom token service.
}
I write the test for loadData()
it('should load data', async(() => {
component.loadData();
fixture.whenStable().then( () => {
fixture.detectChanges();
expect(component.dataToDisplay).toEqual(mockData);
});
});
And it works as expected. But my question is: how I should test the ngOnInit? All that this method does is calling loadData. But how to check that if get property returns true the method is calling? I'm confused ((